| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from types import MethodType |
| 12 |
|
| 13 |
|
| 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 |
.. seealso:: |
| 35 |
|
| 36 |
See also the :doc:`Detect Hypothesis tests |
| 37 |
</how-to/detect-hypothesis-tests>` how-to. |
| 38 |
""" |
| 39 |
if isinstance(f, MethodType): |
| 40 |
return is_hypothesis_test(f.__func__) |
| 41 |
return getattr(f, "is_hypothesis_test", False) |