Skip to content

Commit 729b5e9

Browse files
committed
Merge pull request #923 from The-Compiler/parametrize-idfunc
Generate parametrize IDs for enum/re/class objects.
2 parents 9ae40e3 + 4e21d1d commit 729b5e9

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
2.8.0.dev (compared to 2.7.X)
22
-----------------------------
33

4+
- parametrize now also generates meaningful test IDs for enum, regex and class
5+
objects (as opposed to class instances).
6+
Thanks to Florian Bruhin for the PR.
7+
48
- Add 'warns' to assert that warnings are thrown (like 'raises').
59
Thanks to Eric Hunsberger for the PR.
610

_pytest/python.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" Python test discovery, setup and run of test functions. """
2+
import re
23
import fnmatch
34
import functools
45
import py
@@ -8,6 +9,12 @@
89
from _pytest.mark import MarkDecorator, MarkerError
910
from py._code.code import TerminalRepr
1011

12+
try:
13+
import enum
14+
except ImportError: # pragma: no cover
15+
# Only available in Python 3.4+ or as a backport
16+
enum = None
17+
1118
import _pytest
1219
import pluggy
1320

@@ -22,6 +29,8 @@
2229
callable = py.builtin.callable
2330
# used to work around a python2 exception info leak
2431
exc_clear = getattr(sys, 'exc_clear', lambda: None)
32+
# The type of re.compile objects is not exposed in Python.
33+
REGEX_TYPE = type(re.compile(''))
2534

2635
def filter_traceback(entry):
2736
return entry.path != cutdir1 and not entry.path.relto(cutdir2)
@@ -979,8 +988,15 @@ def _idval(val, argname, idx, idfn):
979988
return s
980989
except Exception:
981990
pass
991+
982992
if isinstance(val, (float, int, str, bool, NoneType)):
983993
return str(val)
994+
elif isinstance(val, REGEX_TYPE):
995+
return val.pattern
996+
elif enum is not None and isinstance(val, enum.Enum):
997+
return str(val)
998+
elif isclass(val) and hasattr(val, '__name__'):
999+
return val.__name__
9841000
return str(argname)+str(idx)
9851001

9861002
def _idvalset(idx, valset, argnames, idfn):

testing/python/metafunc.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12

23
import pytest, py
34
from _pytest import python as funcargs
@@ -138,6 +139,8 @@ def test_idmaker_native_strings(self):
138139
("three", "three hundred"),
139140
(True, False),
140141
(None, None),
142+
(re.compile('foo'), re.compile('bar')),
143+
(str, int),
141144
(list("six"), [66, 66]),
142145
(set([7]), set("seven")),
143146
(tuple("eight"), (8, -8, 8))
@@ -147,9 +150,18 @@ def test_idmaker_native_strings(self):
147150
"three-three hundred",
148151
"True-False",
149152
"None-None",
150-
"a5-b5",
151-
"a6-b6",
152-
"a7-b7"]
153+
"foo-bar",
154+
"str-int",
155+
"a7-b7",
156+
"a8-b8",
157+
"a9-b9"]
158+
159+
def test_idmaker_enum(self):
160+
from _pytest.python import idmaker
161+
enum = pytest.importorskip("enum")
162+
e = enum.Enum("Foo", "one, two")
163+
result = idmaker(("a", "b"), [(e.one, e.two)])
164+
assert result == ["Foo.one-Foo.two"]
153165

154166
@pytest.mark.issue351
155167
def test_idmaker_idfn(self):

0 commit comments

Comments
 (0)