Skip to content

Commit 7b9a272

Browse files
committed
document is_hypothesis_test
1 parent 33c050c commit 7b9a272

File tree

8 files changed

+42
-11
lines changed

8 files changed

+42
-11
lines changed

hypothesis-python/RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: minor
2+
3+
Add |is_hypothesis_test|, for third-party libraries which want to determine whether a test has been defined with Hypothesis.

hypothesis-python/docs/conf.py

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def setup(app):
259259
.. |BackgroundWriteDatabase| replace:: :class:`~hypothesis.database.BackgroundWriteDatabase`
260260
.. |RedisExampleDatabase| replace:: :class:`~hypothesis.extra.redis.RedisExampleDatabase`
261261
262+
.. |is_hypothesis_test| replace:: :func:`~hypothesis.is_hypothesis_test`
263+
262264
.. |str| replace:: :obj:`python:str`
263265
.. |int| replace:: :obj:`python:int`
264266
.. |bool| replace:: :obj:`python:bool`

hypothesis-python/docs/reference/api.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1013,3 +1013,10 @@ For authors of test runners however, assigning to the ``inner_test`` attribute o
10131013
and ``**kwargs`` expected by the original test.
10141014

10151015
If the end user has also specified a custom executor using the ``execute_example`` method, it - and all other execution-time logic - will be applied to the *new* inner test assigned by the test runner.
1016+
1017+
Detecting Hypothesis tests
1018+
--------------------------
1019+
1020+
To determine whether a test has been defined with Hypothesis or not, use |is_hypothesis_test|:
1021+
1022+
.. autofunction:: hypothesis.is_hypothesis_test

hypothesis-python/src/_hypothesis_pytestplugin.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ def pytest_runtest_call(item):
203203
yield
204204
return
205205

206-
from hypothesis import core
207-
from hypothesis.internal.detection import is_hypothesis_test
206+
from hypothesis import core, is_hypothesis_test
208207

209208
# See https://github.com/pytest-dev/pytest/issues/9159
210209
core.pytest_shows_exceptiongroups = (
@@ -415,7 +414,7 @@ def pytest_collection_modifyitems(items):
415414
if "hypothesis" not in sys.modules:
416415
return
417416

418-
from hypothesis.internal.detection import is_hypothesis_test
417+
from hypothesis import is_hypothesis_test
419418

420419
for item in items:
421420
if isinstance(item, pytest.Function) and is_hypothesis_test(item.obj):
@@ -433,7 +432,7 @@ def pytest_sessionstart(session):
433432

434433
def _ban_given_call(self, function):
435434
if "hypothesis" in sys.modules:
436-
from hypothesis.internal.detection import is_hypothesis_test
435+
from hypothesis import is_hypothesis_test
437436

438437
if is_hypothesis_test(function):
439438
raise RuntimeError(

hypothesis-python/src/hypothesis/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
from hypothesis.core import example, find, given, reproduce_failure, seed
3030
from hypothesis.entry_points import run
31+
from hypothesis.internal.detection import is_hypothesis_test
3132
from hypothesis.internal.entropy import register_random
3233
from hypothesis.utils.conventions import infer
3334
from hypothesis.version import __version__, __version_info__
@@ -45,6 +46,7 @@
4546
"find",
4647
"given",
4748
"infer",
49+
"is_hypothesis_test",
4850
"note",
4951
"register_random",
5052
"reject",

hypothesis-python/src/hypothesis/internal/detection.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@
1111
from types import MethodType
1212

1313

14-
def is_hypothesis_test(test: object) -> bool:
15-
if isinstance(test, MethodType):
16-
return is_hypothesis_test(test.__func__)
17-
return getattr(test, "is_hypothesis_test", False)
14+
def is_hypothesis_test(f: object) -> bool:
15+
"""
16+
Returns ``True`` if ``f`` represents a test function that has been defined
17+
with Hypothesis. This is true for:
18+
19+
* Functions decorated with |@given|
20+
* The ``runTest`` method of stateful tests
21+
22+
For example:
23+
24+
.. code-block:: python
25+
26+
@given(st.integers())
27+
def f(n): ...
28+
29+
class MyStateMachine(RuleBasedStateMachine): ...
30+
31+
assert is_hypothesis_test(f)
32+
assert is_hypothesis_test(MyStateMachine.TestCase().runTest)
33+
"""
34+
if isinstance(f, MethodType):
35+
return is_hypothesis_test(f.__func__)
36+
return getattr(f, "is_hypothesis_test", False)

hypothesis-python/tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import pytest
2222
from _pytest.monkeypatch import MonkeyPatch
2323

24+
from hypothesis import is_hypothesis_test
2425
from hypothesis._settings import is_in_ci
2526
from hypothesis.errors import NonInteractiveExampleWarning
2627
from hypothesis.internal.compat import add_note
2728
from hypothesis.internal.conjecture import junkdrawer
28-
from hypothesis.internal.detection import is_hypothesis_test
2929

3030
from tests.common import TIME_INCREMENT
3131
from tests.common.setup import run

hypothesis-python/tests/cover/test_detection.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11-
from hypothesis import given
12-
from hypothesis.internal.detection import is_hypothesis_test
11+
from hypothesis import given, is_hypothesis_test
1312
from hypothesis.stateful import RuleBasedStateMachine, rule
1413
from hypothesis.strategies import integers
1514

0 commit comments

Comments
 (0)