diff --git a/AUTHORS b/AUTHORS index b0f9d165195..763d904a4c8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -103,6 +103,7 @@ George Kussumoto Georgy Dyuldin Graham Horler Greg Price +Gregory Lee Grig Gheorghiu Grigorii Eremeev (budulianin) Guido Wesdorp diff --git a/changelog/6152.improvement.rst b/changelog/6152.improvement.rst new file mode 100644 index 00000000000..8e5f4d52aa2 --- /dev/null +++ b/changelog/6152.improvement.rst @@ -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. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index d7fb3d78e17..c1654b1c93c 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -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) diff --git a/testing/test_mark.py b/testing/test_mark.py index 071775aefba..ba759980400 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -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", [