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

Extend _pytest.python._idval to return __name__ of functions as well #2976

Merged
merged 6 commits into from
Nov 30, 2017
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
2 changes: 1 addition & 1 deletion _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions changelog/2976.trivial
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change parametrized automatic test id generation to use the ``__name__`` attribute of functions instead of the fallback argument name plus counter.
19 changes: 19 additions & 0 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,25 @@ def test_bytes_idval(self):
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
Expand Down