Debian Codegraph @

testing/test_error_diffs.py

raw
1 """
2 Tests and examples for correct "+/-" usage in error diffs.
3
4 See https://github.com/pytest-dev/pytest/issues/3333 for details.
5
6 """
7
8 from __future__ import annotations
9
10 from _pytest.pytester import Pytester
11 import pytest
12
13
14 TESTCASES = [
15 pytest.param(
16 """
17 def test_this():
18 result = [1, 4, 3]
19 expected = [1, 2, 3]
20 assert result == expected
21 """,
22 """
23 > assert result == expected
24 E assert [1, 4, 3] == [1, 2, 3]
25 E At index 1 diff: 4 != 2
26 E Full diff:
27 E [
28 E 1,
29 E - 2,
30 E ? ^
31 E + 4,
32 E ? ^
33 E 3,
34 E ]
35 """,
36 id="Compare lists, one item differs",
37 ),
38 pytest.param(
39 """
40 def test_this():
41 result = [1, 2, 3]
42 expected = [1, 2]
43 assert result == expected
44 """,
45 """
46 > assert result == expected
47 E assert [1, 2, 3] == [1, 2]
48 E Left contains one more item: 3
49 E Full diff:
50 E [
51 E 1,
52 E 2,
53 E + 3,
54 E ]
55 """,
56 id="Compare lists, one extra item",
57 ),
58 pytest.param(
59 """
60 def test_this():
61 result = [1, 3]
62 expected = [1, 2, 3]
63 assert result == expected
64 """,
65 """
66 > assert result == expected
67 E assert [1, 3] == [1, 2, 3]
68 E At index 1 diff: 3 != 2
69 E Right contains one more item: 3
70 E Full diff:
71 E [
72 E 1,
73 E - 2,
74 E 3,
75 E ]
76 """,
77 id="Compare lists, one item missing",
78 ),
79 pytest.param(
80 """
81 def test_this():
82 result = (1, 4, 3)
83 expected = (1, 2, 3)
84 assert result == expected
85 """,
86 """
87 > assert result == expected
88 E assert (1, 4, 3) == (1, 2, 3)
89 E At index 1 diff: 4 != 2
90 E Full diff:
91 E (
92 E 1,
93 E - 2,
94 E ? ^
95 E + 4,
96 E ? ^
97 E 3,
98 E )
99 """,
100 id="Compare tuples",
101 ),
102 pytest.param(
103 """
104 def test_this():
105 result = {1, 3, 4}
106 expected = {1, 2, 3}
107 assert result == expected
108 """,
109 """
110 > assert result == expected
111 E assert {1, 3, 4} == {1, 2, 3}
112 E Extra items in the left set:
113 E 4
114 E Extra items in the right set:
115 E 2
116 E Full diff:
117 E {
118 E 1,
119 E - 2,
120 E 3,
121 E + 4,
122 E }
123 """,
124 id="Compare sets",
125 ),
126 pytest.param(
127 """
128 def test_this():
129 result = {1: 'spam', 3: 'eggs'}
130 expected = {1: 'spam', 2: 'eggs'}
131 assert result == expected
132 """,
133 """
134 > assert result == expected
135 E AssertionError: assert {1: 'spam', 3: 'eggs'} == {1: 'spam', 2: 'eggs'}
136 E Common items:
137 E {1: 'spam'}
138 E Left contains 1 more item:
139 E {3: 'eggs'}
140 E Right contains 1 more item:
141 E {2: 'eggs'}
142 E Full diff:
143 E {
144 E 1: 'spam',
145 E - 2: 'eggs',
146 E ? ^
147 E + 3: 'eggs',
148 E ? ^
149 E }
150 """,
151 id="Compare dicts with differing keys",
152 ),
153 pytest.param(
154 """
155 def test_this():
156 result = {1: 'spam', 2: 'eggs'}
157 expected = {1: 'spam', 2: 'bacon'}
158 assert result == expected
159 """,
160 """
161 > assert result == expected
162 E AssertionError: assert {1: 'spam', 2: 'eggs'} == {1: 'spam', 2: 'bacon'}
163 E Common items:
164 E {1: 'spam'}
165 E Differing items:
166 E {2: 'eggs'} != {2: 'bacon'}
167 E Full diff:
168 E {
169 E 1: 'spam',
170 E - 2: 'bacon',
171 E + 2: 'eggs',
172 E }
173 """,
174 id="Compare dicts with differing values",
175 ),
176 pytest.param(
177 """
178 def test_this():
179 result = {1: 'spam', 2: 'eggs'}
180 expected = {1: 'spam', 3: 'bacon'}
181 assert result == expected
182 """,
183 """
184 > assert result == expected
185 E AssertionError: assert {1: 'spam', 2: 'eggs'} == {1: 'spam', 3: 'bacon'}
186 E Common items:
187 E {1: 'spam'}
188 E Left contains 1 more item:
189 E {2: 'eggs'}
190 E Right contains 1 more item:
191 E {3: 'bacon'}
192 E Full diff:
193 E {
194 E 1: 'spam',
195 E - 3: 'bacon',
196 E + 2: 'eggs',
197 E }
198 """,
199 id="Compare dicts with differing items",
200 ),
201 pytest.param(
202 """
203 def test_this():
204 result = "spmaeggs"
205 expected = "spameggs"
206 assert result == expected
207 """,
208 """
209 > assert result == expected
210 E AssertionError: assert 'spmaeggs' == 'spameggs'
211 E - spameggs
212 E ? -
213 E + spmaeggs
214 E ? +
215 """,
216 id="Compare strings",
217 ),
218 pytest.param(
219 """
220 def test_this():
221 result = "spam bacon eggs"
222 assert "bacon" not in result
223 """,
224 """
225 > assert "bacon" not in result
226 E AssertionError: assert 'bacon' not in 'spam bacon eggs'
227 E 'bacon' is contained here:
228 E spam bacon eggs
229 E ? +++++
230 """,
231 id='Test "not in" string',
232 ),
233 pytest.param(
234 """
235 from dataclasses import dataclass
236
237 @dataclass
238 class A:
239 a: int
240 b: str
241
242 def test_this():
243 result = A(1, 'spam')
244 expected = A(2, 'spam')
245 assert result == expected
246 """,
247 """
248 > assert result == expected
249 E AssertionError: assert A(a=1, b='spam') == A(a=2, b='spam')
250 E Matching attributes:
251 E ['b']
252 E Differing attributes:
253 E ['a']
254 E Drill down into differing attribute a:
255 E a: 1 != 2
256 """,
257 id="Compare data classes",
258 ),
259 pytest.param(
260 """
261 import attr
262
263 @attr.s(auto_attribs=True)
264 class A:
265 a: int
266 b: str
267
268 def test_this():
269 result = A(1, 'spam')
270 expected = A(1, 'eggs')
271 assert result == expected
272 """,
273 """
274 > assert result == expected
275 E AssertionError: assert A(a=1, b='spam') == A(a=1, b='eggs')
276 E Matching attributes:
277 E ['a']
278 E Differing attributes:
279 E ['b']
280 E Drill down into differing attribute b:
281 E b: 'spam' != 'eggs'
282 E - eggs
283 E + spam
284 """,
285 id="Compare attrs classes",
286 ),
287 ]
288
289
290 @pytest.mark.parametrize("code, expected", TESTCASES)
291 def test_error_diff(code: str, expected: str, pytester: Pytester) -> None:
292 expected_lines = [line.lstrip() for line in expected.splitlines()]
293 p = pytester.makepyfile(code)
294 result = pytester.runpytest(p, "-vv")
295 result.stdout.fnmatch_lines(expected_lines)
296 assert result.ret == 1