Skip to content

Issue #562 - Ensure @nose.tools.istest is respected #921

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 4 commits into from
Aug 9, 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
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
2.8.0.dev (compared to 2.7.X)
-----------------------------

- Fix #562: @nose.tools.istest now fully respected.

- 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.
Expand Down
41 changes: 33 additions & 8 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def filter_traceback(entry):


def get_real_func(obj):
"""gets the real function object of the (possibly) wrapped object by
""" gets the real function object of the (possibly) wrapped object by
functools.wraps or functools.partial.
"""
while hasattr(obj, "__wrapped__"):
Expand All @@ -64,6 +64,17 @@ def getimfunc(func):
except AttributeError:
return func

def safe_getattr(object, name, default):
""" Like getattr but return default upon any Exception.

Attribute access can potentially fail for 'evil' Python objects.
See issue214
"""
try:
return getattr(object, name, default)
except Exception:
return default


class FixtureFunctionMarker:
def __init__(self, scope, params,
Expand Down Expand Up @@ -257,11 +268,10 @@ def pytest_pycollect_makeitem(collector, name, obj):
raise StopIteration
# nothing was collected elsewhere, let's do it here
if isclass(obj):
if collector.classnamefilter(name):
if collector.istestclass(obj, name):
Class = collector._getcustomclass("Class")
outcome.force_result(Class(name, parent=collector))
elif collector.funcnamefilter(name) and hasattr(obj, "__call__") and\
getfixturemarker(obj) is None:
elif collector.istestfunction(obj, name):
# mock seems to store unbound methods (issue473), normalize it
obj = getattr(obj, "__func__", obj)
if not isfunction(obj):
Expand Down Expand Up @@ -347,9 +357,24 @@ class PyCollector(PyobjMixin, pytest.Collector):
def funcnamefilter(self, name):
return self._matches_prefix_or_glob_option('python_functions', name)

def isnosetest(self, obj):
""" Look for the __test__ attribute, which is applied by the
@nose.tools.istest decorator
"""
return safe_getattr(obj, '__test__', False)

def classnamefilter(self, name):
return self._matches_prefix_or_glob_option('python_classes', name)

def istestfunction(self, obj, name):
return (
(self.funcnamefilter(name) or self.isnosetest(obj))
and safe_getattr(obj, "__call__", False) and getfixturemarker(obj) is None
)

def istestclass(self, obj, name):
return self.classnamefilter(name) or self.isnosetest(obj)

def _matches_prefix_or_glob_option(self, option_name, name):
"""
checks if the given name matches the prefix or glob-pattern defined
Expand Down Expand Up @@ -494,7 +519,7 @@ def __init__(self, argnames, names_closure, name2fixturedefs):


def _marked(func, mark):
"""Returns True if :func: is already marked with :mark:, False orherwise.
""" Returns True if :func: is already marked with :mark:, False otherwise.
This can happen if marker is applied to class and the test file is
invoked more than once.
"""
Expand Down Expand Up @@ -1130,9 +1155,9 @@ def raises(expected_exception, *args, **kwargs):
" derived from BaseException, not %s")
if isinstance(expected_exception, tuple):
for exc in expected_exception:
if not inspect.isclass(exc):
if not isclass(exc):
raise TypeError(msg % type(exc))
elif not inspect.isclass(expected_exception):
elif not isclass(expected_exception):
raise TypeError(msg % type(expected_exception))

if not args:
Expand Down Expand Up @@ -1379,7 +1404,7 @@ def session(self):
return self._pyfuncitem.session

def addfinalizer(self, finalizer):
"""add finalizer/teardown function to be called after the
""" add finalizer/teardown function to be called after the
last test within the requesting test context finished
execution. """
# XXX usually this method is shadowed by fixturedef specific ones
Expand Down
46 changes: 46 additions & 0 deletions testing/test_nose.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,49 @@ def test_skipping():
""")
reprec = testdir.inline_run()
reprec.assertoutcome(skipped=1)

def test_istest_function_decorator(testdir):
p = testdir.makepyfile("""
import nose.tools
@nose.tools.istest
def not_test_prefix():
pass
""")
result = testdir.runpytest(p)
result.assert_outcomes(passed=1)

def test_nottest_function_decorator(testdir):
testdir.makepyfile("""
import nose.tools
@nose.tools.nottest
def test_prefix():
pass
""")
reprec = testdir.inline_run()
assert not reprec.getfailedcollections()
calls = reprec.getreports("pytest_runtest_logreport")
assert not calls

def test_istest_class_decorator(testdir):
p = testdir.makepyfile("""
import nose.tools
@nose.tools.istest
class NotTestPrefix:
def test_method(self):
pass
""")
result = testdir.runpytest(p)
result.assert_outcomes(passed=1)

def test_nottest_class_decorator(testdir):
testdir.makepyfile("""
import nose.tools
@nose.tools.nottest
class TestPrefix:
def test_method(self):
pass
""")
reprec = testdir.inline_run()
assert not reprec.getfailedcollections()
calls = reprec.getreports("pytest_runtest_logreport")
assert not calls