2010-11-07 06:45:48 +08:00
|
|
|
""" discovery and running of std-library "unittest" style tests. """
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest, py
|
2013-10-02 20:32:40 +08:00
|
|
|
import sys
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2011-12-29 00:47:08 +08:00
|
|
|
# for transfering markers
|
|
|
|
from _pytest.python import transfer_markers
|
|
|
|
|
2013-08-02 05:48:40 +08:00
|
|
|
|
|
|
|
def is_unittest(obj):
|
|
|
|
"""Is obj a subclass of unittest.TestCase?"""
|
2010-11-07 06:45:48 +08:00
|
|
|
unittest = sys.modules.get('unittest')
|
|
|
|
if unittest is None:
|
2013-08-02 05:48:40 +08:00
|
|
|
return # nobody can have derived unittest.TestCase
|
2009-10-09 21:09:26 +08:00
|
|
|
try:
|
2013-08-02 05:48:40 +08:00
|
|
|
return issubclass(obj, unittest.TestCase)
|
2010-04-28 21:24:38 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
2013-08-02 05:48:40 +08:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_pycollect_makeitem(collector, name, obj):
|
|
|
|
if is_unittest(obj):
|
|
|
|
return UnitTestCase(name, parent=collector)
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
class UnitTestCase(pytest.Class):
|
2012-10-17 17:20:45 +08:00
|
|
|
nofuncargs = True # marker for fixturemanger.getfixtureinfo()
|
|
|
|
# to declare that our children do not support funcargs
|
2013-08-02 15:52:40 +08:00
|
|
|
#
|
|
|
|
def setup(self):
|
|
|
|
cls = self.obj
|
|
|
|
if getattr(cls, '__unittest_skip__', False):
|
|
|
|
return # skipped
|
|
|
|
setup = getattr(cls, 'setUpClass', None)
|
|
|
|
if setup is not None:
|
|
|
|
setup()
|
|
|
|
teardown = getattr(cls, 'tearDownClass', None)
|
|
|
|
if teardown is not None:
|
|
|
|
self.addfinalizer(teardown)
|
|
|
|
super(UnitTestCase, self).setup()
|
2012-10-16 19:48:00 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def collect(self):
|
2012-10-16 22:13:12 +08:00
|
|
|
self.session._fixturemanager.parsefactories(self, unittest=True)
|
2009-02-27 18:18:27 +08:00
|
|
|
loader = py.std.unittest.TestLoader()
|
2011-12-29 00:47:08 +08:00
|
|
|
module = self.getparent(pytest.Module).obj
|
|
|
|
cls = self.obj
|
2012-10-22 22:25:09 +08:00
|
|
|
foundsomething = False
|
2010-11-02 06:08:16 +08:00
|
|
|
for name in loader.getTestCaseNames(self.obj):
|
2011-12-29 00:47:08 +08:00
|
|
|
x = getattr(self.obj, name)
|
|
|
|
funcobj = getattr(x, 'im_func', x)
|
|
|
|
transfer_markers(funcobj, cls, module)
|
2010-11-02 06:08:16 +08:00
|
|
|
yield TestCaseFunction(name, parent=self)
|
2012-10-22 22:25:09 +08:00
|
|
|
foundsomething = True
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2012-10-22 22:25:09 +08:00
|
|
|
if not foundsomething:
|
2012-10-23 01:22:01 +08:00
|
|
|
runtest = getattr(self.obj, 'runTest', None)
|
|
|
|
if runtest is not None:
|
|
|
|
ut = sys.modules.get("twisted.trial.unittest", None)
|
|
|
|
if ut is None or runtest != ut.TestCase.runTest:
|
|
|
|
yield TestCaseFunction('runTest', parent=self)
|
2012-09-23 00:24:53 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
class TestCaseFunction(pytest.Function):
|
2010-11-23 22:42:23 +08:00
|
|
|
_excinfo = None
|
2010-11-24 23:17:49 +08:00
|
|
|
|
2010-11-07 08:10:15 +08:00
|
|
|
def setup(self):
|
2010-11-24 23:17:49 +08:00
|
|
|
self._testcase = self.parent.obj(self.name)
|
|
|
|
self._obj = getattr(self._testcase, self.name)
|
|
|
|
if hasattr(self._testcase, 'setup_method'):
|
|
|
|
self._testcase.setup_method(self._obj)
|
2012-09-18 16:54:12 +08:00
|
|
|
if hasattr(self, "_request"):
|
2012-10-05 20:24:44 +08:00
|
|
|
self._request._fillfixtures()
|
2010-11-24 23:17:49 +08:00
|
|
|
|
2010-11-07 08:10:15 +08:00
|
|
|
def teardown(self):
|
2010-11-24 23:17:49 +08:00
|
|
|
if hasattr(self._testcase, 'teardown_method'):
|
|
|
|
self._testcase.teardown_method(self._obj)
|
|
|
|
|
2010-11-02 06:08:16 +08:00
|
|
|
def startTest(self, testcase):
|
|
|
|
pass
|
2010-11-23 22:42:23 +08:00
|
|
|
|
|
|
|
def _addexcinfo(self, rawexcinfo):
|
2010-11-24 18:48:55 +08:00
|
|
|
# unwrap potential exception info (see twisted trial support below)
|
|
|
|
rawexcinfo = getattr(rawexcinfo, '_rawexcinfo', rawexcinfo)
|
2010-11-23 22:42:23 +08:00
|
|
|
try:
|
2010-11-24 21:35:04 +08:00
|
|
|
excinfo = py.code.ExceptionInfo(rawexcinfo)
|
2010-11-23 22:42:23 +08:00
|
|
|
except TypeError:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
l = py.std.traceback.format_exception(*rawexcinfo)
|
|
|
|
l.insert(0, "NOTE: Incompatible Exception Representation, "
|
|
|
|
"displaying natively:\n\n")
|
|
|
|
pytest.fail("".join(l), pytrace=False)
|
|
|
|
except (pytest.fail.Exception, KeyboardInterrupt):
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
pytest.fail("ERROR: Unknown Incompatible Exception "
|
|
|
|
"representation:\n%r" %(rawexcinfo,), pytrace=False)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
2010-11-24 18:48:55 +08:00
|
|
|
except pytest.fail.Exception:
|
2010-11-24 21:35:04 +08:00
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
self.__dict__.setdefault('_excinfo', []).append(excinfo)
|
2010-11-23 22:42:23 +08:00
|
|
|
|
2010-11-02 06:08:16 +08:00
|
|
|
def addError(self, testcase, rawexcinfo):
|
2010-11-23 22:42:23 +08:00
|
|
|
self._addexcinfo(rawexcinfo)
|
2010-11-02 06:08:16 +08:00
|
|
|
def addFailure(self, testcase, rawexcinfo):
|
2010-11-23 22:42:23 +08:00
|
|
|
self._addexcinfo(rawexcinfo)
|
2012-03-20 13:53:52 +08:00
|
|
|
|
2010-11-25 22:48:59 +08:00
|
|
|
def addSkip(self, testcase, reason):
|
|
|
|
try:
|
|
|
|
pytest.skip(reason)
|
|
|
|
except pytest.skip.Exception:
|
|
|
|
self._addexcinfo(sys.exc_info())
|
2012-03-20 13:53:52 +08:00
|
|
|
|
|
|
|
def addExpectedFailure(self, testcase, rawexcinfo, reason=""):
|
2010-11-25 22:48:59 +08:00
|
|
|
try:
|
|
|
|
pytest.xfail(str(reason))
|
|
|
|
except pytest.xfail.Exception:
|
|
|
|
self._addexcinfo(sys.exc_info())
|
2012-03-20 13:53:52 +08:00
|
|
|
|
|
|
|
def addUnexpectedSuccess(self, testcase, reason=""):
|
|
|
|
self._unexpectedsuccess = reason
|
|
|
|
|
2010-11-02 06:08:16 +08:00
|
|
|
def addSuccess(self, testcase):
|
|
|
|
pass
|
2012-03-20 13:53:52 +08:00
|
|
|
|
2010-11-02 06:08:16 +08:00
|
|
|
def stopTest(self, testcase):
|
|
|
|
pass
|
2012-03-20 13:53:52 +08:00
|
|
|
|
2010-11-02 06:08:16 +08:00
|
|
|
def runtest(self):
|
2010-11-24 23:17:49 +08:00
|
|
|
self._testcase(result=self)
|
2010-11-23 22:42:23 +08:00
|
|
|
|
2011-03-06 00:49:51 +08:00
|
|
|
def _prunetraceback(self, excinfo):
|
|
|
|
pytest.Function._prunetraceback(self, excinfo)
|
2011-12-19 07:01:39 +08:00
|
|
|
traceback = excinfo.traceback.filter(
|
|
|
|
lambda x:not x.frame.f_globals.get('__unittest'))
|
|
|
|
if traceback:
|
|
|
|
excinfo.traceback = traceback
|
2011-03-06 00:49:51 +08:00
|
|
|
|
2010-11-23 22:42:23 +08:00
|
|
|
@pytest.mark.tryfirst
|
|
|
|
def pytest_runtest_makereport(item, call):
|
|
|
|
if isinstance(item, TestCaseFunction):
|
2010-11-24 23:17:49 +08:00
|
|
|
if item._excinfo:
|
2010-11-24 21:35:04 +08:00
|
|
|
call.excinfo = item._excinfo.pop(0)
|
2013-09-09 15:56:53 +08:00
|
|
|
try:
|
|
|
|
del call.result
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2010-11-24 18:48:55 +08:00
|
|
|
|
|
|
|
# twisted trial support
|
|
|
|
def pytest_runtest_protocol(item, __multicall__):
|
|
|
|
if isinstance(item, TestCaseFunction):
|
|
|
|
if 'twisted.trial.unittest' in sys.modules:
|
|
|
|
ut = sys.modules['twisted.python.failure']
|
|
|
|
Failure__init__ = ut.Failure.__init__.im_func
|
|
|
|
check_testcase_implements_trial_reporter()
|
2011-11-19 00:34:46 +08:00
|
|
|
def excstore(self, exc_value=None, exc_type=None, exc_tb=None,
|
|
|
|
captureVars=None):
|
2010-11-24 18:48:55 +08:00
|
|
|
if exc_value is None:
|
|
|
|
self._rawexcinfo = sys.exc_info()
|
|
|
|
else:
|
2010-11-24 20:54:56 +08:00
|
|
|
if exc_type is None:
|
|
|
|
exc_type = type(exc_value)
|
|
|
|
self._rawexcinfo = (exc_type, exc_value, exc_tb)
|
2011-11-19 00:34:46 +08:00
|
|
|
try:
|
|
|
|
Failure__init__(self, exc_value, exc_type, exc_tb,
|
|
|
|
captureVars=captureVars)
|
|
|
|
except TypeError:
|
|
|
|
Failure__init__(self, exc_value, exc_type, exc_tb)
|
2010-11-24 18:48:55 +08:00
|
|
|
ut.Failure.__init__ = excstore
|
|
|
|
try:
|
|
|
|
return __multicall__.execute()
|
|
|
|
finally:
|
|
|
|
ut.Failure.__init__ = Failure__init__
|
|
|
|
|
|
|
|
def check_testcase_implements_trial_reporter(done=[]):
|
|
|
|
if done:
|
|
|
|
return
|
|
|
|
from zope.interface import classImplements
|
|
|
|
from twisted.trial.itrial import IReporter
|
|
|
|
classImplements(TestCaseFunction, IReporter)
|
|
|
|
done.append(1)
|