-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_snapshot_option_include_details.py
148 lines (129 loc) · 4.09 KB
/
test_snapshot_option_include_details.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
import pytest
@pytest.fixture
def testcases():
return {
"used": (
"""
def test_used(snapshot):
assert snapshot == 'used'
"""
),
"unused": (
"""
def test_unused(snapshot):
assert snapshot == 'unused'
"""
),
}
@pytest.fixture
def extra_testcases():
return {
"extra_a": (
"""
def test_extra_a(snapshot):
assert snapshot == 'extra_a'
"""
),
"extra_b": (
"""
def test_extra_b(snapshot):
assert snapshot == 'extra_b'
"""
),
}
@pytest.fixture
def run_testfiles_with_update(testdir):
def run_testfiles_with_update_impl(**testfiles):
testdir.makepyfile(
**{
filename: "\n\n".join(cases.values())
for (filename, cases) in testfiles.items()
}
)
result = testdir.runpytest("-v", "--snapshot-update")
result.stdout.re_match_lines((r"[0-9]+ snapshots generated\."))
return testdir
return run_testfiles_with_update_impl
@pytest.mark.parametrize(
(
"options",
"expected_status_code",
),
(
(("-v", "--snapshot-details"), 1),
(("-v", "--snapshot-details", "--snapshot-warn-unused"), 0),
),
)
def test_unused_snapshots_details(
options,
expected_status_code,
run_testfiles_with_update,
testcases,
plugin_args,
):
testdir = run_testfiles_with_update(test_file=testcases)
testdir.makepyfile(test_file=testcases["used"])
result = testdir.runpytest(*options, *plugin_args)
result.stdout.re_match_lines(
(
r"1 snapshot passed\. 1 snapshot unused\.",
r"Unused test_unused \(__snapshots__[\\/]test_file.ambr\)",
r"Re-run pytest with --snapshot-update to delete unused snapshots\.",
)
)
assert result.ret == expected_status_code
def test_unused_snapshots_details_multiple_tests(
run_testfiles_with_update, testcases, extra_testcases, plugin_args
):
testdir = run_testfiles_with_update(
test_file=testcases, test_second_file=extra_testcases
)
testdir.makepyfile(
test_file="\n\n".join(testcases.values()),
test_second_file="",
)
result = testdir.runpytest("-v", "--snapshot-details", *plugin_args)
result.stdout.re_match_lines(
(
r"2 snapshots passed\. 2 snapshots unused\.",
r"Unused test_extra_a, test_extra_b "
r"\(__snapshots__[\\/]test_second_file.ambr\)",
r"Re-run pytest with --snapshot-update to delete unused snapshots\.",
)
)
assert result.ret == 1
def test_unused_snapshots_details_multiple_locations(
run_testfiles_with_update, testcases, extra_testcases, plugin_args
):
testdir = run_testfiles_with_update(
test_file=testcases, test_second_file=extra_testcases
)
testdir.makepyfile(
test_file=testcases["used"],
test_second_file=extra_testcases["extra_a"],
)
result = testdir.runpytest("-v", "--snapshot-details", *plugin_args)
result.stdout.re_match_lines_random(
(
r"2 snapshots passed\. 2 snapshots unused\.",
r"Unused test_extra_b \(__snapshots__[\\/]test_second_file.ambr\)",
r"Unused test_unused \(__snapshots__[\\/]test_file.ambr\)",
r"Re-run pytest with --snapshot-update to delete unused snapshots\.",
)
)
assert result.ret == 1
def test_unused_snapshots_details_no_details_on_deletion(
run_testfiles_with_update, testcases, plugin_args
):
testdir = run_testfiles_with_update(test_file=testcases)
testdir.makepyfile(test_file=testcases["used"])
result = testdir.runpytest(
"-v", "--snapshot-details", "--snapshot-update", *plugin_args
)
result.stdout.re_match_lines(
(
r"1 snapshot passed\. 1 unused snapshot deleted\.",
r"Deleted test_unused \(__snapshots__[\\/]test_file.ambr\)",
)
)
assert result.ret == 0