2009-02-27 18:18:27 +08:00
|
|
|
"""
|
2010-07-27 03:15:15 +08:00
|
|
|
automatically discover and run traditional "unittest.py" style tests.
|
2009-02-27 18:18:27 +08:00
|
|
|
"""
|
|
|
|
import py
|
2009-07-21 00:54:18 +08:00
|
|
|
import sys
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-15 21:15:40 +08:00
|
|
|
def pytest_pycollect_makeitem(collector, name, obj):
|
2009-07-21 00:54:18 +08:00
|
|
|
if 'unittest' not in sys.modules:
|
2009-10-22 00:42:40 +08:00
|
|
|
return # nobody derived unittest.TestCase
|
2009-10-09 21:09:26 +08:00
|
|
|
try:
|
|
|
|
isunit = issubclass(obj, py.std.unittest.TestCase)
|
2010-04-28 21:24:38 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
except Exception:
|
2009-10-09 21:09:26 +08:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if isunit:
|
|
|
|
return UnitTestCase(name, parent=collector)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class UnitTestCase(py.test.collect.Class):
|
|
|
|
def collect(self):
|
|
|
|
loader = py.std.unittest.TestLoader()
|
2010-11-02 06:08:16 +08:00
|
|
|
for name in loader.getTestCaseNames(self.obj):
|
|
|
|
yield TestCaseFunction(name, parent=self)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def setup(self):
|
2010-11-02 06:08:16 +08:00
|
|
|
meth = getattr(self.obj, 'setUpClass', None)
|
|
|
|
if meth is not None:
|
|
|
|
meth()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def teardown(self):
|
2010-11-02 06:08:16 +08:00
|
|
|
meth = getattr(self.obj, 'tearDownClass', None)
|
|
|
|
if meth is not None:
|
|
|
|
meth()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-11-02 06:08:16 +08:00
|
|
|
class TestCaseFunction(py.test.collect.Function):
|
|
|
|
def startTest(self, testcase):
|
|
|
|
pass
|
|
|
|
def addError(self, testcase, rawexcinfo):
|
|
|
|
py.builtin._reraise(*rawexcinfo)
|
|
|
|
def addFailure(self, testcase, rawexcinfo):
|
|
|
|
py.builtin._reraise(*rawexcinfo)
|
|
|
|
def addSuccess(self, testcase):
|
|
|
|
pass
|
|
|
|
def stopTest(self, testcase):
|
|
|
|
pass
|
|
|
|
def runtest(self):
|
|
|
|
testcase = self.parent.obj(self.name)
|
|
|
|
testcase(result=self)
|