python: unify code to generate ID from value

In the following

    @pytest.mark.parametrize(..., ids=[val])

the ID values are only allowed to be `str`, `float`, `int` or `bool`.

In the following

    @pytest.mark.parametrize(..., [val])

    @pytest.mark.parametrize(..., [pytest.param(..., id=val])

a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.

In the interest of consistency, use the latter code path for all cases.
This commit is contained in:
Ran Benita
2022-02-05 12:25:48 +02:00
parent c01a5c177b
commit c3aa4647c7
4 changed files with 85 additions and 79 deletions

View File

@@ -106,8 +106,8 @@ class TestMetafunc:
with pytest.raises(
fail.Exception,
match=(
r"In func: ids must be list of string/float/int/bool, found:"
r" Exc\(from_gen\) \(type: <class .*Exc'>\) at index 2"
r"In func: ids contains unsupported value Exc\(from_gen\) \(type: <class .*Exc'>\) at index 2. "
r"Supported types are: .*"
),
):
metafunc.parametrize("x", [1, 2, 3], ids=gen()) # type: ignore[arg-type]
@@ -285,7 +285,7 @@ class TestMetafunc:
deadline=400.0
) # very close to std deadline and CI boxes are not reliable in CPU power
def test_idval_hypothesis(self, value) -> None:
escaped = IdMaker([], [], None, None, None, None)._idval(value, "a", 6)
escaped = IdMaker([], [], None, None, None, None, None)._idval(value, "a", 6)
assert isinstance(escaped, str)
escaped.encode("ascii")
@@ -308,7 +308,8 @@ class TestMetafunc:
]
for val, expected in values:
assert (
IdMaker([], [], None, None, None, None)._idval(val, "a", 6) == expected
IdMaker([], [], None, None, None, None, None)._idval(val, "a", 6)
== expected
)
def test_unicode_idval_with_config(self) -> None:
@@ -337,7 +338,7 @@ class TestMetafunc:
("ação", MockConfig({option: False}), "a\\xe7\\xe3o"),
]
for val, config, expected in values:
actual = IdMaker([], [], None, None, config, None)._idval(val, "a", 6)
actual = IdMaker([], [], None, None, config, None, None)._idval(val, "a", 6)
assert actual == expected
def test_bytes_idval(self) -> None:
@@ -351,7 +352,8 @@ class TestMetafunc:
]
for val, expected in values:
assert (
IdMaker([], [], None, None, None, None)._idval(val, "a", 6) == expected
IdMaker([], [], None, None, None, None, None)._idval(val, "a", 6)
== expected
)
def test_class_or_function_idval(self) -> None:
@@ -367,7 +369,8 @@ class TestMetafunc:
values = [(TestClass, "TestClass"), (test_function, "test_function")]
for val, expected in values:
assert (
IdMaker([], [], None, None, None, None)._idval(val, "a", 6) == expected
IdMaker([], [], None, None, None, None, None)._idval(val, "a", 6)
== expected
)
def test_notset_idval(self) -> None:
@@ -376,7 +379,9 @@ class TestMetafunc:
Regression test for #7686.
"""
assert IdMaker([], [], None, None, None, None)._idval(NOTSET, "a", 0) == "a0"
assert (
IdMaker([], [], None, None, None, None, None)._idval(NOTSET, "a", 0) == "a0"
)
def test_idmaker_autoname(self) -> None:
"""#250"""
@@ -387,6 +392,7 @@ class TestMetafunc:
None,
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["string-1.0", "st-ring-2.0"]
@@ -397,17 +403,18 @@ class TestMetafunc:
None,
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["a0-1.0", "a1-b1"]
# unicode mixing, issue250
result = IdMaker(
("a", "b"), [pytest.param({}, b"\xc3\xb4")], None, None, None, None
("a", "b"), [pytest.param({}, b"\xc3\xb4")], None, None, None, None, None
).make_unique_parameterset_ids()
assert result == ["a0-\\xc3\\xb4"]
def test_idmaker_with_bytes_regex(self) -> None:
result = IdMaker(
("a"), [pytest.param(re.compile(b"foo"), 1.0)], None, None, None, None
("a"), [pytest.param(re.compile(b"foo"), 1.0)], None, None, None, None, None
).make_unique_parameterset_ids()
assert result == ["foo"]
@@ -433,6 +440,7 @@ class TestMetafunc:
None,
None,
None,
None,
).make_unique_parameterset_ids()
assert result == [
"1.0--1.1",
@@ -465,6 +473,7 @@ class TestMetafunc:
None,
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["\\x00-1", "\\x05-2", "\\x00-3", "\\x05-4", "\\t-5", "\\t-6"]
@@ -479,6 +488,7 @@ class TestMetafunc:
None,
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["hello \\x00", "hello \\x05"]
@@ -486,7 +496,7 @@ class TestMetafunc:
enum = pytest.importorskip("enum")
e = enum.Enum("Foo", "one, two")
result = IdMaker(
("a", "b"), [pytest.param(e.one, e.two)], None, None, None, None
("a", "b"), [pytest.param(e.one, e.two)], None, None, None, None, None
).make_unique_parameterset_ids()
assert result == ["Foo.one-Foo.two"]
@@ -509,6 +519,7 @@ class TestMetafunc:
None,
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["10.0-IndexError()", "20-KeyError()", "three-b2"]
@@ -529,6 +540,7 @@ class TestMetafunc:
None,
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["a-a0", "a-a1", "a-a2"]
@@ -560,7 +572,13 @@ class TestMetafunc:
]
for config, expected in values:
result = IdMaker(
("a",), [pytest.param("string")], lambda _: "ação", None, config, None
("a",),
[pytest.param("string")],
lambda _: "ação",
None,
config,
None,
None,
).make_unique_parameterset_ids()
assert result == [expected]
@@ -592,7 +610,7 @@ class TestMetafunc:
]
for config, expected in values:
result = IdMaker(
("a",), [pytest.param("string")], None, ["ação"], config, None
("a",), [pytest.param("string")], None, ["ação"], config, None, None
).make_unique_parameterset_ids()
assert result == [expected]
@@ -657,6 +675,7 @@ class TestMetafunc:
["a", None],
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["a", "3-4"]
@@ -668,6 +687,7 @@ class TestMetafunc:
["a", None],
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["me", "you"]
@@ -679,6 +699,7 @@ class TestMetafunc:
["a", "a", "b", "c", "b"],
None,
None,
None,
).make_unique_parameterset_ids()
assert result == ["a0", "a1", "b0", "c", "b1"]
@@ -1318,7 +1339,7 @@ class TestMetafuncFunctional:
"""
import pytest
@pytest.mark.parametrize("x, expected", [(1, 2), (3, 4), (5, 6)], ids=(None, 2, type))
@pytest.mark.parametrize("x, expected", [(1, 2), (3, 4), (5, 6)], ids=(None, 2, OSError()))
def test_ids_numbers(x,expected):
assert x * 2 == expected
"""
@@ -1326,8 +1347,8 @@ class TestMetafuncFunctional:
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"In test_ids_numbers: ids must be list of string/float/int/bool,"
" found: <class 'type'> (type: <class 'type'>) at index 2"
"In test_ids_numbers: ids contains unsupported value OSError() (type: <class 'OSError'>) at index 2. "
"Supported types are: str, bytes, int, float, complex, bool, enum, regex or anything with a __name__."
]
)