| 1 |
Detect Hypothesis tests |
| 2 |
----------------------- |
| 3 |
|
| 4 |
How to dynamically determine whether a test function has been defined with Hypothesis. |
| 5 |
|
| 6 |
Via ``is_hypothesis_test`` |
| 7 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 |
|
| 9 |
The most straightforward way is to use |is_hypothesis_test|: |
| 10 |
|
| 11 |
.. code-block:: python |
| 12 |
|
| 13 |
from hypothesis import is_hypothesis_test |
| 14 |
|
| 15 |
@given(st.integers()) |
| 16 |
def f(n): ... |
| 17 |
|
| 18 |
assert is_hypothesis_test(f) |
| 19 |
|
| 20 |
This works for stateful tests as well: |
| 21 |
|
| 22 |
.. code-block:: python |
| 23 |
|
| 24 |
from hypothesis import is_hypothesis_test |
| 25 |
from hypothesis.stateful import RuleBasedStateMachine |
| 26 |
|
| 27 |
class MyStateMachine(RuleBasedStateMachine): ... |
| 28 |
|
| 29 |
assert is_hypothesis_test(MyStateMachine.TestCase().runTest) |
| 30 |
|
| 31 |
Via pytest |
| 32 |
~~~~~~~~~~ |
| 33 |
|
| 34 |
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. |