Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use the test node location when determining snapshot class name #197

Merged
merged 2 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/syrupy/location.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path
from typing import (
Any,
Iterator,
Optional,
)

Expand All @@ -16,8 +17,8 @@ def __init__(self, node: Any):

@property
def classname(self) -> Optional[str]:
classes = self._node.obj.__qualname__.split(".")[:-1]
return ".".join(classes) if classes else None
_, __, qualname = self._node.location
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are you seeing documentation for "location"?

Copy link
Collaborator

@noahnu noahnu Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah found it. This is "Item" not "Node": https://docs.pytest.org/en/latest/_modules/_pytest/nodes.html#Item, although I don't see where the 3rd element of the tuple is defined

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

@iamogbz iamogbz Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's Function which inherits from Item and for our use PyobjMixin
https://docs.pytest.org/en/latest/_modules/_pytest/python.html#Function

With the location property from Item using the reportinfo method overwritten in PyobjMixin

https://github.com/pytest-dev/pytest/blob/7d5f5a878598aaa7f3e100ef6a97bcd13c1b408d/src/_pytest/python.py#L315-L330

return ".".join(list(self.__valid_ids(qualname))[:-1]) or None

@property
def filename(self) -> str:
Expand All @@ -38,9 +39,11 @@ def __valid_id(self, name: str) -> str:
valid_id = new_valid_id
return valid_id

def __valid_ids(self, name: str) -> Iterator[str]:
return filter(None, (self.__valid_id(n) for n in name.split(".")))

def __parse(self, name: str) -> str:
valid_ids = (self.__valid_id(n) for n in name.split("."))
return ".".join(valid_id for valid_id in valid_ids if valid_id)
return ".".join(self.__valid_ids(name))

def matches_snapshot_name(self, snapshot_name: str) -> bool:
return self.__parse(self.snapshot_name) == self.__parse(snapshot_name)
Expand Down
21 changes: 21 additions & 0 deletions tests/__snapshots__/test_extension_amber.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@
# name: TestClass.test_class_method_parametrized[c]
'c'
---
# name: TestSubClass.TestNestedClass.test_nested_class_method[x]
'parameterized nested class method x'
---
# name: TestSubClass.TestNestedClass.test_nested_class_method[y]
'parameterized nested class method y'
---
# name: TestSubClass.TestNestedClass.test_nested_class_method[z]
'parameterized nested class method z'
---
# name: TestSubClass.test_class_method_name
'this is in a test class'
---
# name: TestSubClass.test_class_method_parametrized[a]
'a'
---
# name: TestSubClass.test_class_method_parametrized[b]
'b'
---
# name: TestSubClass.test_class_method_parametrized[c]
'c'
---
# name: test_bool[False]
False
---
Expand Down
4 changes: 4 additions & 0 deletions tests/test_extension_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,7 @@ def test_class_method_parametrized(self, snapshot, actual):
class TestNestedClass:
def test_nested_class_method(self, snapshot, actual):
assert snapshot == f"parameterized nested class method {actual}"


class TestSubClass(TestClass):
pass