Meaning
A hypothesis decorator that supplies two arbitrary integer arguments to a test function for property-based testing.
Primary Function
Decorates a test function to generate random integer pairs via hypothesis strategies, enabling property-based testing.
Communicative Purpose
Indicates that the decorated function is a property‑based test expecting two integer inputs generated by hypothesis strategies.
Pattern
@given(st.<strategy1>(), st.<strategy2>()) where each strategy returns a value for a test function parameter.
Core Structure
@given(strategy1, strategy2) where each strategy is a call from hypothesis.strategies returning a value.
Função primária
Decorates a test function to generate random integer pairs via hypothesis strategies, enabling property-based testing.
Propósito comunicativo
Indicates that the decorated function is a property‑based test expecting two integer inputs generated by hypothesis strategies.
Situações de gatilho
When writing property‑based tests with the hypothesis library to automatically generate diverse integer inputs for testing invariants.
Contextos
Used in Python test modules that import hypothesis and hypothesis.strategies as st, typically in test suites using pytest or unittest.
Padrão
@given(st.<strategy1>(), st.<strategy2>()) where each strategy returns a value for a test function parameter.
Estrutura central
@given(strategy1, strategy2) where each strategy is a call from hypothesis.strategies returning a value.
Slots de substituição
strategy1: hypothesis.strategies.SearchStrategy returning a value for the first parameter; strategy2: hypothesis.strategies.SearchStrategy returning a value for the second parameter.
Colocados típicos
- hypothesis
- given
- st
- integers
- floats
- text
- lists
- tuples
- data
- settings
- example
- seed
Substituições comuns
- st.integers() → st.floats()
- st.text()
- st.lists(st.integers())
- st.sets(st.integers())
- etc.
- varying number of arguments.
Erros comuns
Forgetting to import hypothesis or st, causing NameError. Using a strategy that returns non‑hashable objects when the test function expects hashable arguments, leading to TypeError during shrinking. Mismatching the number of @given arguments with the test function parameters, causing a TypeError about missing or extra arguments. Using mutable strategies (e.g., lists) without copying, resulting in side‑effects across generated examples. Assuming hypothesis produces only positive integers when st.integers() includes negatives and zero, which can miss edge cases.
Similar / contraste
@given() with no arguments – provides no generated arguments, useful for tests that need no input. @given(st.data()) – provides a single data object for drawing complex data interactively. @given(x=st.integers(), y=st.integers()) – same behavior but uses keyword arguments for clarity. @given(st.lists(st.integers())) – generates a list of integers instead of two separate integers.
Interferências
Coming from plain unittest: may forget to add @given and write a plain test method, resulting in no property‑based testing – add @given decorator to enable hypothesis. Coming from pytest: may rely on pytest fixtures and forget that @given does not work with fixture injection; use @given as an outer decorator or combine with hypothesis given as a fixture. Coming from languages with built‑in random testing (e.g., QuickCheck in Haskell): may expect lazy infinite streams; hypothesis uses explicit strategies and shrinking, so adapt to its explicit drawing model.
Família do chunk
- hypothesis decorators
- property‑based testing patterns
- given decorator family
Nuance
Do not use @given when deterministic examples are sufficient; property‑based testing can be slower and may miss domain‑specific edge cases that manual examples catch. The default integer strategy spans the full Python integer range, which can generate very large numbers; consider st.integers(min_value, max_value) to bound the search space and improve performance. Hypothesis automatically shrinks failing examples to a minimal counterexample; however, if the test involves non‑deterministic state (e.g., random number generators) the shrinking process may be misleading – isolate side effects.
Efeito pragmático
Enables automatic discovery of edge‑case bugs by generating a wide range of integer inputs, increasing confidence that properties hold for all possible inputs.
Dica de memória
Think of @given as a factory that feeds your test with random data, like a fuzzer that keeps trying until it finds a counterexample.
Nota
The st.integers() strategy produces values across the whole integer range, including negatives and zero; use min_value/max_value arguments to constrain the range when needed.
Upgrade path
Using @given with more complex strategies (e.g., tuples, lists, custom strategies) or adjusting settings such as max_examples and deadline for larger search spaces.
Log in to save chunks.