Skip to content

Commit

Permalink
Merge pull request pytest-dev#6152 from grlee77/module_name_in_id
Browse files Browse the repository at this point in the history
use __name__ attribute in the parametrize id for modules as well
  • Loading branch information
nicoddemus authored Nov 8, 2019
2 parents 0489104 + c16b121 commit 245e1f1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ George Kussumoto
Georgy Dyuldin
Graham Horler
Greg Price
Gregory Lee
Grig Gheorghiu
Grigorii Eremeev (budulianin)
Guido Wesdorp
Expand Down
1 change: 1 addition & 0 deletions changelog/6152.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Now parametrization will use the ``__name__`` attribute of any object for the id, if present. Previously it would only use ``__name__`` for functions and classes.
3 changes: 2 additions & 1 deletion src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,8 @@ def _idval(val, argname, idx, idfn, item, config):
return ascii_escaped(val.pattern)
elif isinstance(val, enum.Enum):
return str(val)
elif (inspect.isclass(val) or inspect.isfunction(val)) and hasattr(val, "__name__"):
elif hasattr(val, "__name__") and isinstance(val.__name__, str):
# name of a class, function, module, etc.
return val.__name__
return str(argname) + str(idx)

Expand Down
15 changes: 15 additions & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,21 @@ def test_func(arg):
assert list(passed) == list(passed_result)


def test_parametrize_with_module(testdir):
testdir.makepyfile(
"""
import pytest
@pytest.mark.parametrize("arg", [pytest,])
def test_func(arg):
pass
"""
)
rec = testdir.inline_run()
passed, skipped, fail = rec.listoutcomes()
expected_id = "test_func[" + pytest.__name__ + "]"
assert passed[0].nodeid.split("::")[-1] == expected_id


@pytest.mark.parametrize(
"spec",
[
Expand Down

0 comments on commit 245e1f1

Please sign in to comment.