Debian Codegraph @

hypothesis-python/tests/pytest/test_constant_collection_timing.py

raw
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 import re
12
13 import pytest
14
15 pytest_plugins = "pytester"
16
17
18 CONFTEST = """
19 import time
20 import hypothesis.internal.conjecture.providers as providers
21
22 _called = False
23
24 def slow_get_local_constants():
25 global _called
26 if not _called:
27 _called = True
28 time.sleep(0.1)
29 return providers._local_constants
30
31 providers._get_local_constants = slow_get_local_constants
32 providers._sys_modules_len = None
33 """
34
35
36 TESTSUITE = """
37 from hypothesis import given, Phase, settings
38 from hypothesis.strategies import integers
39 from hypothesis.internal.conjecture.providers import _get_local_constants
40
41 def test_first():
42 # Force constant collection to happen during this test
43 _get_local_constants()
44
45 @given(integers())
46 @settings(phases=[Phase.generate], max_examples=1, database=None)
47 def test_second(x):
48 pass
49 """
50
51
52 @pytest.mark.parametrize("plugin_disabled", [False, True])
53 def test_constant_collection_timing(testdir, plugin_disabled):
54 # See https://github.com/HypothesisWorks/hypothesis/issues/4627
55 testdir.makeconftest(CONFTEST)
56 testdir.makepyfile(TESTSUITE)
57
58 args = ["--durations=0", "-vv"]
59 if plugin_disabled:
60 args += ["-p", "no:hypothesispytest"]
61
62 result = testdir.runpytest(*args)
63 result.assert_outcomes(passed=2)
64
65 output = "\n".join(result.stdout.lines)
66 match = re.search(r"([\d.]+)s call\s+\S+::test_first", output)
67 assert match, f"Could not find test_first timing in:\n{output}"
68 test_first_time = float(match.group(1))
69
70 if plugin_disabled:
71 assert test_first_time >= 0.05, f"took {test_first_time}s, expected >= 0.05s"
72 else:
73 assert test_first_time < 0.05, f"took {test_first_time}s, expected < 0.05s"