generated from tophat/new-project-kit
-
Notifications
You must be signed in to change notification settings - Fork 34
/
test_integration_default.py
398 lines (350 loc) · 13.8 KB
/
test_integration_default.py
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from pathlib import Path
import pytest
from .utils import clean_output
@pytest.fixture
def collection(testdir):
tests = {
"test_collected": (
"""
import pytest
@pytest.mark.parametrize("actual", [1, 2, 3])
def test_collected(snapshot, actual):
assert snapshot == actual
"""
),
"test_not_collected": (
"""
def test_collected1(snapshot):
assert snapshot == "hello"
"""
),
}
testdir.makepyfile(**tests)
result = testdir.runpytest("-v", "--snapshot-update")
result_stdout = clean_output(result.stdout.str())
assert "4 snapshots generated" in result_stdout
testdir.makefile(".ambr", **{"__snapshots__/other_snapfile": ""})
return testdir
def test_unused_snapshots_ignored_if_not_targeted_using_dash_m(collection):
updated_tests = {
"test_collected": (
"""
import pytest
@pytest.mark.parametrize("actual", [1, "2"])
def test_collected(snapshot, actual):
assert snapshot == actual
"""
),
}
collection.makepyfile(**updated_tests)
result = collection.runpytest("-v", "--snapshot-update", "-m", "parametrize")
result_stdout = clean_output(result.stdout.str())
assert "1 snapshot passed" in result_stdout
assert "1 snapshot updated" in result_stdout
assert "1 unused snapshot deleted" in result_stdout
snapshot_path = [collection.tmpdir, "__snapshots__"]
assert Path(*snapshot_path).joinpath("test_not_collected.ambr").exists()
assert Path(*snapshot_path).joinpath("other_snapfile.ambr").exists()
def test_unused_snapshots_ignored_if_not_targeted_using_dash_k(collection):
updated_tests = {
"test_collected": (
"""
import pytest
@pytest.mark.parametrize("actual", [1, "2"])
def test_collected(snapshot, actual):
assert snapshot == actual
"""
),
}
collection.makepyfile(**updated_tests)
result = collection.runpytest("-v", "--snapshot-update", "-k", "test_collected[")
result_stdout = clean_output(result.stdout.str())
assert "1 snapshot passed" in result_stdout
assert "1 snapshot updated" in result_stdout
assert "1 unused snapshot deleted" in result_stdout
snapshot_path = [collection.tmpdir, "__snapshots__"]
assert Path(*snapshot_path).joinpath("test_not_collected.ambr").exists()
assert Path(*snapshot_path).joinpath("other_snapfile.ambr").exists()
def test_unused_parameterized_ignored_if_not_targeted_using_dash_k(collection):
updated_tests = {
"test_collected": (
"""
import pytest
@pytest.mark.parametrize("actual", [1, 2])
def test_collected(snapshot, actual):
assert snapshot == actual
"""
),
}
collection.makepyfile(**updated_tests)
result = collection.runpytest("-v", "--snapshot-update", "-k", "test_collected[")
result_stdout = clean_output(result.stdout.str())
assert "2 snapshots passed" in result_stdout
assert "snapshot updated" not in result_stdout
assert "1 unused snapshot deleted" in result_stdout
snapshot_path = [collection.tmpdir, "__snapshots__"]
assert Path(*snapshot_path).joinpath("test_not_collected.ambr").exists()
assert Path(*snapshot_path).joinpath("other_snapfile.ambr").exists()
@pytest.fixture
def testcases():
return {
"inject": (
"""
def test_injection(snapshot):
assert snapshot is not None
"""
),
"used": (
"""
def test_used(snapshot):
assert snapshot == 'used'
"""
),
"unused": (
"""
def test_unused(snapshot):
assert snapshot == 'unused'
"""
),
"updated_1": (
"""
def test_updated_1(snapshot):
assert snapshot == ['this', 'will', 'be', 'updated']
"""
),
"updated_2": (
"""
def test_updated_2(snapshot):
assert ['this', 'will', 'be', 'updated'] == snapshot
"""
),
"updated_3": (
"""
def test_updated_3(snapshot):
assert snapshot == ['this', 'will', 'be', 'updated']
"""
),
"updated_4": (
"""
def test_updated_4(snapshot):
assert snapshot == "single line change"
"""
),
"updated_5": (
"""
def test_updated_5(snapshot):
assert snapshot == '''
multiple line changes
with some lines staying the same
intermittent changes that have to be ignore by the differ output
because when there are a lot of changes you only want to see changes
you do not want to see this line
or this line
\x1b[38;5;1mthis line should show up because it changes color\x1b[0m
'''
"""
),
}
@pytest.fixture
def testcases_updated(testcases):
updated_testcases = {
"updated_1": (
"""
def test_updated_1(snapshot):
assert snapshot == ['this', 'will', 'not', 'match']
"""
),
"updated_2": (
"""
def test_updated_2(snapshot):
assert ['this', 'will', 'fail'] == snapshot
"""
),
"updated_3": (
"""
def test_updated_3(snapshot):
assert snapshot == ['this', 'will', 'be', 'too', 'much']
"""
),
"updated_4": (
"""
def test_updated_4(snapshot):
assert snapshot == "sing line changeling"
"""
),
"updated_5": (
"""
def test_updated_5(snapshot):
assert snapshot == '''
multiple line changes
with some lines not staying the same
intermittent changes so unchanged lines have to be ignored by the differ
cause when there are a lot of changes you only want to see what changed
you do not want to see this line
or this line
\x1b[38;5;3mthis line should show up because it changes color\x1b[0m
and this line does not exist in the first one
'''
"""
),
}
return {**testcases, **updated_testcases}
def test_missing_snapshots(testdir, testcases):
testdir.makepyfile(test_file=testcases["used"])
result = testdir.runpytest("-v")
assert "Snapshot does not exist" in clean_output(result.stdout.str())
assert result.ret == 1
@pytest.fixture
def stubs(testdir, testcases):
pyfile_content = "\n\n".join(testcases.values())
testdir.makepyfile(test_file=pyfile_content)
filepath = Path(testdir.tmpdir, "__snapshots__", "test_file.ambr")
return testdir.runpytest("-v", "--snapshot-update"), testdir, testcases, filepath
def test_injected_fixture(stubs):
result = stubs[0]
result.stdout.fnmatch_lines(["*::test_injection PASSED*"])
assert result.ret == 0
def test_generated_snapshots(stubs):
result = stubs[0]
result_stdout = clean_output(result.stdout.str())
assert "7 snapshots generated" in result_stdout
assert "snapshots unused" not in result_stdout
assert result.ret == 0
def test_failing_snapshots_diff(stubs, testcases_updated, snapshot):
testdir = stubs[1]
testdir.makepyfile(test_file="\n\n".join(testcases_updated.values()))
result = testdir.runpytest("-vv")
result_stdout = clean_output(result.stdout.str())
start_index = result_stdout.find("==== FAILURES")
end_index = result_stdout.find("==== 5 failed")
result_stdout = "\n".join(
line
for line in result_stdout[start_index:end_index].splitlines()
if not line.startswith("____") and not line.startswith("====")
)
assert snapshot == result_stdout
assert result.ret == 1
def test_updated_snapshots(stubs, testcases_updated):
testdir = stubs[1]
testdir.makepyfile(test_file="\n\n".join(testcases_updated.values()))
result = testdir.runpytest("-v", "--snapshot-update")
result_stdout = clean_output(result.stdout.str())
assert "2 snapshots passed" in result_stdout
assert "5 snapshots updated" in result_stdout
assert result.ret == 0
def test_unused_snapshots(stubs):
_, testdir, tests, _ = stubs
testdir.makepyfile(test_file="\n\n".join(tests[k] for k in tests if k != "unused"))
result = testdir.runpytest("-v")
result_stdout = clean_output(result.stdout.str())
assert "snapshots generated" not in result_stdout
assert "6 snapshots passed" in result_stdout
assert "1 snapshot unused" in result_stdout
assert result.ret == 1
def test_unused_snapshots_warning(stubs):
_, testdir, tests, _ = stubs
testdir.makepyfile(test_file="\n\n".join(tests[k] for k in tests if k != "unused"))
result = testdir.runpytest("-v", "--snapshot-warn-unused")
result_stdout = clean_output(result.stdout.str())
assert "snapshots generated" not in result_stdout
assert "6 snapshots passed" in result_stdout
assert "1 snapshot unused" in result_stdout
assert result.ret == 0
def test_unused_snapshots_ignored_if_not_targeted_by_testnode_ids(stubs):
_, testdir, tests, snapshot_file = stubs
testdir.makepyfile(test_file="\n\n".join(tests[k] for k in tests if k != "unused"))
testdir.makefile(
".ambr", **{"__snapshots__/other_snapfile": ""},
)
test_content = (
"def test_life_always_finds_a_way(snapshot):\n\tassert snapshot == snapshot"
)
testdir.makefile(
".py", **{"test_life_always_finds_a_way": test_content},
)
testfile = Path(testdir.tmpdir, "test_life_always_finds_a_way.py")
result = testdir.runpytest(
f"{testfile}::test_life_always_finds_a_way", "-v", "--snapshot-update"
)
result_stdout = clean_output(result.stdout.str())
assert "1 snapshot generated" in result_stdout
assert result.ret == 0
assert Path(snapshot_file).exists()
assert Path("__snapshots__/other_snapfile.ambr").exists()
def test_unused_snapshots_ignored_if_not_targeted_by_module_testfiles(stubs):
_, testdir, tests, _ = stubs
testdir.makepyfile(test_file="\n\n".join(tests[k] for k in tests if k != "unused"))
testdir.makefile(
".ambr", **{"__snapshots__/other_snapfile": ""},
)
result = testdir.runpytest("-v", "--snapshot-update", "test_file.py")
result_stdout = clean_output(result.stdout.str())
assert "1 unused snapshot deleted" in result_stdout
assert result.ret == 0
assert Path("__snapshots__/other_snapfile.ambr").exists()
def test_unused_snapshots_cleaned_up_when_targeting_specific_testfiles(stubs):
_, testdir, tests, _ = stubs
testdir.makepyfile(
test_file=(
"""
def test_used(snapshot):
assert True
"""
)
)
testdir.makefile(
".ambr", **{"__snapshots__/other_snapfile": ""},
)
result = testdir.runpytest("-v", "--snapshot-update", "test_file.py")
result_stdout = clean_output(result.stdout.str())
assert "7 unused snapshots deleted" in result_stdout
assert result.ret == 0
assert Path("__snapshots__/other_snapfile.ambr").exists()
def test_removed_snapshots(stubs):
_, testdir, tests, filepath = stubs
assert Path(filepath).exists()
testdir.makepyfile(test_file="\n\n".join(tests[k] for k in tests if k != "unused"))
result = testdir.runpytest("-v", "--snapshot-update")
result_stdout = clean_output(result.stdout.str())
assert "snapshot unused" not in result_stdout
assert "1 unused snapshot deleted" in result_stdout
assert result.ret == 0
assert Path(filepath).exists()
def test_removed_snapshot_fossil(stubs):
_, testdir, tests, filepath = stubs
assert Path(filepath).exists()
testdir.makepyfile(test_file=tests["inject"])
result = testdir.runpytest("-v", "--snapshot-update")
result_stdout = clean_output(result.stdout.str())
assert "snapshots unused" not in result_stdout
assert "7 unused snapshots deleted" in result_stdout
assert result.ret == 0
assert not Path(filepath).exists()
def test_removed_empty_snapshot_fossil_only(stubs):
_, testdir, _, filepath = stubs
testdir.makefile(".ambr", **{"__snapshots__/test_empty": ""})
empty_filepath = Path(testdir.tmpdir, "__snapshots__/test_empty.ambr")
assert empty_filepath.exists()
result = testdir.runpytest("-v", "--snapshot-update")
result_stdout = clean_output(result.stdout.str())
assert str(Path(filepath).relative_to(Path.cwd())) not in result_stdout
assert "1 unused snapshot deleted" in result_stdout
assert "empty snapshot" in result_stdout
assert str(empty_filepath.relative_to(Path.cwd())) in result_stdout
assert result.ret == 0
assert Path(filepath).exists()
assert not Path(empty_filepath).exists()
def test_removed_hanging_snapshot_fossil(stubs):
_, testdir, _, filepath = stubs
testdir.makefile(".abc", **{"__snapshots__/test_hanging": ""})
hanging_filepath = Path(testdir.tmpdir, "__snapshots__/test_hanging.abc")
assert hanging_filepath.exists()
result = testdir.runpytest("-v", "--snapshot-update")
result_stdout = clean_output(result.stdout.str())
assert str(Path(filepath).relative_to(Path.cwd())) not in result_stdout
assert "1 unused snapshot deleted" in result_stdout
assert "unknown snapshot" in result_stdout
assert str(hanging_filepath.relative_to(Path.cwd())) in result_stdout
assert result.ret == 0
assert Path(filepath).exists()
assert not Path(hanging_filepath).exists()