Skip to content

Files

Latest commit

 

History

History
178 lines (129 loc) · 8.03 KB

strategies.rst

File metadata and controls

178 lines (129 loc) · 8.03 KB

Projects extending Hypothesis

Hypothesis has been eagerly used and extended by the open source community. This page lists extensions and applications; you can find more or newer packages by searching PyPI by keyword or filter by classifier, or search libraries.io.

If there's something missing which you think should be here, let us know!

Note

Being listed on this page does not imply that the Hypothesis maintainers endorse a package.

External strategies

Some packages provide strategies directly:

Others provide a function to infer a strategy from some other schema:

Other cool things

:pypi:`schemathesis` is a tool for testing web applications built with Open API / Swagger specifications. It reads the schema and generates test cases which will ensure that the application is compliant with its schema. The application under test could be written in any language, the only thing you need is a valid API schema in a supported format. Includes CLI and convenient :pypi:`pytest` integration. Powered by Hypothesis and :pypi:`hypothesis-jsonschema`, inspired by the earlier :pypi:`swagger-conformance` library.

Trio is an async framework with "an obsessive focus on usability and correctness", so naturally it works with Hypothesis! :pypi:`pytest-trio` includes :ref:`a custom hook <custom-function-execution>` that allows @given(...) to work with Trio-style async test functions, and :pypi:`hypothesis-trio` includes stateful testing extensions to support concurrent programs.

:pypi:`pymtl3` is "an open-source Python-based hardware generation, simulation, and verification framework with multi-level hardware modeling support", which ships with Hypothesis integrations to check that all of those levels are eqivalent, from function-level to register-transfer level and even to hardware.

:pypi:`libarchimedes` makes it easy to use Hypothesis in the Hy language, a Lisp embedded in Python.

:pypi:`battle_tested` is a fuzzing tool that will show you how your code can fail - by trying all kinds of inputs and reporting whatever happens.

:pypi:`pytest-subtesthack` functions as a workaround for :issue:`377`.

:pypi:`returns` uses Hypothesis to verify that Higher Kinded Types correctly implement functor, applicative, monad, and other laws; allowing a declarative approach to be combined with traditional pythonic code.

Writing an extension

See :gh-file:`CONTRIBUTING.rst` for more information.

New strategies can be added to Hypothesis, or published as an external package on PyPI - either is fine for most strategies. If in doubt, ask!

It's generally much easier to get things working outside, because there's more freedom to experiment and fewer requirements in stability and API style. We're happy to review and help with external packages as well as pull requests!

If you're thinking about writing an extension, please name it hypothesis-{something} - a standard prefix makes the community more visible and searching for extensions easier. And make sure you use the Framework :: Hypothesis trove classifier!

On the other hand, being inside gets you access to some deeper implementation features (if you need them) and better long-term guarantees about maintenance. We particularly encourage pull requests for new composable primitives that make implementing other strategies easier, or for widely used types in the standard library. Strategies for other things are also welcome; anything with external dependencies just goes in hypothesis.extra.

Registering strategies via setuptools entry points

If you would like to ship Hypothesis strategies for a custom type - either as part of the upstream library, or as a third-party extension, there's a catch: :func:`~hypothesis.strategies.from_type` only works after the corresponding call to :func:`~hypothesis.strategies.register_type_strategy`. This means that either

  • you have to try importing Hypothesis to register the strategy when your library is imported, though that's only useful at test time, or
  • the user has to call a 'register the strategies' helper that you provide before running their tests

Entry points are Python's standard way of automating the latter: when you register a "hypothesis" entry point in your setup.py, we'll import and run it automatically when hypothesis is imported. Nothing happens unless Hypothesis is already in use, and it's totally seamless for downstream users!

Let's look at an example. You start by adding a function somewhere in your package that does all the Hypothesis-related setup work:

# mymodule.py


class MyCustomType:
    def __init__(self, x: int):
        assert x >= 0, f"got {x}, but only positive numbers are allowed"
        self.x = x


def _hypothesis_setup_hook():
    import hypothesis.strategies as st

    st.register_type_strategy(MyCustomType, st.integers(min_value=0))

and then tell setuptools that this is your "hypothesis" entry point:

# setup.py

# You can list a module to import by dotted name
entry_points = {"hypothesis": ["_ = mymodule.a_submodule"]}

# Or name a specific function too, and Hypothesis will call it for you
entry_points = {"hypothesis": ["_ = mymodule:_hypothesis_setup_hook"]}

And that's all it takes!

Interaction with :pypi:`pytest-cov`

Because pytest does not load plugins from entrypoints in any particular order, using the Hypothesis entrypoint may import your module before :pypi:`pytest-cov` starts. This is a known issue, but there are workarounds.

You can use :command:`coverage run pytest ...` instead of :command:`pytest --cov ...`, opting out of the pytest plugin entirely. Alternatively, you can ensure that Hypothesis is loaded after coverage measurement is started by disabling the entrypoint, and loading our pytest plugin from your conftest.py instead:

echo "pytest_plugins = ['hypothesis.extra.pytestplugin']\n" > tests/conftest.py
pytest -p "no:hypothesispytest" ...