Skip to content

Commit

Permalink
fix: incorrect marking of TestClass.test_method as unused, close #717 (
Browse files Browse the repository at this point in the history
  • Loading branch information
noahnu authored Jun 19, 2023
1 parent eb3183d commit 0badfdb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/syrupy/extensions/single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ def _read_snapshot_collection(
) -> "SnapshotCollection":
file_ext_len = len(self._file_extension) + 1 if self._file_extension else 0
filename_wo_ext = snapshot_location[:-file_ext_len]
basename = Path(filename_wo_ext).parts[-1]

snapshot_collection = SnapshotCollection(location=snapshot_location)
snapshot_collection.add(Snapshot(name=Path(filename_wo_ext).stem))
snapshot_collection.add(Snapshot(name=basename))
return snapshot_collection

def _read_snapshot_data_from_location(
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/test_single_file_multiple_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,39 @@ def test_dot_in_filename(snapshot):
result.stdout.re_match_lines((r"1 snapshot passed\."))
assert "snapshots unused" not in result.stdout.str()
assert result.ret == 0


def test_class_style(testdir):
"""
Regression test for https://github.com/tophat/syrupy/issues/717
"""

testcase = """
import pytest
from syrupy.extensions.json import JSONSnapshotExtension
@pytest.fixture
def snapshot(snapshot):
return snapshot.use_extension(JSONSnapshotExtension)
class TestFoo:
def test_foo(self, snapshot):
assert { 'key': 'value' } == snapshot
"""

test_file: Path = testdir.makepyfile(test_file=testcase)

result = testdir.runpytest("-v", "--snapshot-update")
result.stdout.re_match_lines((r"1 snapshot generated\."))
assert "deleted" not in result.stdout.str()
assert result.ret == 0

snapshot_file = (
Path(test_file).parent / "__snapshots__" / "test_file" / "TestFoo.test_foo.json"
)
assert snapshot_file.exists()

result = testdir.runpytest("-v")
result.stdout.re_match_lines((r"1 snapshot passed\."))
assert "snapshots unused" not in result.stdout.str()
assert result.ret == 0

0 comments on commit 0badfdb

Please sign in to comment.