1717from hypothesis .strategies ._internal .lazy import unwrap_strategies
1818
1919
20+ def _get_output (test_fn ):
21+ with pytest .raises (AssertionError ) as err :
22+ test_fn ()
23+ return "\n " .join (err .value .__notes__ ).strip ()
24+
25+
2026def test_includes_non_default_args_in_repr ():
2127 assert repr (st .integers ()) == "integers()"
2228 assert repr (st .integers (min_value = 1 )) == "integers(min_value=1)"
@@ -79,41 +85,24 @@ class Bar(Foo):
7985 pass
8086
8187
82- def test_reprs_as_created ():
88+ def test_reprs_as_created (snapshot ):
8389 @given (foo = st .builds (Foo ), bar = st .from_type (Bar ), baz = st .none ().map (Foo ))
8490 @settings (print_blob = False , max_examples = 10_000 , derandomize = True )
8591 def inner (foo , bar , baz ):
8692 assert baz .x is None
8793 assert foo .x <= 0 or bar .x >= 0
8894
89- with pytest .raises (AssertionError ) as err :
90- inner ()
91- expected = """
92- Falsifying example: inner(
93- foo=Foo(x=1),
94- bar=Bar(x=-1),
95- baz=Foo(None),
96- )
97- """
98- assert "\n " .join (err .value .__notes__ ).strip () == expected .strip ()
95+ assert _get_output (inner ) == snapshot
9996
10097
101- def test_reprs_as_created_interactive ():
98+ def test_reprs_as_created_interactive (snapshot ):
10299 @given (st .data ())
103100 @settings (print_blob = False , max_examples = 10_000 )
104101 def inner (data ):
105102 bar = data .draw (st .builds (Bar , st .just (10 )))
106103 assert not bar .x
107104
108- with pytest .raises (AssertionError ) as err :
109- inner ()
110- expected = """
111- Falsifying example: inner(
112- data=data(...),
113- )
114- Draw 1: Bar(10)
115- """
116- assert "\n " .join (err .value .__notes__ ).strip () == expected .strip ()
105+ assert _get_output (inner ) == snapshot
117106
118107
119108CONSTANT_FOO = Foo (None )
@@ -143,7 +132,7 @@ def inner(a, b):
143132 assert re .fullmatch (expected_re , got ), got
144133
145134
146- def test_reprs_as_created_consistent_calls_despite_indentation ():
135+ def test_reprs_as_created_consistent_calls_despite_indentation (snapshot ):
147136 aas = "a" * 60
148137 strat = st .builds (some_foo , st .just (aas ))
149138
@@ -154,19 +143,7 @@ def test_reprs_as_created_consistent_calls_despite_indentation():
154143 def inner (a , b ):
155144 assert a == b
156145
157- with pytest .raises (AssertionError ) as err :
158- inner ()
159- expected = f"""
160- Falsifying example: inner(
161- a=some_foo({ aas !r} ),
162- b=Bar(
163- some_foo(
164- { aas !r} ,
165- ),
166- ),
167- )
168- """
169- assert "\n " .join (err .value .__notes__ ).strip () == expected .strip ()
146+ assert _get_output (inner ) == snapshot
170147
171148
172149@pytest .mark .parametrize (
@@ -199,33 +176,19 @@ def test_characters_repr(strategy, expected_repr):
199176 assert repr (unwrap_strategies (strategy )) == expected_repr
200177
201178
202- def test_map_to_str_prints_as_repr ():
179+ def test_map_to_str_prints_as_repr (snapshot ):
203180 @given (s = st .integers ().map (str ))
204181 @settings (phases = [Phase .generate , Phase .shrink ], print_blob = False )
205182 def inner (s ):
206183 raise AssertionError
207184
208- with pytest .raises (AssertionError ) as err :
209- inner ()
210- expected = """
211- Falsifying example: inner(
212- s='0',
213- )
214- """
215- assert "\n " .join (err .value .__notes__ ).strip () == expected .strip ()
185+ assert _get_output (inner ) == snapshot
216186
217187
218- def test_map_to_bytes_prints_as_repr ():
188+ def test_map_to_bytes_prints_as_repr (snapshot ):
219189 @given (b = st .binary ().map (lambda b : hashlib .sha256 (b ).digest ()))
220190 @settings (phases = [Phase .generate , Phase .shrink ], print_blob = False )
221191 def inner (b ):
222192 raise AssertionError
223193
224- with pytest .raises (AssertionError ) as err :
225- inner ()
226- expected = f"""
227- Falsifying example: inner(
228- b={ hashlib .sha256 (b"" ).digest ()!r} ,
229- )
230- """
231- assert "\n " .join (err .value .__notes__ ).strip () == expected .strip ()
194+ assert _get_output (inner ) == snapshot
0 commit comments