{"id":2725,"library":"pytest-hypothesis","title":"pytest-hypothesis","description":"pytest-hypothesis is a bridge for using the Hypothesis property-based testing library with the pytest test framework. It enables developers to write tests that automatically generate varied inputs, helping to discover edge cases and subtle bugs in their code. While the `pytest-hypothesis` PyPI package is a legacy wrapper, its core functionality for pytest integration is now built directly into the main Hypothesis library (version 6.0.0 and later). It is actively maintained as part of Hypothesis, with a continuous release cadence.","status":"active","version":"10.0.3","language":"en","source_language":"en","source_url":"https://github.com/HypothesisWorks/hypothesis","tags":["pytest","testing","property-based testing","hypothesis","test generation"],"install":[{"cmd":"pip install hypothesis pytest","lang":"bash","label":"Install Hypothesis and pytest"}],"dependencies":[{"reason":"Required test runner for integration.","package":"pytest"},{"reason":"The core property-based testing library.","package":"hypothesis"}],"imports":[{"note":"The primary decorator for defining property-based tests.","symbol":"given","correct":"from hypothesis import given"},{"note":"Used to access built-in data generation strategies (e.g., st.integers(), st.text()).","symbol":"strategies","correct":"from hypothesis import strategies as st"}],"quickstart":{"code":"from hypothesis import given, strategies as st\nimport pytest\n\ndef add(a, b):\n    return a + b\n\n@given(st.integers(), st.integers())\ndef test_add_integers(a, b):\n    \"\"\"Test that addition is commutative and associative for integers.\"\"\"\n    assert add(a, b) == a + b\n    assert add(a, b) == add(b, a) # Commutativity\n    # Run this file with `pytest -v your_test_file.py`\n\n# Example of using assume() to filter inputs without failing the test\n@given(st.integers(min_value=1), st.integers(min_value=1))\ndef test_division_produces_float_or_int(numerator, denominator):\n    # Hypothesis generates pairs, but we might only care about certain conditions\n    # For instance, if we only want to test non-zero results, we can use assume\n    from hypothesis import assume\n    assume(denominator != 0)\n    result = numerator / denominator\n    assert isinstance(result, (float, int))\n    assert numerator == result * denominator\n","lang":"python","description":"This quickstart demonstrates how to use `hypothesis.given` decorator with `pytest`. Tests decorated with `@given` will be run multiple times by Hypothesis, with automatically generated inputs adhering to the specified strategies (e.g., `st.integers()`). Run these tests using the `pytest` command line tool."},"warnings":[{"fix":"Remove `pytest-hypothesis` from your project's dependencies and ensure you have `hypothesis` installed. The integration works automatically.","message":"The standalone `pytest-hypothesis` PyPI package is deprecated. Its functionality is now integrated directly into the core `Hypothesis` library (version 6.0.0 and newer). You no longer need to install `pytest-hypothesis` separately.","severity":"deprecated","affected_versions":"All versions of `pytest-hypothesis` (legacy package), Hypothesis >= 6.0.0"},{"fix":"Manually manage state resets within the test function (e.g., using context managers or explicit setup/teardown in the test body) or use broader fixture scopes (e.g., `module`, `session`) if appropriate, and ensure test examples are isolated.","message":"`pytest` function-scoped fixtures (e.g., `@pytest.fixture(scope='function')`) will run only once for the entire Hypothesis test function, not once for each generated example. This can lead to unexpected state issues if your fixture is meant to reset state per example (e.g., database transactions).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your test environment and any stateful resources (e.g., databases, files) are properly cleaned up and reset before each Hypothesis example. Use fixtures with appropriate scope or explicit setup/teardown.","message":"Hypothesis tests can report 'Flaky: Inconsistent test results!' if the test's outcome depends on external state or non-deterministic factors that are not reset between examples. This often indicates a lack of proper test isolation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that `test_` functions using `@given` do not explicitly return any value.","message":"Hypothesis test functions decorated with `@given` should not return any value other than `None`. Returning a value will cause Hypothesis to error, and `pytest` itself will also raise a warning or error for non-None return values from test functions.","severity":"gotcha","affected_versions":"All versions of Hypothesis and pytest"},{"fix":"Replace `assert` statements used purely for input filtering with `from hypothesis import assume; assume(condition)`.","message":"When defining strategies, if certain generated inputs are invalid for your test's logic but are valid according to the strategy, use `hypothesis.assume()` to filter them out. Using `assert False` for filtering will be reported as a test failure, not a discarded example, and prevents Hypothesis from finding a valid failing example.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}