Debian Codegraph @

doc/en/example/assertion/failure_demo.py

raw
1 from __future__ import annotations
2
3 import pytest
4 from pytest import raises
5
6
7 def otherfunc(a, b):
8 assert a == b
9
10
11 def somefunc(x, y):
12 otherfunc(x, y)
13
14
15 def otherfunc_multi(a, b):
16 assert a == b
17
18
19 @pytest.mark.parametrize("param1, param2", [(3, 6)])
20 def test_generative(param1, param2):
21 assert param1 * 2 < param2
22
23
24 class TestFailing:
25 def test_simple(self):
26 def f():
27 return 42
28
29 def g():
30 return 43
31
32 assert f() == g()
33
34 def test_simple_multiline(self):
35 otherfunc_multi(42, 6 * 9)
36
37 def test_not(self):
38 def f():
39 return 42
40
41 assert not f()
42
43
44 class TestSpecialisedExplanations:
45 def test_eq_text(self):
46 assert "spam" == "eggs"
47
48 def test_eq_similar_text(self):
49 assert "foo 1 bar" == "foo 2 bar"
50
51 def test_eq_multiline_text(self):
52 assert "foo\nspam\nbar" == "foo\neggs\nbar"
53
54 def test_eq_long_text(self):
55 a = "1" * 100 + "a" + "2" * 100
56 b = "1" * 100 + "b" + "2" * 100
57 assert a == b
58
59 def test_eq_long_text_multiline(self):
60 a = "1\n" * 100 + "a" + "2\n" * 100
61 b = "1\n" * 100 + "b" + "2\n" * 100
62 assert a == b
63
64 def test_eq_list(self):
65 assert [0, 1, 2] == [0, 1, 3]
66
67 def test_eq_list_long(self):
68 a = [0] * 100 + [1] + [3] * 100
69 b = [0] * 100 + [2] + [3] * 100
70 assert a == b
71
72 def test_eq_dict(self):
73 assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0}
74
75 def test_eq_set(self):
76 assert {0, 10, 11, 12} == {0, 20, 21}
77
78 def test_eq_longer_list(self):
79 assert [1, 2] == [1, 2, 3]
80
81 def test_in_list(self):
82 assert 1 in [0, 2, 3, 4, 5]
83
84 def test_not_in_text_multiline(self):
85 text = "some multiline\ntext\nwhich\nincludes foo\nand a\ntail"
86 assert "foo" not in text
87
88 def test_not_in_text_single(self):
89 text = "single foo line"
90 assert "foo" not in text
91
92 def test_not_in_text_single_long(self):
93 text = "head " * 50 + "foo " + "tail " * 20
94 assert "foo" not in text
95
96 def test_not_in_text_single_long_term(self):
97 text = "head " * 50 + "f" * 70 + "tail " * 20
98 assert "f" * 70 not in text
99
100 def test_eq_dataclass(self):
101 from dataclasses import dataclass
102
103 @dataclass
104 class Foo:
105 a: int
106 b: str
107
108 left = Foo(1, "b")
109 right = Foo(1, "c")
110 assert left == right
111
112 def test_eq_attrs(self):
113 import attr
114
115 @attr.s
116 class Foo:
117 a = attr.ib()
118 b = attr.ib()
119
120 left = Foo(1, "b")
121 right = Foo(1, "c")
122 assert left == right
123
124
125 def test_attribute():
126 class Foo:
127 b = 1
128
129 i = Foo()
130 assert i.b == 2
131
132
133 def test_attribute_instance():
134 class Foo:
135 b = 1
136
137 assert Foo().b == 2
138
139
140 def test_attribute_failure():
141 class Foo:
142 def _get_b(self):
143 raise Exception("Failed to get attrib")
144
145 b = property(_get_b)
146
147 i = Foo()
148 assert i.b == 2
149
150
151 def test_attribute_multiple():
152 class Foo:
153 b = 1
154
155 class Bar:
156 b = 2
157
158 assert Foo().b == Bar().b
159
160
161 def globf(x):
162 return x + 1
163
164
165 class TestRaises:
166 def test_raises(self):
167 s = "qwe"
168 raises(TypeError, int, s)
169
170 def test_raises_doesnt(self):
171 raises(OSError, int, "3")
172
173 def test_raise(self):
174 raise ValueError("demo error")
175
176 def test_tupleerror(self):
177 a, b = [1] # noqa: F841
178
179 def test_reinterpret_fails_with_print_for_the_fun_of_it(self):
180 items = [1, 2, 3]
181 print(f"items is {items!r}")
182 a, b = items.pop()
183
184 def test_some_error(self):
185 if namenotexi: # noqa: F821
186 pass
187
188 def func1(self):
189 assert 41 == 42
190
191
192 # thanks to Matthew Scott for this test
193 def test_dynamic_compile_shows_nicely():
194 import importlib.util
195 import sys
196
197 src = "def foo():\n assert 1 == 0\n"
198 name = "abc-123"
199 spec = importlib.util.spec_from_loader(name, loader=None)
200 module = importlib.util.module_from_spec(spec)
201 code = compile(src, name, "exec")
202 exec(code, module.__dict__)
203 sys.modules[name] = module
204 module.foo()
205
206
207 class TestMoreErrors:
208 def test_complex_error(self):
209 def f():
210 return 44
211
212 def g():
213 return 43
214
215 somefunc(f(), g())
216
217 def test_z1_unpack_error(self):
218 items = []
219 a, b = items
220
221 def test_z2_type_error(self):
222 items = 3
223 a, b = items
224
225 def test_startswith(self):
226 s = "123"
227 g = "456"
228 assert s.startswith(g)
229
230 def test_startswith_nested(self):
231 def f():
232 return "123"
233
234 def g():
235 return "456"
236
237 assert f().startswith(g())
238
239 def test_global_func(self):
240 assert isinstance(globf(42), float)
241
242 def test_instance(self):
243 self.x = 6 * 7
244 assert self.x != 42
245
246 def test_compare(self):
247 assert globf(10) < 5
248
249 def test_try_finally(self):
250 x = 1
251 try:
252 assert x == 0
253 finally:
254 x = 0
255
256
257 class TestCustomAssertMsg:
258 def test_single_line(self):
259 class A:
260 a = 1
261
262 b = 2
263 assert A.a == b, "A.a appears not to be b"
264
265 def test_multiline(self):
266 class A:
267 a = 1
268
269 b = 2
270 assert A.a == b, (
271 "A.a appears not to be b\nor does not appear to be b\none of those"
272 )
273
274 def test_custom_repr(self):
275 class JSON:
276 a = 1
277
278 def __repr__(self):
279 return "This is JSON\n{\n 'foo': 'bar'\n}"
280
281 a = JSON()
282 b = 2
283 assert a.a == b, a