From f814cb53463bfc821cf45d4669161f128e269030 Mon Sep 17 00:00:00 2001 From: Ed Singleton Date: Fri, 3 Sep 2010 11:27:47 +0100 Subject: [PATCH] Added a test and fix for nose compatible setup/teardown functions that are partials, and so errors are not ignored --HG-- branch : trunk --- py/_plugin/pytest_nose.py | 11 +++--- testing/plugin/test_pytest_nose.py | 58 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/py/_plugin/pytest_nose.py b/py/_plugin/pytest_nose.py index d4fba021d..660b5f8ae 100644 --- a/py/_plugin/pytest_nose.py +++ b/py/_plugin/pytest_nose.py @@ -90,9 +90,8 @@ def pytest_make_collect_report(collector): def call_optional(obj, name): method = getattr(obj, name, None) - if method: - ismethod = inspect.ismethod(method) - rawcode = py.code.getrawcode(method) - if not rawcode.co_varnames[ismethod:rawcode.co_argcount]: - method() - return True + if method and callable(method): + # If there's any problems allow the exception to raise rather than + # silently ignoring them + method() + return True diff --git a/testing/plugin/test_pytest_nose.py b/testing/plugin/test_pytest_nose.py index 7a872c6ba..199561cf7 100644 --- a/testing/plugin/test_pytest_nose.py +++ b/testing/plugin/test_pytest_nose.py @@ -50,6 +50,64 @@ def test_nose_setup_func(testdir): ]) +def test_nose_setup_func_failure(testdir): + p = testdir.makepyfile(""" + l = [] + + my_setup = lambda x: 1 + my_teardown = lambda x: 2 + + def test_hello(): + print l + assert l == [1] + + def test_world(): + print l + assert l == [1,2] + + test_hello.setup = my_setup + test_hello.teardown = my_teardown + """) + result = testdir.runpytest(p, '-p', 'nose') + result.stdout.fnmatch_lines([ + "*TypeError: () takes exactly 1 argument (0 given)*" + ]) + + +def test_nose_setup_partial(testdir): + p = testdir.makepyfile(""" + from functools import partial + + l = [] + + def my_setup(x): + a = x + l.append(a) + + def my_teardown(x): + b = x + l.append(b) + + my_setup_partial = partial(my_setup, 1) + my_teardown_partial = partial(my_teardown, 2) + + def test_hello(): + print l + assert l == [1] + + def test_world(): + print l + assert l == [1,2] + + test_hello.setup = my_setup_partial + test_hello.teardown = my_teardown_partial + """) + result = testdir.runpytest(p, '-p', 'nose') + result.stdout.fnmatch_lines([ + "*2 passed*" + ]) + + def test_nose_test_generator_fixtures(testdir): p = testdir.makepyfile(""" # taken from nose-0.11.1 unit_tests/test_generator_fixtures.py