From 13c545686844f2e09e6e459ec1d291bd8605d3b2 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 7 Aug 2015 07:31:04 +0200 Subject: [PATCH] Generate parametrize IDs for enum/re/class objects. --- _pytest/python.py | 15 +++++++++++++++ testing/python/metafunc.py | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index fe93c938e..c5a479aeb 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1,4 +1,5 @@ """ Python test discovery, setup and run of test functions. """ +import re import fnmatch import functools import py @@ -976,8 +977,22 @@ def _idval(val, argname, idx, idfn): return s except Exception: pass + + try: + import enum + except ImportError: + # Only available in Python 3.4+ + enum = None + if isinstance(val, (float, int, str, bool, NoneType)): 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) def _idvalset(idx, valset, argnames, idfn): diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 7a6d1eebf..90346a5b6 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -1,3 +1,4 @@ +import re import pytest, py from _pytest import python as funcargs @@ -138,6 +139,8 @@ class TestMetafunc: ("three", "three hundred"), (True, False), (None, None), + (re.compile('foo'), re.compile('bar')), + (str, int), (list("six"), [66, 66]), (set([7]), set("seven")), (tuple("eight"), (8, -8, 8)) @@ -147,9 +150,18 @@ class TestMetafunc: "three-three hundred", "True-False", "None-None", - "a5-b5", - "a6-b6", - "a7-b7"] + "foo-bar", + "str-int", + "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 def test_idmaker_idfn(self):