Meaning
@given(st.integers()) is a hypothesis decorator that configures a test function to receive automatically generated integer arguments for property-based testing.
Primary Function
Decorates a test function to generate integer inputs via hypothesis.strategies.integers() for property-based testing.
Communicative Purpose
Indicates that the decorated function is a property‑based test that will be invoked with automatically generated integer arguments.
Pattern
@given(st.integers())
Core Structure
@given(st.<strategy>())
Função primária
Decorates a test function to generate integer inputs via hypothesis.strategies.integers() for property-based testing.
Propósito comunicativo
Indicates that the decorated function is a property‑based test that will be invoked with automatically generated integer arguments.
Situações de gatilho
Used when writing property‑based tests with the hypothesis library to generate random integer inputs for verifying properties across a wide range of values.
Contextos
Property‑based testing in Python using the hypothesis library; typically applied to test functions that assert properties of functions or data structures over a range of integer inputs.
Padrão
@given(st.integers())
Estrutura central
@given(st.<strategy>())
Slots de substituição
strategy: hypothesis.strategy (e.g., st.integers(), st.text(), st.lists(st.integers()))
Colocados típicos
- @example
- @seed
- @settings
- hypothesis.strategies
- assert
- assert_equal
Substituições comuns
- st.text() – generates strings instead of integers
- st.floats() – generates floating‑point numbers
- st.lists(st.integers()) – generates lists of integers
- st.booleans() – generates booleans
- each changes the type of generated data and the properties that can be tested.
Erros comuns
{"cause":"Failing to import hypothesis or hypothesis.strategies","consequence":"NameError: name 'st' is not defined"} {"cause":"Using a non‑hashable strategy (e.g., st.lists()) for arguments that must be hashable","consequence":"InvalidArgumentError: arguments must be hashable"} {"cause":"Assuming generated integers are within a specific range without specifying it","consequence":"Test may fail on unexpected large/small values, giving false negatives"} {"cause":"Neglecting to add an assertion inside the test function","consequence":"Test passes trivially because no property is actually checked"} {"cause":"Using @given with a function that has default arguments","consequence":"DuplicateArgumentError: argument receives a value from both @given and a default"}
Similar / contraste
{"concept":"@given(st.tuples(...))","distinction":"generates tuples of values instead of single integers"} {"concept":"@given(st.lists(...))","distinction":"generates lists rather than scalar integers"} {"concept":"@given(st.data())","distinction":"provides a data object for drawing additional data dynamically inside the test"} {"concept":"@example(42)","distinction":"provides a fixed example in addition to generated data"} {"concept":"@settings(max_examples=500)","distinction":"controls the number of generated examples rather than the data generation strategy"}
Interferências
Coming from unittest: may forget to add assertions, assuming the test framework will verify outcomes — hypothesis requires explicit assertions. Coming from pytest: may rely on fixture injection; @given does not work with fixture parameters unless using hypothesis[pytest] plugin. Coming from doctest: expects interactive examples; hypothesis generates random inputs, making doctests unreliable.
Família do chunk
- @given
- @example
- @settings
- @seed
- hypothesis.strategies
Nuance
Do not use @given when the function under test has side effects that depend on specific input values; property‑based testing assumes the property should hold for all generated inputs. Performance: generating many complex examples (e.g., large lists) can slow test runs; keep strategies simple or use settings like max_examples to bound runtime. Boundary conditions: hypothesis shrinks failing examples to a minimal falsifying case; ensure your properties are stable under shrinking (e.g., avoid relying on specific exception messages that may change).
Efeito pragmático
Enables systematic exploration of a large input space, increasing confidence that properties hold for edge cases and reducing the likelihood of missing bugs due to insufficient manual test cases.
Dica de memória
Think of @given(st.integers()) as a robot that hands your test function random integer LEGO bricks, letting you build and test properties of your code across countless constructions.
Nota
The st.integers() strategy by default generates integers across the full range supported by Python’s arbitrary‑precision int; you can constrain it with arguments like min_value, max_value.
Upgrade path
Consider using @given(st.data()) for more complex data drawing or @given(st.tuples(...)) when you need multiple related inputs.
Log in to save chunks.