Skip to content

Commit 245e1f1

Browse files
authored
Merge pull request #6152 from grlee77/module_name_in_id
use __name__ attribute in the parametrize id for modules as well
2 parents 0489104 + c16b121 commit 245e1f1

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ George Kussumoto
103103
Georgy Dyuldin
104104
Graham Horler
105105
Greg Price
106+
Gregory Lee
106107
Grig Gheorghiu
107108
Grigorii Eremeev (budulianin)
108109
Guido Wesdorp

changelog/6152.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

src/_pytest/python.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,8 @@ def _idval(val, argname, idx, idfn, item, config):
11621162
return ascii_escaped(val.pattern)
11631163
elif isinstance(val, enum.Enum):
11641164
return str(val)
1165-
elif (inspect.isclass(val) or inspect.isfunction(val)) and hasattr(val, "__name__"):
1165+
elif hasattr(val, "__name__") and isinstance(val.__name__, str):
1166+
# name of a class, function, module, etc.
11661167
return val.__name__
11671168
return str(argname) + str(idx)
11681169

testing/test_mark.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ def test_func(arg):
314314
assert list(passed) == list(passed_result)
315315

316316

317+
def test_parametrize_with_module(testdir):
318+
testdir.makepyfile(
319+
"""
320+
import pytest
321+
@pytest.mark.parametrize("arg", [pytest,])
322+
def test_func(arg):
323+
pass
324+
"""
325+
)
326+
rec = testdir.inline_run()
327+
passed, skipped, fail = rec.listoutcomes()
328+
expected_id = "test_func[" + pytest.__name__ + "]"
329+
assert passed[0].nodeid.split("::")[-1] == expected_id
330+
331+
317332
@pytest.mark.parametrize(
318333
"spec",
319334
[

0 commit comments

Comments
 (0)