-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytest-parser.test.js
More file actions
327 lines (253 loc) · 14.1 KB
/
Copy pathpytest-parser.test.js
File metadata and controls
327 lines (253 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const test = require('node:test');
const assert = require('node:assert/strict');
const { parsePytestOutput, extractPytestShortSummaryTarget } = require('./pytest-parser');
test('extracts the real technical message for pytest attribute failures', () => {
const stdout = `
============================= test session starts ==============================
platform darwin -- Python 3.9.10, pytest-7.4.0, pluggy-1.2.0
collected 1 item
test_program.py F [100%]
=================================== FAILURES ===================================
______________________________ test_hat_variables ______________________________
def test_hat_variables():
> assert program.numberOfHats == 9
E AttributeError: module 'program' has no attribute 'numberOfHats'
/tmp/test_program.py:4: AttributeError
=========================== short test summary info ============================
FAILED test_program.py::test_hat_variables
============================== 1 failed in 0.02s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failed, 1);
assert.equal(parsed.failure_details.length, 1);
assert.equal(parsed.failure_details[0].test_case, 'test_hat_variables');
assert.equal(
parsed.failure_details[0].error_message,
"AttributeError: module 'program' has no attribute 'numberOfHats'"
);
assert.match(parsed.failure_details[0].rawout, /FAILED test_program\.py::test_hat_variables/);
});
test('normalizes pytest short summary targets to the test name', () => {
const stdout = `
FAILED ../../../../../../../var/folders/example/test_program.py::test_hat_variables
`.trim();
assert.equal(
extractPytestShortSummaryTarget(stdout, 'FAILED'),
'test_hat_variables'
);
});
test('preserves custom assertion messages for hasattr-style lesson failures', () => {
const stdout = `
============================= test session starts ==============================
platform linux -- Python 3.11.12, pytest-9.1.1, pluggy-1.6.0
collected 1 item
test_program.py F [100%]
=================================== FAILURES ===================================
______________________________ test_hat_variables ______________________________
def test_hat_variables():
> assert hasattr(program,"HatName"), "There should be a variable named exactly HatName"
E AssertionError: There should be a variable named exactly HatName
E assert False
E + where False = hasattr(program, 'HatName')
test_program.py:8: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_hat_variables - AssertionError: There should be ...
============================== 1 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failed, 1);
assert.equal(parsed.failure_details.length, 1);
assert.equal(
parsed.failure_details[0].error_message,
'AssertionError: There should be a variable named exactly HatName'
);
assert.equal(parsed.failure_details[0].expected, '');
assert.equal(parsed.failure_details[0].received, '');
});
test('preserves custom assertion messages for printed dataframe regression cases', () => {
const stdout = `
============================= test session starts ==============================
platform linux -- Python 3.11.12, pytest-9.1.1, pluggy-1.6.0
collected 1 item
test_program.py F [100%]
=================================== FAILURES ===================================
____________________________ test_describe_printed _____________________________
def test_describe_printed(capsys):
> assert part in out, f"Expected '{part}' in output, got:\\n{out}"
E AssertionError: Expected 'count' in output, got:
E 0 1 2 3
test_program.py:12: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_describe_printed - AssertionError: Expected 'cou...
============================== 1 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failed, 1);
assert.equal(parsed.failure_details.length, 1);
assert.equal(parsed.failure_details[0].test_case, 'test_describe_printed');
assert.equal(
parsed.failure_details[0].error_message,
"AssertionError: Expected 'count' in output, got:"
);
});
test('extracts expected and received values for numeric equality assertions', () => {
const stdout = `
============================= test session starts ==============================
platform linux -- Python 3.11.12, pytest-9.1.1, pluggy-1.6.0
collected 1 item
test_program.py F [100%]
=================================== FAILURES ===================================
______________________________ test_hat_variables ______________________________
def test_hat_variables():
> assert program.NumberOfHats == 9
E assert 8 == 9
test_program.py:12: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_hat_variables - assert 8 == 9
============================== 1 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failure_details[0].error_message, 'Assertion failed: assert program.NumberOfHats == 9');
assert.equal(parsed.failure_details[0].received, '8');
assert.equal(parsed.failure_details[0].expected, '9');
});
test('extracts expected and received values for string equality assertions', () => {
const stdout = `
============================= test session starts ==============================
platform linux -- Python 3.11.12, pytest-9.1.1, pluggy-1.6.0
collected 1 item
test_program.py F [100%]
=================================== FAILURES ===================================
______________________________ test_hat_variables ______________________________
def test_hat_variables():
> assert program.HatName == "Veracruz"
E assert "Oaxaca" == "Veracruz"
test_program.py:10: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_hat_variables - assert "Oaxaca" == "Veracruz"
============================== 1 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failure_details[0].error_message, 'Assertion failed: assert program.HatName == "Veracruz"');
assert.equal(parsed.failure_details[0].received, '"Oaxaca"');
assert.equal(parsed.failure_details[0].expected, '"Veracruz"');
});
test('ignores comparison operators inside quoted assertion values', () => {
const comparisons = [
["'This is wrong' == 'This is right'", "'This is wrong'", "'This is right'"],
["'value is not ready' == 'value is not done'", "'value is not ready'", "'value is not done'"],
["'left == side' == 'right == side'", "'left == side'", "'right == side'"],
];
for (const [expression, received, expected] of comparisons) {
const stdout = `
=================================== FAILURES ===================================
______________________________ test_string_value _______________________________
> assert actual == expected
E assert ${expression}
test_program.py:10: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_string_value - assert ${expression}
============================== 1 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failure_details[0].received, received);
assert.equal(parsed.failure_details[0].expected, expected);
}
});
test('extracts expected and received values for float equality assertions', () => {
const stdout = `
============================= test session starts ==============================
platform linux -- Python 3.11.12, pytest-9.1.1, pluggy-1.6.0
collected 1 item
test_program.py F [100%]
=================================== FAILURES ===================================
______________________________ test_hat_variables ______________________________
def test_hat_variables():
> assert program.CostOfHats == 278.91
E assert 199.99 == 278.91
test_program.py:14: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_hat_variables - assert 199.99 == 278.91
============================== 1 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failure_details[0].error_message, 'Assertion failed: assert program.CostOfHats == 278.91');
assert.equal(parsed.failure_details[0].received, '199.99');
assert.equal(parsed.failure_details[0].expected, '278.91');
});
test('extracts expected and received values for boolean identity assertions', () => {
const stdout = `
============================= test session starts ==============================
platform linux -- Python 3.11.12, pytest-9.1.1, pluggy-1.6.0
collected 1 item
test_program.py F [100%]
=================================== FAILURES ===================================
______________________________ test_hat_variables ______________________________
def test_hat_variables():
> assert program.WearingHat is False
E assert True is False
E + where True = program.WearingHat
test_program.py:16: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_hat_variables - assert True is False
============================== 1 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failure_details[0].error_message, 'Assertion failed: assert program.WearingHat is False');
assert.equal(parsed.failure_details[0].received, 'True');
assert.equal(parsed.failure_details[0].expected, 'False');
});
test('extracts complete captured output values from pytest verbose assertion failures', () => {
// Captured from pytest 8.2.0 with `pytest -vv`; -vv prevents pytest from
// abbreviating the compared output with ellipses and a truncation notice.
const stdout = `
============================= test session starts ==============================
collected 1 item
test_program.py::test_weekly_todo FAILED [100%]
=================================== FAILURES ===================================
_______________________________ test_weekly_todo _______________________________
> assert capsys.readouterr().out == expected_output
E AssertionError: assert 'Weekly to-do list for the 7 days of the weeke:\\nMon: Laundry\\nTues: Groceries\\nWed Gym\\nThur: Study\\nFri: Work\\nSat: Hike\\nSun: Rest\\n' == 'Weekly to-do list for the 7 days of the week:\\nMon: Laundry\\nTues: Groceries\\nWed Gym\\nThur: Study\\nFri: Work\\nSat: Hike\\nSun: Rest\\n'
E
E - Weekly to-do list for the 7 days of the week:
E + Weekly to-do list for the 7 days of the weeke:
E ? +
E Mon: Laundry
test_program.py:29: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_weekly_todo - AssertionError: assert 'Weekly to-do list for the 7 days of the weeke:\\nMon: Laundry\\nTues: Groceries\\nWed Gym\\nThur: Study\\nFri: Work\\nSat: Hike\\nSun: Rest\\n' == 'Weekly to-do list for the 7 days of the week:\\nMon: Laundry\\nTues: Groceries\\nWed Gym\\nThur: Study\\nFri: Work\\nSat: Hike\\nSun: Rest\\n'
============================== 1 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failure_details.length, 1);
assert.equal(parsed.failure_details[0].received, "'Weekly to-do list for the 7 days of the weeke:\\nMon: Laundry\\nTues: Groceries\\nWed Gym\\nThur: Study\\nFri: Work\\nSat: Hike\\nSun: Rest\\n'");
assert.equal(parsed.failure_details[0].expected, "'Weekly to-do list for the 7 days of the week:\\nMon: Laundry\\nTues: Groceries\\nWed Gym\\nThur: Study\\nFri: Work\\nSat: Hike\\nSun: Rest\\n'");
});
test('keeps a distinct scoped diagnostic for each pytest failure', () => {
const stdout = `
============================= test session starts ==============================
collected 2 items
test_program.py::test_hats FAILED [ 50%]
test_program.py::TestClothes::test_shirt[color-red] FAILED [100%]
=================================== FAILURES ===================================
__________________________________ test_hats ___________________________________
> assert program.NumberOfHats == 9
E assert 8 == 9
test_program.py:4: AssertionError
_____________________ TestClothes.test_shirt[color-red] ______________________
> assert program.ShirtColor == "red"
E assert "blue" == "red"
test_program.py:8: AssertionError
=========================== short test summary info ============================
FAILED test_program.py::test_hats - assert 8 == 9
FAILED test_program.py::TestClothes::test_shirt[color-red] - assert "blue" == "red"
============================== 2 failed in 0.01s ===============================
`.trim();
const parsed = parsePytestOutput(stdout, '', 1);
assert.equal(parsed.failure_details.length, 2);
assert.match(parsed.failure_details[0].diagnostic, /program\.NumberOfHats/);
assert.doesNotMatch(parsed.failure_details[0].diagnostic, /program\.ShirtColor/);
assert.match(parsed.failure_details[1].diagnostic, /program\.ShirtColor/);
assert.doesNotMatch(parsed.failure_details[1].diagnostic, /program\.NumberOfHats/);
assert.equal(parsed.failure_details[0].rawout, parsed.failure_details[1].rawout);
});