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
|
import pytest
@pytest.fixture(params=["__snapshots__", "snapshots"])
def testcases(testdir, tmp_path, request):
dirname = tmp_path.joinpath(request.param)
testdir.makeconftest(
f"""
import pytest
from syrupy.extensions.amber import AmberSnapshotExtension
class CustomSnapshotExtension(AmberSnapshotExtension):
@classmethod
def dirname(cls, *, test_location):
return {str(dirname)!r}
@pytest.fixture
def snapshot(snapshot):
return snapshot.use_extension(CustomSnapshotExtension)
"""
)
return {
"zero": (
"""
def test_do_it(snapshot):
pass
"""
),
"one": (
"""
def test_do_it(snapshot):
assert snapshot == 'passed1'
"""
),
"two": (
"""
def test_do_it(snapshot):
assert snapshot == 'passed1'
assert snapshot == 'passed2'
"""
),
}
@pytest.fixture
def generate_snapshots(testdir, testcases):
testdir.makepyfile(test_file=testcases["two"])
result = testdir.runpytest("-v", "--snapshot-update")
return result, testdir, testcases
def test_generated_snapshots(generate_snapshots):
result = generate_snapshots[0]
result.stdout.re_match_lines((r"2 snapshots generated\.",))
assert "snapshots unused" not in result.stdout.str()
assert result.ret == 0
def test_unmatched_snapshots(generate_snapshots, plugin_args_fails_xdist):
_, testdir, testcases = generate_snapshots
testdir.makepyfile(test_file=testcases["one"])
result = testdir.runpytest("-v", *plugin_args_fails_xdist)
result.stdout.re_match_lines((r"1 snapshot passed. 1 snapshot unused\.",))
assert result.ret == 1
def test_updated_snapshots_partial_delete(generate_snapshots, plugin_args_fails_xdist):
_, testdir, testcases = generate_snapshots
testdir.makepyfile(test_file=testcases["one"])
result = testdir.runpytest("-v", "--snapshot-update", *plugin_args_fails_xdist)
result.stdout.re_match_lines(r"1 snapshot passed. 1 unused snapshot deleted\.")
assert result.ret == 0
def test_updated_snapshots_full_delete(generate_snapshots, plugin_args_fails_xdist):
_, testdir, testcases = generate_snapshots
testdir.makepyfile(test_file=testcases["zero"])
result = testdir.runpytest("-v", "--snapshot-update", *plugin_args_fails_xdist)
result.stdout.re_match_lines(r"2 unused snapshots deleted\.")
assert result.ret == 0
|