2010-11-18 21:56:16 +08:00
|
|
|
""" run test suites written for nose. """
|
2009-08-10 17:27:13 +08:00
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest, py
|
2009-08-10 17:27:13 +08:00
|
|
|
import sys
|
2013-02-08 00:53:13 +08:00
|
|
|
from _pytest import unittest
|
|
|
|
|
2009-08-12 01:00:41 +08:00
|
|
|
def pytest_runtest_makereport(__multicall__, item, call):
|
2009-08-10 17:27:13 +08:00
|
|
|
SkipTest = getattr(sys.modules.get('nose', None), 'SkipTest', None)
|
|
|
|
if SkipTest:
|
|
|
|
if call.excinfo and call.excinfo.errisinstance(SkipTest):
|
2010-07-27 03:15:15 +08:00
|
|
|
# let's substitute the excinfo with a py.test.skip one
|
2013-09-06 18:48:54 +08:00
|
|
|
call2 = call.__class__(lambda:
|
|
|
|
pytest.skip(str(call.excinfo.value)), call.when)
|
2010-07-27 03:15:15 +08:00
|
|
|
call.excinfo = call2.excinfo
|
2009-08-10 17:27:13 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2011-07-06 03:23:59 +08:00
|
|
|
@pytest.mark.trylast
|
2009-08-10 17:27:13 +08:00
|
|
|
def pytest_runtest_setup(item):
|
2013-02-08 00:53:13 +08:00
|
|
|
if is_potential_nosetest(item):
|
2010-11-13 16:05:11 +08:00
|
|
|
if isinstance(item.parent, pytest.Generator):
|
2010-07-27 03:15:15 +08:00
|
|
|
gen = item.parent
|
2009-08-10 17:27:13 +08:00
|
|
|
if not hasattr(gen, '_nosegensetup'):
|
|
|
|
call_optional(gen.obj, 'setup')
|
2010-11-13 16:05:11 +08:00
|
|
|
if isinstance(gen.parent, pytest.Instance):
|
2009-08-10 17:27:13 +08:00
|
|
|
call_optional(gen.parent.obj, 'setup')
|
|
|
|
gen._nosegensetup = True
|
2009-10-23 21:11:53 +08:00
|
|
|
if not call_optional(item.obj, 'setup'):
|
|
|
|
# call module level setup if there is no object level one
|
|
|
|
call_optional(item.parent.obj, 'setup')
|
2013-03-25 17:52:02 +08:00
|
|
|
#XXX this implies we only call teardown when setup worked
|
|
|
|
item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item)
|
2009-08-10 17:27:13 +08:00
|
|
|
|
2013-03-25 17:52:02 +08:00
|
|
|
def teardown_nose(item):
|
2013-02-08 00:53:13 +08:00
|
|
|
if is_potential_nosetest(item):
|
2009-10-23 21:27:59 +08:00
|
|
|
if not call_optional(item.obj, 'teardown'):
|
|
|
|
call_optional(item.parent.obj, 'teardown')
|
2009-08-10 17:27:13 +08:00
|
|
|
#if hasattr(item.parent, '_nosegensetup'):
|
|
|
|
# #call_optional(item._nosegensetup, 'teardown')
|
|
|
|
# del item.parent._nosegensetup
|
|
|
|
|
|
|
|
def pytest_make_collect_report(collector):
|
2013-09-06 18:48:54 +08:00
|
|
|
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,)
|
2010-11-13 16:05:11 +08:00
|
|
|
if isinstance(collector, pytest.Generator):
|
2009-08-10 17:27:13 +08:00
|
|
|
call_optional(collector.obj, 'setup')
|
|
|
|
|
2013-02-08 00:53:13 +08:00
|
|
|
|
|
|
|
def is_potential_nosetest(item):
|
|
|
|
# extra check needed since we do not do nose style setup/teardown
|
|
|
|
# on direct unittest style classes
|
|
|
|
return isinstance(item, pytest.Function) and \
|
|
|
|
not isinstance(item, unittest.TestCaseFunction)
|
|
|
|
|
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
def call_optional(obj, name):
|
|
|
|
method = getattr(obj, name, None)
|
2013-02-07 17:41:07 +08:00
|
|
|
isfixture = hasattr(method, "_pytestfixturefunction")
|
|
|
|
if method is not None and not isfixture and py.builtin.callable(method):
|
2010-09-03 18:27:47 +08:00
|
|
|
# If there's any problems allow the exception to raise rather than
|
|
|
|
# silently ignoring them
|
|
|
|
method()
|
|
|
|
return True
|