Merge pull request #2976 from st--/master

Extend _pytest.python._idval to return __name__ of functions as well
This commit is contained in:
Bruno Oliveira 2017-11-30 18:01:21 -02:00 committed by GitHub
commit 294729962d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -933,7 +933,7 @@ def _idval(val, argname, idx, idfn, config=None):
return ascii_escaped(val.pattern)
elif enum is not None and isinstance(val, enum.Enum):
return str(val)
elif isclass(val) and hasattr(val, '__name__'):
elif (isclass(val) or isfunction(val)) and hasattr(val, '__name__'):
return val.__name__
return str(argname) + str(idx)

1
changelog/2976.trivial Normal file
View File

@ -0,0 +1 @@
Change parametrized automatic test id generation to use the ``__name__`` attribute of functions instead of the fallback argument name plus counter.

View File

@ -235,6 +235,25 @@ class TestMetafunc(object):
for val, expected in values:
assert _idval(val, 'a', 6, None) == expected
def test_class_or_function_idval(self):
"""unittest for the expected behavior to obtain ids for parametrized
values that are classes or functions: their __name__.
"""
from _pytest.python import _idval
class TestClass:
pass
def test_function():
pass
values = [
(TestClass, "TestClass"),
(test_function, "test_function"),
]
for val, expected in values:
assert _idval(val, 'a', 6, None) == expected
@pytest.mark.issue250
def test_idmaker_autoname(self):
from _pytest.python import idmaker