2010-11-07 06:45:48 +08:00
|
|
|
""" discovery and running of std-library "unittest" style tests. """
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2013-10-02 20:32:40 +08:00
|
|
|
import sys
|
2015-11-27 22:43:01 +08:00
|
|
|
import traceback
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2017-02-15 23:00:18 +08:00
|
|
|
# for transferring markers
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2017-03-17 03:51:22 +08:00
|
|
|
from _pytest.config import hookimpl
|
2015-09-19 07:03:05 +08:00
|
|
|
from _pytest.outcomes import fail, skip, xfail
|
2017-03-17 03:51:22 +08:00
|
|
|
from _pytest.python import transfer_markers, Class, Module, Function
|
2015-09-19 07:03:05 +08:00
|
|
|
from _pytest.skipping import MarkEvaluator
|
2011-12-29 00:47:08 +08:00
|
|
|
|
2013-08-02 05:48:40 +08:00
|
|
|
|
|
|
|
def pytest_pycollect_makeitem(collector, name, obj):
|
2014-08-01 16:12:53 +08:00
|
|
|
# has unittest been imported and is obj a subclass of its TestCase?
|
|
|
|
try:
|
|
|
|
if not issubclass(obj, sys.modules["unittest"].TestCase):
|
|
|
|
return
|
|
|
|
except Exception:
|
|
|
|
return
|
|
|
|
# yes, so let's collect it
|
|
|
|
return UnitTestCase(name, parent=collector)
|
2013-08-02 05:48:40 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2017-03-17 03:51:22 +08:00
|
|
|
class UnitTestCase(Class):
|
2016-01-15 07:01:07 +08:00
|
|
|
# marker for fixturemanger.getfixtureinfo()
|
|
|
|
# to declare that our children do not support funcargs
|
|
|
|
nofuncargs = True
|
2017-03-17 03:51:22 +08:00
|
|
|
|
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):
|
2014-08-01 16:12:53 +08:00
|
|
|
from unittest import TestLoader
|
2014-04-10 18:46:27 +08:00
|
|
|
cls = self.obj
|
|
|
|
if not getattr(cls, "__test__", True):
|
|
|
|
return
|
2012-10-16 22:13:12 +08:00
|
|
|
self.session._fixturemanager.parsefactories(self, unittest=True)
|
2014-08-01 16:12:53 +08:00
|
|
|
loader = TestLoader()
|
2017-03-17 03:51:22 +08:00
|
|
|
module = self.getparent(Module).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)
|
2016-05-19 01:09:58 +08:00
|
|
|
if not getattr(x, '__test__', True):
|
|
|
|
continue
|
2011-12-29 00:47:08 +08:00
|
|
|
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
|
|
|
|
2017-03-17 03:51:22 +08:00
|
|
|
class TestCaseFunction(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)
|
2015-11-05 05:54:18 +08:00
|
|
|
self._fix_unittest_skip_decorator()
|
2010-11-24 23:17:49 +08:00
|
|
|
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
|
|
|
|
2015-11-05 05:54:18 +08:00
|
|
|
def _fix_unittest_skip_decorator(self):
|
|
|
|
"""
|
|
|
|
The @unittest.skip decorator calls functools.wraps(self._testcase)
|
|
|
|
The call to functools.wraps() fails unless self._testcase
|
|
|
|
has a __name__ attribute. This is usually automatically supplied
|
|
|
|
if the test is a function or method, but we need to add manually
|
|
|
|
here.
|
|
|
|
|
|
|
|
See issue #1169
|
|
|
|
"""
|
|
|
|
if sys.version_info[0] == 2:
|
|
|
|
setattr(self._testcase, "__name__", self.name)
|
|
|
|
|
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)
|
2016-11-08 10:24:10 +08:00
|
|
|
# Allow garbage collection on TestCase instance attributes.
|
|
|
|
self._testcase = None
|
|
|
|
self._obj = None
|
2010-11-24 23:17:49 +08:00
|
|
|
|
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:
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo(rawexcinfo)
|
2010-11-23 22:42:23 +08:00
|
|
|
except TypeError:
|
|
|
|
try:
|
|
|
|
try:
|
2014-08-01 06:13:40 +08:00
|
|
|
l = traceback.format_exception(*rawexcinfo)
|
2010-11-23 22:42:23 +08:00
|
|
|
l.insert(0, "NOTE: Incompatible Exception Representation, "
|
2017-03-17 03:51:22 +08:00
|
|
|
"displaying natively:\n\n")
|
|
|
|
fail("".join(l), pytrace=False)
|
|
|
|
except (fail.Exception, KeyboardInterrupt):
|
2010-11-23 22:42:23 +08:00
|
|
|
raise
|
|
|
|
except:
|
2017-03-17 03:51:22 +08:00
|
|
|
fail("ERROR: Unknown Incompatible Exception "
|
|
|
|
"representation:\n%r" % (rawexcinfo,), pytrace=False)
|
2010-11-23 22:42:23 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
2017-03-17 03:51:22 +08:00
|
|
|
except fail.Exception:
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo()
|
2010-11-24 21:35:04 +08:00
|
|
|
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)
|
2017-03-17 03:51:22 +08:00
|
|
|
|
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:
|
2017-03-17 03:51:22 +08:00
|
|
|
skip(reason)
|
|
|
|
except skip.Exception:
|
2015-06-19 09:26:35 +08:00
|
|
|
self._evalskip = MarkEvaluator(self, 'SkipTest')
|
|
|
|
self._evalskip.result = True
|
2010-11-25 22:48:59 +08:00
|
|
|
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:
|
2017-03-17 03:51:22 +08:00
|
|
|
xfail(str(reason))
|
|
|
|
except xfail.Exception:
|
2010-11-25 22:48:59 +08:00
|
|
|
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
|
|
|
|
2017-02-02 12:01:51 +08:00
|
|
|
def _handle_skip(self):
|
|
|
|
# implements the skipping machinery (see #2137)
|
|
|
|
# analog to pythons Lib/unittest/case.py:run
|
|
|
|
testMethod = getattr(self._testcase, self._testcase._testMethodName)
|
|
|
|
if (getattr(self._testcase.__class__, "__unittest_skip__", False) or
|
2017-07-17 07:25:07 +08:00
|
|
|
getattr(testMethod, "__unittest_skip__", False)):
|
2017-02-02 12:01:51 +08:00
|
|
|
# If the class or method was skipped.
|
|
|
|
skip_why = (getattr(self._testcase.__class__, '__unittest_skip_why__', '') or
|
|
|
|
getattr(testMethod, '__unittest_skip_why__', ''))
|
|
|
|
try: # PY3, unittest2 on PY2
|
|
|
|
self._testcase._addSkip(self, self._testcase, skip_why)
|
|
|
|
except TypeError: # PY2
|
|
|
|
if sys.version_info[0] != 2:
|
|
|
|
raise
|
|
|
|
self._testcase._addSkip(self, skip_why)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2010-11-02 06:08:16 +08:00
|
|
|
def runtest(self):
|
2016-08-31 03:55:49 +08:00
|
|
|
if self.config.pluginmanager.get_plugin("pdbinvoke") is None:
|
|
|
|
self._testcase(result=self)
|
|
|
|
else:
|
2016-09-01 04:33:47 +08:00
|
|
|
# disables tearDown and cleanups for post mortem debugging (see #1890)
|
2017-02-02 12:01:51 +08:00
|
|
|
if self._handle_skip():
|
2017-01-31 04:20:12 +08:00
|
|
|
return
|
2016-08-31 03:55:49 +08:00
|
|
|
self._testcase.debug()
|
|
|
|
|
2011-03-06 00:49:51 +08:00
|
|
|
def _prunetraceback(self, excinfo):
|
2017-03-17 03:51:22 +08:00
|
|
|
Function._prunetraceback(self, excinfo)
|
2011-12-19 07:01:39 +08:00
|
|
|
traceback = excinfo.traceback.filter(
|
2017-03-17 03:51:22 +08:00
|
|
|
lambda x: not x.frame.f_globals.get('__unittest'))
|
2011-12-19 07:01:39 +08:00
|
|
|
if traceback:
|
|
|
|
excinfo.traceback = traceback
|
2011-03-06 00:49:51 +08:00
|
|
|
|
2017-03-17 03:51:22 +08:00
|
|
|
|
|
|
|
@hookimpl(tryfirst=True)
|
2010-11-23 22:42:23 +08:00
|
|
|
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
|
2014-10-09 02:23:40 +08:00
|
|
|
|
2017-03-17 03:51:22 +08:00
|
|
|
|
|
|
|
@hookimpl(hookwrapper=True)
|
2014-10-09 02:23:40 +08:00
|
|
|
def pytest_runtest_protocol(item):
|
|
|
|
if isinstance(item, TestCaseFunction) and \
|
|
|
|
'twisted.trial.unittest' in sys.modules:
|
|
|
|
ut = sys.modules['twisted.python.failure']
|
|
|
|
Failure__init__ = ut.Failure.__init__
|
|
|
|
check_testcase_implements_trial_reporter()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-09 02:23:40 +08:00
|
|
|
def excstore(self, exc_value=None, exc_type=None, exc_tb=None,
|
2017-07-17 07:25:07 +08:00
|
|
|
captureVars=None):
|
2014-10-09 02:23:40 +08:00
|
|
|
if exc_value is None:
|
|
|
|
self._rawexcinfo = sys.exc_info()
|
|
|
|
else:
|
|
|
|
if exc_type is None:
|
|
|
|
exc_type = type(exc_value)
|
|
|
|
self._rawexcinfo = (exc_type, exc_value, exc_tb)
|
2010-11-24 18:48:55 +08:00
|
|
|
try:
|
2014-10-09 02:23:40 +08:00
|
|
|
Failure__init__(self, exc_value, exc_type, exc_tb,
|
2017-07-17 07:25:07 +08:00
|
|
|
captureVars=captureVars)
|
2014-10-09 02:23:40 +08:00
|
|
|
except TypeError:
|
|
|
|
Failure__init__(self, exc_value, exc_type, exc_tb)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-09 02:23:40 +08:00
|
|
|
ut.Failure.__init__ = excstore
|
|
|
|
yield
|
|
|
|
ut.Failure.__init__ = Failure__init__
|
|
|
|
else:
|
|
|
|
yield
|
|
|
|
|
2010-11-24 18:48:55 +08:00
|
|
|
|
|
|
|
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)
|