Debian Codegraph @

hypothesis-python/tests/cover/test_phases.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 pytest
12
13 from hypothesis import Phase, example, given, settings, strategies as st
14 from hypothesis.database import ExampleDatabase, InMemoryExampleDatabase
15 from hypothesis.errors import InvalidArgument
16
17 from tests.common.utils import skipif_emscripten
18
19
20 @example(11)
21 @settings(phases=(Phase.explicit,))
22 @given(st.integers())
23 def test_only_runs_explicit_examples(i):
24 assert i == 11
25
26
27 @example("hello world")
28 @settings(phases=(Phase.reuse, Phase.generate, Phase.shrink))
29 @given(st.booleans())
30 def test_does_not_use_explicit_examples(i):
31 assert isinstance(i, bool)
32
33
34 @skipif_emscripten # TODO: actually Pytest 9.0, remove this workaround ASAP
35 @settings(phases=(Phase.reuse, Phase.shrink), database=InMemoryExampleDatabase())
36 @given(st.booleans())
37 def test_this_would_fail_if_you_ran_it(b):
38 raise AssertionError
39
40
41 @pytest.mark.parametrize(
42 "arg,expected",
43 [
44 (tuple(Phase)[::-1], tuple(Phase)),
45 ([Phase.explicit, Phase.explicit], (Phase.explicit,)),
46 ],
47 )
48 def test_sorts_and_dedupes_phases(arg, expected):
49 assert settings(phases=arg).phases == expected
50
51
52 def test_phases_default_to_all():
53 assert settings().phases == tuple(Phase)
54
55
56 def test_does_not_reuse_saved_examples_if_reuse_not_in_phases():
57 class BadDatabase(ExampleDatabase):
58 def save(self, key, value):
59 pass
60
61 def delete(self, key, value):
62 pass
63
64 def fetch(self, key):
65 raise ValueError
66
67 def close(self):
68 pass
69
70 @settings(database=BadDatabase(), phases=(Phase.generate,))
71 @given(st.integers())
72 def test_usage(i):
73 pass
74
75 test_usage()
76
77
78 def test_will_save_when_reuse_not_in_phases():
79 database = InMemoryExampleDatabase()
80
81 assert not database.data
82
83 @settings(database=database, phases=(Phase.generate,))
84 @given(st.integers())
85 def test_usage(i):
86 raise ValueError
87
88 with pytest.raises(ValueError):
89 test_usage()
90
91 (saved,) = (v for k, v in database.data.items() if b"pareto" not in k)
92 assert len(saved) == 1
93
94
95 def test_rejects_non_phases():
96 with pytest.raises(InvalidArgument):
97 settings(phases=["cabbage"])