Skip to content

Commit 1a79137

Browse files
committed
Add originalname attribute to Function
Related to #1790
1 parent 530b005 commit 1a79137

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

_pytest/python.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,13 @@ def _genfunctions(self, name, funcobj):
357357
fixtures.add_funcarg_pseudo_fixture_def(self, metafunc, fm)
358358

359359
for callspec in metafunc._calls:
360-
subname = "%s[%s]" %(name, callspec.id)
360+
subname = "%s[%s]" % (name, callspec.id)
361361
yield Function(name=subname, parent=self,
362362
callspec=callspec, callobj=funcobj,
363363
fixtureinfo=fixtureinfo,
364-
keywords={callspec.id:True})
364+
keywords={callspec.id:True},
365+
originalname=name,
366+
)
365367

366368

367369
def _marked(func, mark):
@@ -1471,7 +1473,7 @@ class Function(FunctionMixin, pytest.Item, fixtures.FuncargnamesCompatAttr):
14711473
_genid = None
14721474
def __init__(self, name, parent, args=None, config=None,
14731475
callspec=None, callobj=NOTSET, keywords=None, session=None,
1474-
fixtureinfo=None):
1476+
fixtureinfo=None, originalname=None):
14751477
super(Function, self).__init__(name, parent, config=config,
14761478
session=session)
14771479
self._args = args
@@ -1493,6 +1495,12 @@ def __init__(self, name, parent, args=None, config=None,
14931495
self.fixturenames = fixtureinfo.names_closure
14941496
self._initrequest()
14951497

1498+
#: original function name, without any decorations (for example
1499+
#: parametrization adds a ``"[...]"`` suffix to function names).
1500+
#:
1501+
#: .. versionadded:: 3.0
1502+
self.originalname = originalname
1503+
14961504
def _initrequest(self):
14971505
self.funcargs = {}
14981506
if self._isyieldedfunction():

testing/python/collect.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,15 @@ def test_passed(x):
634634
result = testdir.runpytest()
635635
result.stdout.fnmatch_lines('* 3 passed in *')
636636

637+
def test_function_original_name(self, testdir):
638+
items = testdir.getitems("""
639+
import pytest
640+
@pytest.mark.parametrize('arg', [1,2])
641+
def test_func(arg):
642+
pass
643+
""")
644+
assert [x.originalname for x in items] == ['test_func', 'test_func']
645+
637646

638647
class TestSorting:
639648
def test_check_equality(self, testdir):

0 commit comments

Comments
 (0)