From 912330a7e2dfd67db252de4197b00a982043caf3 Mon Sep 17 00:00:00 2001 From: ST John Date: Wed, 29 Nov 2017 16:17:49 +0000 Subject: [PATCH 1/5] Extend _pytest.python._idval to return __name__ of functions as well, not just for classes --- _pytest/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pytest/python.py b/_pytest/python.py index 650171a9e9c..57ebcfbb32e 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -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) From 5085aa2bce6dc6494f889fa73aa50965a499ea5a Mon Sep 17 00:00:00 2001 From: ST John Date: Wed, 29 Nov 2017 16:30:34 +0000 Subject: [PATCH 2/5] add changelog file --- changelog/2976.trivial | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2976.trivial diff --git a/changelog/2976.trivial b/changelog/2976.trivial new file mode 100644 index 00000000000..e5ea0685741 --- /dev/null +++ b/changelog/2976.trivial @@ -0,0 +1 @@ +Change _pytest.python._idval to return __name__ for functions instead of using the fallback (argument name plus counter). From fdd4abb88a649964259a51db53507cd67a7a60d2 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 29 Nov 2017 21:11:34 -0200 Subject: [PATCH 3/5] Small rewording of the CHANGELOG --- changelog/2976.trivial | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/2976.trivial b/changelog/2976.trivial index e5ea0685741..5f767dd2740 100644 --- a/changelog/2976.trivial +++ b/changelog/2976.trivial @@ -1 +1 @@ -Change _pytest.python._idval to return __name__ for functions instead of using the fallback (argument name plus counter). +Change parametrized automatic test id generation to use the ``__name__`` attribute of functions instead of the fallback argument name plus counter. From e66473853c0ecd80c233eccb8833d6d19d8ac07b Mon Sep 17 00:00:00 2001 From: ST John Date: Thu, 30 Nov 2017 10:19:29 +0000 Subject: [PATCH 4/5] add test --- testing/python/metafunc.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 2ffb7bb5da2..9ef95dd6bc7 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -235,6 +235,20 @@ 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 From 652936f47fd9167fbfd858fc33c3ed431a49f230 Mon Sep 17 00:00:00 2001 From: ST John Date: Thu, 30 Nov 2017 10:29:05 +0000 Subject: [PATCH 5/5] make linter happier --- testing/python/metafunc.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 9ef95dd6bc7..0ed9f56bfee 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -240,8 +240,13 @@ def test_class_or_function_idval(self): values that are classes or functions: their __name__. """ from _pytest.python import _idval - class TestClass: pass - def test_function(): pass + + class TestClass: + pass + + def test_function(): + pass + values = [ (TestClass, "TestClass"), (test_function, "test_function"),