From c22ce1a12cc1ad75d50696321a96bd2f1d9a7e86 Mon Sep 17 00:00:00 2001 From: Gregory Lee Date: Fri, 8 Nov 2019 00:52:43 -0500 Subject: [PATCH 1/4] parametrize: allow __name__ id for modules or other objects as well --- src/_pytest/python.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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) From cc6c5e15b81200bf19c3e398a154d034341b73f6 Mon Sep 17 00:00:00 2001 From: Gregory Lee Date: Fri, 8 Nov 2019 01:06:33 -0500 Subject: [PATCH 2/4] update AUTHORS list --- AUTHORS | 1 + 1 file changed, 1 insertion(+) 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 From db82432ec850aed3ee654db131c1cd4879e0cb06 Mon Sep 17 00:00:00 2001 From: Gregory Lee Date: Fri, 8 Nov 2019 01:34:46 -0500 Subject: [PATCH 3/4] add minimal test case --- testing/test_mark.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/testing/test_mark.py b/testing/test_mark.py index 2c12c0451c0..d7ba6e23007 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", [ From c16b121594fae69a1fb63dc8dff54332fec2b0a2 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 8 Nov 2019 10:50:51 -0300 Subject: [PATCH 4/4] Add CHANGELOG for #6152 --- changelog/6152.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/6152.improvement.rst 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.