我需要有一个运行 setup/teardown 的基类,然后是继承的子类,以便允许所有测试运行,无论某些测试是否失败。当尝试让多个子类导入一个基类时,我看到第二个子类的导入错误。我有以下文件:
runtestcases.py
testcase1.py
testcase2.py
这是runtestcases.py:
import unittest
import testcase1, testcase2
class ArithTestSuper (unittest.TestCase):
def setUp (self):
print("Setting up ArithTest cases")
def tearDown (self):
print("Cleaning up ArithTest cases")
def my_suite():
suite = unittest.TestSuite()
suite.addTest (testcase1.ArithTest())
suite.addTest (testcase2.ArithTestFail())
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
test_suite = my_suite()
runner.run (test_suite)
这是testcase1.py:
from runtestcases import ArithTestSuper
class ArithTest (ArithTestSuper):
def runTest (self):
""" Test addition and succeed. """
print("Running ArithTest")
self.failUnless (1+1==2, 'one plus one fails!')
self.failIf (1+1 != 2, 'one plus one fails again!')
self.failUnlessEqual (1+1, 2, 'more trouble with one plus one!')
这是testcase2.py:
from runtestcases import ArithTestSuper
class ArithTestFail (ArithTestSuper):
def runTest (self):
""" Test addition and fail. """
print("Running ArithTestFail")
self.failUnless (1+1==2, 'one plus one fails!')
self.failIf (1+1 != 2, 'one plus one fails again!')
self.failUnlessEqual (1+1, 2, 'more trouble with one plus one!')
self.failIfEqual (1+1, 2, 'expected failure here')
self.failIfEqual (1+1, 2, 'second failure')
Compilation fails on testcase2.py with:
ImportError: cannot import name ArithTestSuper
如果我只运行 testcase1 它可以工作 - 同样,只运行 testcase2 也可以。如果我尝试这两种错误都会发生。想法?