Coverage for /usr/local/lib/python3.7/site-packages/hypothesis/extra/pytestplugin.py : 25%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of Hypothesis, which may be found at
2# https://github.com/HypothesisWorks/hypothesis/
3#
4# Most of this work is copyright (C) 2013-2020 David R. MacIver
5# (david@drmaciver.com), but it contains contributions by others. See
6# CONTRIBUTING.rst for a full list of people who may hold copyright, and
7# consult the git log if you need to determine who owns an individual
8# contribution.
9#
10# This Source Code Form is subject to the terms of the Mozilla Public License,
11# v. 2.0. If a copy of the MPL was not distributed with this file, You can
12# obtain one at https://mozilla.org/MPL/2.0/.
13#
14# END HEADER
16from distutils.version import LooseVersion
17from inspect import signature
19import pytest
21from hypothesis import Verbosity, core, settings
22from hypothesis._settings import note_deprecation
23from hypothesis.errors import InvalidArgument
24from hypothesis.internal.detection import is_hypothesis_test
25from hypothesis.reporting import default as default_reporter, with_reporter
26from hypothesis.statistics import collector
28LOAD_PROFILE_OPTION = "--hypothesis-profile"
29VERBOSITY_OPTION = "--hypothesis-verbosity"
30PRINT_STATISTICS_OPTION = "--hypothesis-show-statistics"
31SEED_OPTION = "--hypothesis-seed"
34class StoringReporter:
35 def __init__(self, config):
36 self.config = config
37 self.results = []
39 def __call__(self, msg):
40 if self.config.getoption("capture", "fd") == "no":
41 default_reporter(msg)
42 if not isinstance(msg, str):
43 msg = repr(msg)
44 self.results.append(msg)
47if LooseVersion(pytest.__version__) < "4.3": # pragma: no cover
48 import warnings
49 from hypothesis.errors import HypothesisWarning
51 PYTEST_TOO_OLD_MESSAGE = """
52 You are using Pytest version %s. Hypothesis tests work with any test
53 runner, but our Pytest plugin requires Pytest 4.3 or newer.
54 Note that the Pytest developers no longer support this version either!
55 Disabling the Hypothesis pytest plugin...
56 """
57 warnings.warn(PYTEST_TOO_OLD_MESSAGE % (pytest.__version__,), HypothesisWarning)
59else:
61 def pytest_addoption(parser):
62 group = parser.getgroup("hypothesis", "Hypothesis")
63 group.addoption(
64 LOAD_PROFILE_OPTION,
65 action="store",
66 help="Load in a registered hypothesis.settings profile",
67 )
68 group.addoption(
69 VERBOSITY_OPTION,
70 action="store",
71 choices=[opt.name for opt in Verbosity],
72 help="Override profile with verbosity setting specified",
73 )
74 group.addoption(
75 PRINT_STATISTICS_OPTION,
76 action="store_true",
77 help="Configure when statistics are printed",
78 default=False,
79 )
80 group.addoption(
81 SEED_OPTION,
82 action="store",
83 help="Set a seed to use for all Hypothesis tests",
84 )
86 def pytest_report_header(config):
87 profile = config.getoption(LOAD_PROFILE_OPTION)
88 if not profile:
89 profile = settings._current_profile
90 settings_str = settings.get_profile(profile).show_changed()
91 if settings_str != "":
92 settings_str = " -> %s" % (settings_str)
93 if (
94 config.option.verbose >= 1
95 or settings.default.verbosity >= Verbosity.verbose
96 ):
97 return "hypothesis profile %r%s" % (profile, settings_str)
99 def pytest_configure(config):
100 core.running_under_pytest = True
101 profile = config.getoption(LOAD_PROFILE_OPTION)
102 if profile:
103 settings.load_profile(profile)
104 verbosity_name = config.getoption(VERBOSITY_OPTION)
105 if verbosity_name:
106 verbosity_value = Verbosity[verbosity_name]
107 profile_name = "%s-with-%s-verbosity" % (
108 settings._current_profile,
109 verbosity_name,
110 )
111 # register_profile creates a new profile, exactly like the current one,
112 # with the extra values given (in this case 'verbosity')
113 settings.register_profile(profile_name, verbosity=verbosity_value)
114 settings.load_profile(profile_name)
115 seed = config.getoption(SEED_OPTION)
116 if seed is not None:
117 try:
118 seed = int(seed)
119 except ValueError:
120 pass
121 core.global_force_seed = seed
122 config.addinivalue_line("markers", "hypothesis: Tests which use hypothesis.")
124 @pytest.hookimpl(hookwrapper=True)
125 def pytest_runtest_call(item):
126 if not hasattr(item, "obj"):
127 yield
128 elif not is_hypothesis_test(item.obj):
129 # If @given was not applied, check whether other hypothesis
130 # decorators were applied, and raise an error if they were.
131 if getattr(item.obj, "is_hypothesis_strategy_function", False):
132 raise InvalidArgument(
133 "%s is a function that returns a Hypothesis strategy, but pytest "
134 "has collected it as a test function. This is useless as the "
135 "function body will never be executed. To define a test "
136 "function, use @given instead of @composite." % (item.nodeid,)
137 )
138 message = "Using `@%s` on a test without `@given` is completely pointless."
139 for name, attribute in [
140 ("example", "hypothesis_explicit_examples"),
141 ("seed", "_hypothesis_internal_use_seed"),
142 ("settings", "_hypothesis_internal_settings_applied"),
143 ("reproduce_example", "_hypothesis_internal_use_reproduce_failure"),
144 ]:
145 if hasattr(item.obj, attribute):
146 raise InvalidArgument(message % (name,))
147 yield
148 else:
149 # Warn about function-scoped fixtures, excluding autouse fixtures because
150 # the advice is probably not actionable and the status quo seems OK...
151 # See https://github.com/HypothesisWorks/hypothesis/issues/377 for detail.
152 argnames = None
153 for fx_defs in item._request._fixturemanager.getfixtureinfo(
154 node=item, func=item.function, cls=None
155 ).name2fixturedefs.values():
156 if argnames is None:
157 argnames = frozenset(signature(item.function).parameters)
158 for fx in fx_defs:
159 if fx.scope == "function" and fx.argname in argnames:
160 note_deprecation(
161 "%s uses the %r fixture, but function-scoped fixtures "
162 "should not be used with @given(...) tests, because "
163 "fixtures are not reset between generated examples!"
164 % (item.nodeid, fx.argname),
165 since="2020-02-29",
166 )
168 if item.get_closest_marker("parametrize") is not None:
169 # Give every parametrized test invocation a unique database key
170 key = item.nodeid.encode("utf-8")
171 item.obj.hypothesis.inner_test._hypothesis_internal_add_digest = key
173 store = StoringReporter(item.config)
175 def note_statistics(stats):
176 lines = [item.nodeid + ":", ""] + stats.get_description() + [""]
177 item.hypothesis_statistics = lines
179 with collector.with_value(note_statistics):
180 with with_reporter(store):
181 yield
182 if store.results:
183 item.hypothesis_report_information = list(store.results)
185 @pytest.hookimpl(hookwrapper=True)
186 def pytest_runtest_makereport(item, call):
187 report = (yield).get_result()
188 if hasattr(item, "hypothesis_report_information"):
189 report.sections.append(
190 ("Hypothesis", "\n".join(item.hypothesis_report_information))
191 )
192 if hasattr(item, "hypothesis_statistics") and report.when == "teardown":
193 val = ("hypothesis-stats", item.hypothesis_statistics)
194 report.user_properties.append(val)
196 def pytest_terminal_summary(terminalreporter):
197 if not terminalreporter.config.getoption(PRINT_STATISTICS_OPTION):
198 return
199 terminalreporter.section("Hypothesis Statistics")
200 # terminalreporter.stats is a dict, where the empty string appears to
201 # always be the key for a list of _pytest.reports.TestReport objects
202 # (where we stored the statistics data in pytest_runtest_makereport above)
203 for test_report in terminalreporter.stats.get("", []):
204 for name, lines in test_report.user_properties:
205 if name == "hypothesis-stats" and test_report.when == "teardown":
206 for li in lines:
207 terminalreporter.write_line(li)
209 def pytest_collection_modifyitems(items):
210 for item in items:
211 if isinstance(item, pytest.Function) and is_hypothesis_test(item.obj):
212 item.add_marker("hypothesis")
215def load():
216 """Required for `pluggy` to load a plugin from setuptools entrypoints."""