From 9a91b67eeb3550142f8a6e4b093e59140ddc9320 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 25 Aug 2020 19:48:26 -0300 Subject: [PATCH] Merge pull request #7687 from bluetech/idval-notset python: fix empty parametrize() leading to "NotSetType.token" id --- changelog/7686.bugfix.rst | 2 ++ src/_pytest/python.py | 3 +++ testing/python/metafunc.py | 9 +++++++++ 3 files changed, 14 insertions(+) create mode 100644 changelog/7686.bugfix.rst diff --git a/changelog/7686.bugfix.rst b/changelog/7686.bugfix.rst new file mode 100644 index 00000000000..8549fae8eb1 --- /dev/null +++ b/changelog/7686.bugfix.rst @@ -0,0 +1,2 @@ +Fixed `NotSetType.token` being used as the parameter ID when the parametrization list is empty. +Regressed in pytest 6.0.0. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index aa817148683..28c2dc849c7 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -1254,6 +1254,9 @@ def _idval( return str(val) elif isinstance(val, REGEX_TYPE): return ascii_escaped(val.pattern) + elif val is NOTSET: + # Fallback to default. Note that NOTSET is an enum.Enum. + pass elif isinstance(val, enum.Enum): return str(val) elif isinstance(getattr(val, "__name__", None), str): diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 4e6cfaf91ba..5d935153931 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -19,6 +19,7 @@ import pytest from _pytest import fixtures from _pytest import python +from _pytest.compat import NOTSET from _pytest.outcomes import fail from _pytest.pytester import Testdir from _pytest.python import _idval @@ -363,6 +364,14 @@ def test_function(): for val, expected in values: assert _idval(val, "a", 6, None, nodeid=None, config=None) == expected + def test_notset_idval(self) -> None: + """Test that a NOTSET value (used by an empty parameterset) generates + a proper ID. + + Regression test for #7686. + """ + assert _idval(NOTSET, "a", 0, None, nodeid=None, config=None) == "a0" + def test_idmaker_autoname(self) -> None: """#250""" result = idmaker(