File: detect-hypothesis-tests.rst

package info (click to toggle)
python-hypothesis 6.138.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,304 kB
  • sloc: python: 63,312; ruby: 1,107; sh: 253; makefile: 41; javascript: 6
file content (34 lines) | stat: -rw-r--r-- 999 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Detect Hypothesis tests
-----------------------

How to dynamically determine whether a test function has been defined with Hypothesis.

Via ``is_hypothesis_test``
~~~~~~~~~~~~~~~~~~~~~~~~~~

The most straightforward way is to use |is_hypothesis_test|:

.. code-block:: python

    from hypothesis import is_hypothesis_test

    @given(st.integers())
    def f(n): ...

    assert is_hypothesis_test(f)

This works for stateful tests as well:

.. code-block:: python

    from hypothesis import is_hypothesis_test
    from hypothesis.stateful import RuleBasedStateMachine

    class MyStateMachine(RuleBasedStateMachine): ...

    assert is_hypothesis_test(MyStateMachine.TestCase().runTest)

Via pytest
~~~~~~~~~~

If you're working with :pypi:`pytest`, the :ref:`Hypothesis pytest plugin <pytest-plugin>` automatically adds the ``@pytest.mark.hypothesis`` mark to all Hypothesis tests. You can use ``node.get_closest_marker("hypothesis")`` or similar methods to detect the existence of this mark.