Debian Codegraph @

hypothesis-python/tests/cover/test_setup_teardown.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 HealthCheck, assume, given, settings
14 from hypothesis.strategies import integers, text
15
16
17 class HasSetup:
18 def setup_example(self):
19 self.setups = getattr(self, "setups", 0)
20 self.setups += 1
21
22
23 class HasTeardown:
24 def teardown_example(self, ex):
25 self.teardowns = getattr(self, "teardowns", 0)
26 self.teardowns += 1
27
28
29 class SomeGivens:
30 @given(integers())
31 @settings(suppress_health_check=[HealthCheck.differing_executors])
32 def give_me_an_int(self, x):
33 pass
34
35 @given(text())
36 def give_me_a_string(self, x):
37 pass
38
39 @given(integers())
40 @settings(max_examples=1000)
41 def give_me_a_positive_int(self, x):
42 assert x >= 0
43
44 @given(integers().map(lambda x: x.nope))
45 def fail_in_reify(self, x):
46 pass
47
48 @given(integers())
49 @settings(suppress_health_check=[HealthCheck.filter_too_much])
50 def assume_some_stuff(self, x):
51 assume(x > 0)
52
53 @given(integers().filter(lambda x: x > 0))
54 def assume_in_reify(self, x):
55 pass
56
57
58 class HasSetupAndTeardown(HasSetup, HasTeardown, SomeGivens):
59 pass
60
61
62 def test_calls_setup_and_teardown_on_self_as_first_argument():
63 x = HasSetupAndTeardown()
64 x.give_me_an_int()
65 x.give_me_a_string()
66 assert x.setups > 0
67 assert x.teardowns == x.setups
68
69
70 def test_calls_setup_and_teardown_on_self_unbound():
71 x = HasSetupAndTeardown()
72 HasSetupAndTeardown.give_me_an_int(x)
73 assert x.setups > 0
74 assert x.teardowns == x.setups
75
76
77 def test_calls_setup_and_teardown_on_failure():
78 x = HasSetupAndTeardown()
79 with pytest.raises(AssertionError):
80 x.give_me_a_positive_int()
81 assert x.setups > 0
82 assert x.teardowns == x.setups
83
84
85 def test_still_tears_down_on_error_in_generation():
86 x = HasSetupAndTeardown()
87 with pytest.raises(AttributeError):
88 x.fail_in_reify()
89 assert x.setups > 0
90 assert x.teardowns == x.setups
91
92
93 def test_still_tears_down_on_failed_assume():
94 x = HasSetupAndTeardown()
95 x.assume_some_stuff()
96 assert x.setups > 0
97 assert x.teardowns == x.setups
98
99
100 def test_still_tears_down_on_failed_assume_in_reify():
101 x = HasSetupAndTeardown()
102 x.assume_in_reify()
103 assert x.setups > 0
104 assert x.teardowns == x.setups
105
106
107 def test_sets_up_without_teardown():
108 class Foo(HasSetup, SomeGivens):
109 pass
110
111 x = Foo()
112 x.give_me_an_int()
113 assert x.setups > 0
114 assert not hasattr(x, "teardowns")
115
116
117 def test_tears_down_without_setup():
118 class Foo(HasTeardown, SomeGivens):
119 pass
120
121 x = Foo()
122 x.give_me_an_int()
123 assert x.teardowns > 0
124 assert not hasattr(x, "setups")