Debian Codegraph @

hypothesis-python/tests/cover/test_seed_printing.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 time
12
13 import pytest
14
15 from hypothesis import (
16 HealthCheck,
17 Verbosity,
18 assume,
19 core,
20 given,
21 settings,
22 strategies as st,
23 )
24 from hypothesis.database import InMemoryExampleDatabase
25 from hypothesis.errors import FailedHealthCheck
26
27 from tests.common.utils import all_values, capture_out
28
29
30 @pytest.mark.parametrize("in_pytest", [False, True])
31 @pytest.mark.parametrize("fail_healthcheck", [False, True])
32 @pytest.mark.parametrize("verbosity", [Verbosity.normal, Verbosity.quiet])
33 def test_prints_seed_only_on_healthcheck(
34 monkeypatch, in_pytest, fail_healthcheck, verbosity
35 ):
36 monkeypatch.setattr(core, "running_under_pytest", in_pytest)
37
38 strategy = st.integers()
39 if fail_healthcheck:
40
41 def slow_map(i):
42 time.sleep(10)
43 return i
44
45 strategy = strategy.map(slow_map)
46 expected_exc = FailedHealthCheck
47 else:
48 expected_exc = AssertionError
49
50 @settings(database=None, verbosity=verbosity, suppress_health_check=())
51 @given(strategy)
52 def test(i):
53 assert fail_healthcheck
54
55 with capture_out() as o, pytest.raises(expected_exc):
56 test()
57
58 output = o.getvalue()
59
60 seed = test._hypothesis_internal_use_generated_seed
61 assert seed is not None
62 if fail_healthcheck and verbosity != Verbosity.quiet:
63 assert f"@seed({seed})" in output
64 contains_pytest_instruction = f"--hypothesis-seed={seed}" in output
65 assert contains_pytest_instruction == in_pytest
66 else:
67 assert "@seed" not in output
68 assert f"--hypothesis-seed={seed}" not in output
69
70
71 def test_uses_global_force(monkeypatch):
72 monkeypatch.setattr(core, "global_force_seed", 42)
73
74 @given(st.integers())
75 def test(i):
76 raise ValueError
77
78 output = []
79
80 for _ in range(2):
81 with capture_out() as o, pytest.raises(ValueError):
82 test()
83 output.append(o.getvalue())
84
85 assert output[0] == output[1]
86 assert "@seed" not in output[0]
87
88
89 def test_does_print_on_reuse_from_database():
90 passes_healthcheck = False
91
92 database = InMemoryExampleDatabase()
93
94 @settings(database=database, suppress_health_check=[])
95 @given(st.integers())
96 def test(i):
97 assume(passes_healthcheck)
98 raise ValueError
99
100 with capture_out() as o, pytest.raises(FailedHealthCheck):
101 test()
102
103 assert "@seed" in o.getvalue()
104
105 passes_healthcheck = True
106
107 with capture_out() as o, pytest.raises(ValueError):
108 test()
109
110 assert all_values(database)
111 assert "@seed" not in o.getvalue()
112
113 passes_healthcheck = False
114
115 with capture_out() as o, pytest.raises(FailedHealthCheck):
116 test()
117
118 assert "@seed" in o.getvalue()
119
120
121 @pytest.mark.parametrize("in_pytest", [True, False])
122 def test_prints_seed_on_very_slow_shrinking(monkeypatch, in_pytest):
123 monkeypatch.setattr(core, "running_under_pytest", in_pytest)
124
125 @settings(database=None, deadline=None, suppress_health_check=list(HealthCheck))
126 @given(st.integers(min_value=0, max_value=2**64 - 1))
127 def test(n):
128 time.sleep(10)
129 assert n <= 2**33
130
131 with capture_out() as o, pytest.raises(AssertionError):
132 test()
133
134 output = o.getvalue()
135 assert "Hypothesis has spent more than five minutes" in output
136 assert (
137 "This test function exited early because it took too long to shrink" in output
138 )
139 seed = test._hypothesis_internal_use_generated_seed
140 assert output.count(f"@seed({seed})") == 1
141 assert (f"--hypothesis-seed={seed}" in output) == in_pytest