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 inspect
|
|
|
|
import sys
|
|
|
|
|
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
|
2009-08-10 17:27:13 +08:00
|
|
|
call2 = call.__class__(lambda: py.test.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
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
def pytest_runtest_setup(item):
|
2010-11-13 16:05:11 +08:00
|
|
|
if isinstance(item, (pytest.Function)):
|
|
|
|
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')
|
2009-08-10 17:27:13 +08:00
|
|
|
|
|
|
|
def pytest_runtest_teardown(item):
|
2010-11-13 16:05:11 +08:00
|
|
|
if isinstance(item, pytest.Function):
|
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):
|
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')
|
|
|
|
|
|
|
|
def call_optional(obj, name):
|
|
|
|
method = getattr(obj, name, None)
|
2010-09-03 18:32:12 +08:00
|
|
|
if 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
|