Merge pull request #923 from The-Compiler/parametrize-idfunc

Generate parametrize IDs for enum/re/class objects.
This commit is contained in:
Bruno Oliveira 2015-08-08 15:09:54 -03:00
commit 729b5e9b2f
3 changed files with 35 additions and 3 deletions

View File

@ -1,6 +1,10 @@
2.8.0.dev (compared to 2.7.X) 2.8.0.dev (compared to 2.7.X)
----------------------------- -----------------------------
- parametrize now also generates meaningful test IDs for enum, regex and class
objects (as opposed to class instances).
Thanks to Florian Bruhin for the PR.
- Add 'warns' to assert that warnings are thrown (like 'raises'). - Add 'warns' to assert that warnings are thrown (like 'raises').
Thanks to Eric Hunsberger for the PR. Thanks to Eric Hunsberger for the PR.

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
@ -8,6 +9,12 @@ import pytest
from _pytest.mark import MarkDecorator, MarkerError from _pytest.mark import MarkDecorator, MarkerError
from py._code.code import TerminalRepr from py._code.code import TerminalRepr
try:
import enum
except ImportError: # pragma: no cover
# Only available in Python 3.4+ or as a backport
enum = None
import _pytest import _pytest
import pluggy import pluggy
@ -22,6 +29,8 @@ isclass = inspect.isclass
callable = py.builtin.callable callable = py.builtin.callable
# used to work around a python2 exception info leak # used to work around a python2 exception info leak
exc_clear = getattr(sys, 'exc_clear', lambda: None) exc_clear = getattr(sys, 'exc_clear', lambda: None)
# The type of re.compile objects is not exposed in Python.
REGEX_TYPE = type(re.compile(''))
def filter_traceback(entry): def filter_traceback(entry):
return entry.path != cutdir1 and not entry.path.relto(cutdir2) return entry.path != cutdir1 and not entry.path.relto(cutdir2)
@ -979,8 +988,15 @@ def _idval(val, argname, idx, idfn):
return s return s
except Exception: except Exception:
pass pass
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, REGEX_TYPE):
return val.pattern
elif enum is not None and isinstance(val, enum.Enum):
return str(val)
elif isclass(val) 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):