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

Function: use originalname in _getobj and make it default to name #350

Open
wants to merge 3 commits into
base: my-master
Choose a base branch
from
Open
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
18 changes: 7 additions & 11 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,12 @@ def __init__(
if callobj is not NOTSET:
self.obj = callobj

#: Original function name, without any decorations (for example
#: parametrization adds a ``"[...]"`` suffix to function names).
#:
#: .. versionadded:: 3.0
self.originalname = originalname if originalname is not None else name

self.keywords.update(self.obj.__dict__)
self.own_markers.extend(get_unpacked_marks(self.obj))
if callspec:
Expand Down Expand Up @@ -1450,12 +1456,6 @@ def __init__(
self.fixturenames = fixtureinfo.names_closure
self._initrequest()

#: original function name, without any decorations (for example
#: parametrization adds a ``"[...]"`` suffix to function names).
#:
#: .. versionadded:: 3.0
self.originalname = originalname

@classmethod
def from_parent(cls, parent, **kw): # todo: determine sound type limitations
"""
Expand All @@ -1473,11 +1473,7 @@ def function(self):
return getimfunc(self.obj)

def _getobj(self):
name = self.name
i = name.find("[") # parametrization
if i != -1:
name = name[:i]
return getattr(self.parent.obj, name)
return getattr(self.parent.obj, self.originalname)

@property
def _pyfuncitem(self):
Expand Down
27 changes: 25 additions & 2 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,16 +695,39 @@ def test_passed(x):
result = testdir.runpytest()
result.stdout.fnmatch_lines(["* 3 passed in *"])

def test_function_original_name(self, testdir):
def test_function_originalname(self, testdir: "Testdir") -> None:
items = testdir.getitems(
"""
import pytest

@pytest.mark.parametrize('arg', [1,2])
def test_func(arg):
pass

def test_no_param():
pass
"""
)
assert [x.originalname for x in items] == ["test_func", "test_func"]
assert [x.originalname for x in items] == [
"test_func",
"test_func",
"test_no_param",
]

def test_function_with_square_brackets(self, testdir: "Testdir") -> None:
"""Check that Function._getobj uses originalname."""
p1 = testdir.makepyfile(
"""
locals()["test_foo[name]"] = lambda: None
"""
)
result = testdir.runpytest("-v", str(p1))
result.stdout.fnmatch_lines(
[
"test_function_with_square_brackets.py::test_foo[[]name[]] PASSED *",
"*= 1 passed in *",
]
)


class TestSorting:
Expand Down