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
|
import pytest
@pytest.fixture
def testcases_initial(testdir):
testdir.makeconftest(
"""
import pytest
from syrupy.extensions.amber import AmberSnapshotExtension
from syrupy.extensions.image import (
PNGImageSnapshotExtension,
SVGImageSnapshotExtension,
)
from syrupy.extensions.single_file import SingleFileSnapshotExtension
class CustomSnapshotExtension(AmberSnapshotExtension):
_file_extension = ""
def serialize(self, data, **kwargs):
return str(data)
@classmethod
def get_snapshot_name(cls, *, test_location, index):
testname = test_location.testname[::-1]
return f"{testname}.{index}"
@classmethod
def _get_file_basename(cls, *, test_location, index):
return test_location.basename[::-1]
@pytest.fixture
def snapshot_custom(snapshot):
return snapshot.use_extension(CustomSnapshotExtension)
@pytest.fixture
def snapshot_single(snapshot):
return snapshot.use_extension(SingleFileSnapshotExtension)
@pytest.fixture
def snapshot_png(snapshot):
return snapshot.use_extension(PNGImageSnapshotExtension)
@pytest.fixture
def snapshot_svg(snapshot):
return snapshot.use_extension(SVGImageSnapshotExtension)
"""
)
return {
"passed": (
"""
def test_passed_custom(snapshot_custom):
assert snapshot_custom == 'passed1'
assert snapshot_custom == 'passed2'
def test_passed_single(snapshot_single):
assert snapshot_single == b'passed1'
assert snapshot_single == b'passed2'
"""
),
"failed": (
"""
def test_failed_single(snapshot_single):
assert snapshot_single == 'failed'
def test_failed_image(snapshot_png):
assert "not a byte string" == snapshot_png
"""
),
}
@pytest.fixture
def testcases_updated(testcases_initial):
updated_testcases = {
"passed": (
"""
def test_passed_single(snapshot_single):
assert snapshot_single == b'passed'
"""
)
}
return {**testcases_initial, **updated_testcases}
@pytest.fixture
def generate_snapshots(testdir, testcases_initial):
testdir.makepyfile(test_file=testcases_initial["passed"])
result = testdir.runpytest("-v", "--snapshot-update")
return result, testdir, testcases_initial
# xdist behavior different on Windows - needs to be reviewed
def test_unsaved_snapshots(testdir, testcases_initial):
testdir.makepyfile(test_file=testcases_initial["passed"])
result = testdir.runpytest("-v")
result.stdout.re_match_lines(
(r".*Snapshot 'test_passed_single' does not exist!", r".*\+ b'passed1'")
)
assert result.ret == 1
def test_failed_snapshots(testdir, testcases_initial, plugin_args_fails_xdist):
testdir.makepyfile(test_file=testcases_initial["failed"])
result = testdir.runpytest("-v", "--snapshot-update", *plugin_args_fails_xdist)
result.stdout.re_match_lines((r"2 snapshots failed\.",))
assert result.ret == 1
def test_generated_snapshots(generate_snapshots):
result = generate_snapshots[0]
result.stdout.re_match_lines((r"4 snapshots generated\.",))
assert "snapshots unused" not in result.stdout.str()
assert result.ret == 0
def test_unmatched_snapshots(
generate_snapshots, testcases_updated, plugin_args_fails_xdist
):
testdir = generate_snapshots[1]
testdir.makepyfile(test_file=testcases_updated["passed"])
result = testdir.runpytest("-v", *plugin_args_fails_xdist)
result.stdout.re_match_lines((r"1 snapshot failed\. 2 snapshots unused\.",))
assert result.ret == 1
def test_updated_snapshots(
generate_snapshots, testcases_updated, plugin_args_fails_xdist
):
testdir = generate_snapshots[1]
testdir.makepyfile(test_file=testcases_updated["passed"])
result = testdir.runpytest("-v", "--snapshot-update", *plugin_args_fails_xdist)
result.stdout.re_match_lines(
(r"1 snapshot updated\. 2 unused snapshots deleted\.",)
)
assert result.ret == 0
def test_warns_on_snapshot_name(generate_snapshots):
result = generate_snapshots[0]
result.stdout.re_match_lines(
(
r".*Warning:\s+",
r"\s+Can not relate snapshot location",
r"\s+Can not relate snapshot name",
r"4 snapshots generated\.",
)
)
assert result.ret == 0
|