From 94b1ce65c636d59511d1f6d457cc39459c6d4856 Mon Sep 17 00:00:00 2001 From: schlamar Date: Fri, 14 Mar 2014 14:04:54 +0100 Subject: [PATCH 1/3] Fixed race condition with SkipTest when module not in sys.modules on collection. --- _pytest/main.py | 4 ---- _pytest/nose.py | 30 ++++++++++++++++++++---------- _pytest/runner.py | 4 +++- testing/test_nose.py | 16 +++++++++++++++- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/_pytest/main.py b/_pytest/main.py index f7060bf6f..23a5430f0 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -408,10 +408,6 @@ class Collector(Node): and thus iteratively build a tree. """ - # the set of exceptions to interpret as "Skip the whole module" during - # collection - skip_exceptions = (Skipped,) - class CollectError(Exception): """ an error during collection, contains a custom message. """ diff --git a/_pytest/nose.py b/_pytest/nose.py index ecfc2316b..3b060f8bc 100644 --- a/_pytest/nose.py +++ b/_pytest/nose.py @@ -1,17 +1,32 @@ """ run test suites written for nose. """ -import pytest, py import sys + +import py +import pytest from _pytest import unittest + +def get_skip_exceptions(): + skip_classes = set() + for module_name in ('unittest', 'unittest2', 'nose'): + mod = sys.modules.get(module_name) + if hasattr(mod, 'SkipTest'): + skip_classes.add(mod.SkipTest) + return tuple(skip_classes) + + def pytest_runtest_makereport(__multicall__, item, call): - SkipTest = getattr(sys.modules.get('nose', None), 'SkipTest', None) - if SkipTest: - if call.excinfo and call.excinfo.errisinstance(SkipTest): + if not call.excinfo: + return + + for skip_exc in get_skip_exceptions(): + if call.excinfo.errisinstance(skip_exc): # let's substitute the excinfo with a pytest.skip one call2 = call.__class__(lambda: pytest.skip(str(call.excinfo.value)), call.when) call.excinfo = call2.excinfo + return @pytest.mark.trylast @@ -38,13 +53,8 @@ def teardown_nose(item): # #call_optional(item._nosegensetup, 'teardown') # del item.parent._nosegensetup + def pytest_make_collect_report(collector): - SkipTest = getattr(sys.modules.get('unittest', None), 'SkipTest', None) - if SkipTest is not None: - collector.skip_exceptions += (SkipTest,) - SkipTest = getattr(sys.modules.get('nose', None), 'SkipTest', None) - if SkipTest is not None: - collector.skip_exceptions += (SkipTest,) if isinstance(collector, pytest.Generator): call_optional(collector.obj, 'setup') diff --git a/_pytest/runner.py b/_pytest/runner.py index 539248117..68719765b 100644 --- a/_pytest/runner.py +++ b/_pytest/runner.py @@ -267,7 +267,9 @@ def pytest_make_collect_report(collector): if not call.excinfo: outcome = "passed" else: - if call.excinfo.errisinstance(collector.skip_exceptions): + from _pytest import nose + skip_exceptions = (Skipped,) + nose.get_skip_exceptions() + if call.excinfo.errisinstance(skip_exceptions): outcome = "skipped" r = collector._repr_failure_py(call.excinfo, "line").reprcrash longrepr = (str(r.path), r.lineno, r.message) diff --git a/testing/test_nose.py b/testing/test_nose.py index 1eda30449..fac7a5d93 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -330,12 +330,26 @@ def test_setup_teardown_linking_issue265(testdir): reprec = testdir.inline_run() reprec.assertoutcome(passed=1, skipped=1) + def test_SkipTest_during_collection(testdir): - testdir.makepyfile(""" + p = testdir.makepyfile(""" import nose raise nose.SkipTest("during collection") def test_failing(): assert False """) + result = testdir.runpytest(p) + outcome = result.parseoutcomes() + outcome.pop('seconds') + assert outcome == dict(skipped=1) + + +def test_SkipTest_in_test(testdir): + testdir.makepyfile(""" + import nose + + def test_skipping(): + raise nose.SkipTest("in test") + """) reprec = testdir.inline_run() reprec.assertoutcome(skipped=1) From 77e1f93ca11c7bc886ba4956187b24b7380e655c Mon Sep 17 00:00:00 2001 From: schlamar Date: Fri, 14 Mar 2014 14:25:36 +0100 Subject: [PATCH 2/3] Fixed pyflakes errors. --- _pytest/main.py | 2 +- testing/python/collect.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/_pytest/main.py b/_pytest/main.py index 23a5430f0..de69d351b 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -8,7 +8,7 @@ try: except ImportError: from UserDict import DictMixin as MappingMixin -from _pytest.runner import collect_one_node, Skipped +from _pytest.runner import collect_one_node tracebackcutdir = py.path.local(_pytest.__file__).dirpath() diff --git a/testing/python/collect.py b/testing/python/collect.py index 1e911cf19..e0fb5aaf7 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -277,7 +277,7 @@ class TestFunction: assert hasattr(modcol.obj, 'test_func') def test_function_as_object_instance_ignored(self, testdir): - item = testdir.makepyfile(""" + testdir.makepyfile(""" class A: def __call__(self, tmpdir): 0/0 From 85e7b11ef56c25549767114313c13efc4f0ee86b Mon Sep 17 00:00:00 2001 From: schlamar Date: Fri, 14 Mar 2014 15:29:42 +0100 Subject: [PATCH 3/3] Removed unnecessary iteration in nose.pytest_runtest_makereport. --- _pytest/nose.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/_pytest/nose.py b/_pytest/nose.py index 3b060f8bc..203db9850 100644 --- a/_pytest/nose.py +++ b/_pytest/nose.py @@ -17,16 +17,11 @@ def get_skip_exceptions(): def pytest_runtest_makereport(__multicall__, item, call): - if not call.excinfo: - return - - for skip_exc in get_skip_exceptions(): - if call.excinfo.errisinstance(skip_exc): - # let's substitute the excinfo with a pytest.skip one - call2 = call.__class__(lambda: - pytest.skip(str(call.excinfo.value)), call.when) - call.excinfo = call2.excinfo - return + if call.excinfo and call.excinfo.errisinstance(get_skip_exceptions()): + # let's substitute the excinfo with a pytest.skip one + call2 = call.__class__(lambda: + pytest.skip(str(call.excinfo.value)), call.when) + call.excinfo = call2.excinfo @pytest.mark.trylast