From 82caacd6331cd849f08f7706538576a1aaf06212 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 23 Oct 2009 15:11:53 +0200 Subject: [PATCH 1/4] nosetest plugin now supports fallback to module level setup --HG-- branch : trunk --- _py/test/plugin/pytest_nose.py | 7 ++++++- testing/pytest/plugin/test_pytest_nose.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/_py/test/plugin/pytest_nose.py b/_py/test/plugin/pytest_nose.py index bc46e6caa..4ea8306d3 100644 --- a/_py/test/plugin/pytest_nose.py +++ b/_py/test/plugin/pytest_nose.py @@ -66,7 +66,9 @@ def pytest_runtest_setup(item): if isinstance(gen.parent, py.test.collect.Instance): call_optional(gen.parent.obj, 'setup') gen._nosegensetup = True - call_optional(item.obj, 'setup') + if not call_optional(item.obj, 'setup'): + # call module level setup if there is no object level one + call_optional(item.parent.obj, 'setup') def pytest_runtest_teardown(item): if isinstance(item, py.test.collect.Function): @@ -83,3 +85,6 @@ def call_optional(obj, name): method = getattr(obj, name, None) if method: method() + return True + else: + return False diff --git a/testing/pytest/plugin/test_pytest_nose.py b/testing/pytest/plugin/test_pytest_nose.py index 4950095dd..aba8f3cc6 100644 --- a/testing/pytest/plugin/test_pytest_nose.py +++ b/testing/pytest/plugin/test_pytest_nose.py @@ -85,3 +85,17 @@ def test_nose_test_generator_fixtures(testdir): ]) + +def test_module_level_setup(testdir): + testdir.makepyfile(""" + items = {} + def setup(): + items[1]=1 + + def test_setup_changed_stuff(): + assert items + """) + result = testdir.runpytest('-p', 'nose') + result.stdout.fnmatch_lines([ + "*1 passed*", + ]) From 8e5efa7d6d8fa948f18204b3d133266c770a4699 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 23 Oct 2009 15:27:59 +0200 Subject: [PATCH 2/4] better tests for the nose plugin, support module level teardown --HG-- branch : trunk --- _py/test/plugin/pytest_nose.py | 3 ++- testing/pytest/plugin/test_pytest_nose.py | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/_py/test/plugin/pytest_nose.py b/_py/test/plugin/pytest_nose.py index 4ea8306d3..f7bfbfda0 100644 --- a/_py/test/plugin/pytest_nose.py +++ b/_py/test/plugin/pytest_nose.py @@ -72,7 +72,8 @@ def pytest_runtest_setup(item): def pytest_runtest_teardown(item): if isinstance(item, py.test.collect.Function): - call_optional(item.obj, 'teardown') + if not call_optional(item.obj, 'teardown'): + call_optional(item.parent.obj, 'teardown') #if hasattr(item.parent, '_nosegensetup'): # #call_optional(item._nosegensetup, 'teardown') # del item.parent._nosegensetup diff --git a/testing/pytest/plugin/test_pytest_nose.py b/testing/pytest/plugin/test_pytest_nose.py index aba8f3cc6..bb8b80d5f 100644 --- a/testing/pytest/plugin/test_pytest_nose.py +++ b/testing/pytest/plugin/test_pytest_nose.py @@ -88,14 +88,30 @@ def test_nose_test_generator_fixtures(testdir): def test_module_level_setup(testdir): testdir.makepyfile(""" + from nose.tools import with_setup items = {} def setup(): items[1]=1 - def test_setup_changed_stuff(): - assert items + def teardown(): + del items[1] + + def setup2(): + items[2] = 2 + + def teardown2(): + del items[2] + + def test_setup_module_setup(): + assert items[1] == 1 + + @with_setup(setup2, teardown2) + def test_local_setup(): + assert items[2] == 2 + assert 1 not in items + """) result = testdir.runpytest('-p', 'nose') result.stdout.fnmatch_lines([ - "*1 passed*", + "*2 passed*", ]) From 5f3bdf2d0b5b1b5a1473b62ad14fb831ed03a245 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 23 Oct 2009 16:16:28 +0200 Subject: [PATCH 3/4] nose plugin wont call setup functions that arent made for it --HG-- branch : trunk --- _py/test/plugin/pytest_nose.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/_py/test/plugin/pytest_nose.py b/_py/test/plugin/pytest_nose.py index f7bfbfda0..61c7ef1fd 100644 --- a/_py/test/plugin/pytest_nose.py +++ b/_py/test/plugin/pytest_nose.py @@ -85,7 +85,9 @@ def pytest_make_collect_report(collector): def call_optional(obj, name): method = getattr(obj, name, None) if method: - method() - return True - else: - return False + argspec = inspect.getargspec(method) + if argspec[0] == ['self']: + argspec = argspec[1:] + if not any(argspec): + method() + return True From 6f80c985fbd3ec1fa6d80a904d2ab679dcdee06a Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 23 Oct 2009 16:17:06 +0200 Subject: [PATCH 4/4] support nose style argument-free setup/teardown functions --HG-- branch : trunk --- _py/test/pycollect.py | 19 +++++++++++++++---- testing/pytest/plugin/test_pytest_nose.py | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/_py/test/pycollect.py b/_py/test/pycollect.py index 8e6fc4111..f2fbf324a 100644 --- a/_py/test/pycollect.py +++ b/_py/test/pycollect.py @@ -161,13 +161,24 @@ class Module(py.test.collect.File, PyCollectorMixin): def setup(self): if getattr(self.obj, 'disabled', 0): py.test.skip("%r is disabled" %(self.obj,)) - mod = self.obj - if hasattr(mod, 'setup_module'): - self.obj.setup_module(mod) + if hasattr(self.obj, 'setup_module'): + #XXX: nose compat hack, move to nose plugin + # if it takes a positional arg, its probably a py.test style one + # so we pass the current module object + if inspect.getargspec(self.obj.setup_module)[0]: + self.obj.setup_module(self.obj) + else: + self.obj.setup_module() def teardown(self): if hasattr(self.obj, 'teardown_module'): - self.obj.teardown_module(self.obj) + #XXX: nose compat hack, move to nose plugin + # if it takes a positional arg, its probably a py.test style one + # so we pass the current module object + if inspect.getargspec(self.obj.teardown_module)[0]: + self.obj.teardown_module(self.obj) + else: + self.obj.teardown_module() class Class(PyCollectorMixin, py.test.collect.Collector): diff --git a/testing/pytest/plugin/test_pytest_nose.py b/testing/pytest/plugin/test_pytest_nose.py index bb8b80d5f..fea4f0cf8 100644 --- a/testing/pytest/plugin/test_pytest_nose.py +++ b/testing/pytest/plugin/test_pytest_nose.py @@ -115,3 +115,24 @@ def test_module_level_setup(testdir): result.stdout.fnmatch_lines([ "*2 passed*", ]) + +def test_nose_style_setup_teardown(testdir): + testdir.makepyfile(""" + l = [] + def setup_module(): + l.append(1) + + def teardown_module(): + del l[0] + + def test_hello(): + assert l == [1] + + def test_world(): + assert l == [1] + """) + result = testdir.runpytest('-p', 'nose') + result.stdout.fnmatch_lines([ + "*2 passed*", + ]) +