Skip to content

Generate parametrize IDs for enum/re/class objects. #923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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').
Thanks to Eric Hunsberger for the PR.

Expand Down
16 changes: 16 additions & 0 deletions _pytest/python.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Python test discovery, setup and run of test functions. """
import re
import fnmatch
import functools
import py
Expand All @@ -8,6 +9,12 @@
from _pytest.mark import MarkDecorator, MarkerError
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 pluggy

Expand All @@ -22,6 +29,8 @@
callable = py.builtin.callable
# used to work around a python2 exception info leak
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):
return entry.path != cutdir1 and not entry.path.relto(cutdir2)
Expand Down Expand Up @@ -976,8 +985,15 @@ def _idval(val, argname, idx, idfn):
return s
except Exception:
pass

if isinstance(val, (float, int, str, bool, NoneType)):
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__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment here about what you are expecting this to identify, it's not immediately obvious

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps using inspect.isclass here would be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoddemus good point! I'll use that instead. It's even there in 2.6 already 😄

return str(argname)+str(idx)

def _idvalset(idx, valset, argnames, idfn):
Expand Down
18 changes: 15 additions & 3 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re

import pytest, py
from _pytest import python as funcargs
Expand Down Expand Up @@ -138,6 +139,8 @@ def test_idmaker_native_strings(self):
("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))
Expand All @@ -147,9 +150,18 @@ def test_idmaker_native_strings(self):
"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):
Expand Down