Generate parametrize IDs for enum/re/class objects.

This commit is contained in:
Florian Bruhin 2015-08-07 07:31:04 +02:00
parent 4f83586f55
commit 13c5456868
2 changed files with 30 additions and 3 deletions

View File

@ -1,4 +1,5 @@
""" Python test discovery, setup and run of test functions. """ """ Python test discovery, setup and run of test functions. """
import re
import fnmatch import fnmatch
import functools import functools
import py import py
@ -976,8 +977,22 @@ def _idval(val, argname, idx, idfn):
return s return s
except Exception: except Exception:
pass pass
try:
import enum
except ImportError:
# Only available in Python 3.4+
enum = None
if isinstance(val, (float, int, str, bool, NoneType)): if isinstance(val, (float, int, str, bool, NoneType)):
return str(val) return str(val)
elif isinstance(val, type(re.compile(''))):
# The type of re.compile objects is not exposed in Python.
return val.pattern
elif enum is not None and isinstance(val, enum.Enum):
return str(val)
elif isinstance(val, type) and hasattr(val, '__name__'):
return val.__name__
return str(argname)+str(idx) return str(argname)+str(idx)
def _idvalset(idx, valset, argnames, idfn): def _idvalset(idx, valset, argnames, idfn):

View File

@ -1,3 +1,4 @@
import re
import pytest, py import pytest, py
from _pytest import python as funcargs from _pytest import python as funcargs
@ -138,6 +139,8 @@ class TestMetafunc:
("three", "three hundred"), ("three", "three hundred"),
(True, False), (True, False),
(None, None), (None, None),
(re.compile('foo'), re.compile('bar')),
(str, int),
(list("six"), [66, 66]), (list("six"), [66, 66]),
(set([7]), set("seven")), (set([7]), set("seven")),
(tuple("eight"), (8, -8, 8)) (tuple("eight"), (8, -8, 8))
@ -147,9 +150,18 @@ class TestMetafunc:
"three-three hundred", "three-three hundred",
"True-False", "True-False",
"None-None", "None-None",
"a5-b5", "foo-bar",
"a6-b6", "str-int",
"a7-b7"] "a7-b7",
"a8-b8",
"a9-b9"]
def test_idmaker_enum(self):
from _pytest.python import idmaker
enum = pytest.importorskip("enum")
e = enum.Enum("Foo", "one, two")
result = idmaker(("a", "b"), [(e.one, e.two)])
assert result == ["Foo.one-Foo.two"]
@pytest.mark.issue351 @pytest.mark.issue351
def test_idmaker_idfn(self): def test_idmaker_idfn(self):