Debian Codegraph @

hypothesis-python/tests/pytest/test_seeding.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 from hypothesis._settings import _CI_VARS
16
17 from tests.common.utils import skipif_threading
18
19 pytest_plugins = "pytester"
20
21
22 TEST_SUITE = """
23 from hypothesis import given, settings, assume
24 import hypothesis.strategies as st
25
26
27 first = None
28
29 @settings(database=None)
30 @given(st.integers())
31 def test_fails_once(some_int):
32 assume(abs(some_int) > 1000)
33 global first
34 if first is None:
35 first = some_int
36
37 assert some_int != first
38 """
39
40
41 CONTAINS_SEED_INSTRUCTION = re.compile(r"--hypothesis-seed=\d+", re.MULTILINE)
42
43
44 @skipif_threading
45 @pytest.mark.parametrize("seed", [0, 42, "foo"])
46 def test_runs_repeatably_when_seed_is_set(seed, testdir):
47 script = testdir.makepyfile(TEST_SUITE)
48
49 results = [
50 testdir.runpytest(
51 script, "--verbose", "--strict-markers", f"--hypothesis-seed={seed}", "-rN"
52 )
53 for _ in range(2)
54 ]
55
56 failure_lines = []
57 for r in results:
58 assert all("--hypothesis-seed" not in l for l in r.stdout.lines)
59 failure_line = [l for l in r.stdout.lines if "some_int=" in l]
60 # each iteration should fail
61 assert len(failure_line) == 1
62 failure_lines.append(failure_line[0])
63
64 # all the same failure
65 assert len(set(failure_lines)) == 1
66
67
68 HEALTH_CHECK_FAILURE = """
69 import os
70
71 from hypothesis import given, strategies as st, assume, reject
72
73 RECORD_EXAMPLES = <file>
74
75 if os.path.exists(RECORD_EXAMPLES):
76 target = None
77 with open(RECORD_EXAMPLES, "r", encoding="utf-8") as i:
78 seen = set(map(int, i.read().strip().split("\\n")))
79 else:
80 target = open(RECORD_EXAMPLES, "w", encoding="utf-8")
81
82 @given(st.integers())
83 def test_failure(i):
84 if target is None:
85 assume(i not in seen)
86 else:
87 target.write(f"{i}\\n")
88 reject()
89 """
90
91
92 def test_repeats_healthcheck_when_following_seed_instruction(
93 testdir, tmp_path, monkeypatch
94 ):
95 for key in _CI_VARS:
96 monkeypatch.delenv(key, raising=False)
97
98 health_check_test = HEALTH_CHECK_FAILURE.replace(
99 "<file>", repr(str(tmp_path / "seen"))
100 )
101
102 script = testdir.makepyfile(health_check_test)
103
104 initial = testdir.runpytest(script, "--verbose", "--strict-markers")
105
106 match = CONTAINS_SEED_INSTRUCTION.search("\n".join(initial.stdout.lines))
107 initial_output = "\n".join(initial.stdout.lines)
108
109 match = CONTAINS_SEED_INSTRUCTION.search(initial_output)
110 assert match is not None
111
112 rerun = testdir.runpytest(script, "--verbose", "--strict-markers", match.group(0))
113 rerun_output = "\n".join(rerun.stdout.lines)
114
115 assert "FailedHealthCheck" in rerun_output
116 assert "--hypothesis-seed" not in rerun_output
117
118 rerun2 = testdir.runpytest(
119 script, "--verbose", "--strict-markers", "--hypothesis-seed=10"
120 )
121 rerun2_output = "\n".join(rerun2.stdout.lines)
122 assert "FailedHealthCheck" not in rerun2_output