我有 2 个模块:test1.py 和 test2.py。
test1.py
import unittest
class ArithTest (unittest.TestCase):
def test_run (self):
""" Test addition and succeed. """
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!')
if __name__ == '__main__':
unittest.main()
test2.py
import unittest
class AlgTest (unittest.TestCase):
def test_alg (self):
""" Test addition and succeed. """
self.assertEqual(1+1, 2, '1+1 != 2? whaaat?')
self.assertEqual(6-5, 1, '6-5 != 5 wft python?')
if __name__ == '__main__':
unittest.main()
-现在- 我想创建一个新模块 test3.py 来测试 test1.py 和 test2.py。我现在不知道如何,我在互联网上阅读了有关套装测试的信息,但我不明白。 我不想再创建一种调用测试的方法,并在 test3.py 上调用它们。 我想将它们分组并调用 test3.py,它们作为 unitests 运行
请您参考如下方法:
测试1.py
import unittest
class ArithTest (unittest.TestCase):
def test_run (self):
""" Test addition and succeed. """
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!')
def runTest(self):
self.test_run()
if __name__ == '__main__':
unittest.main()
测试2.py
import unittest
class AlgTest (unittest.TestCase):
def test_alg (self):
""" Test addition and succeed. """
self.assertEqual(1+1, 2, '1+1 != 2? whaaat?')
self.assertEqual(6-5, 1, '6-5 != 5 wft python?')
def runTest(self):
self.test_alg()
if __name__ == '__main__':
unittest.main()
测试3.py
from .test1 import ArithTest
from .test2 import AlgTest
import unittest
def suite_2():
suite = unittest.TestSuite()
suite.addTest(ArithTest())
suite.addTest(AlgTest())
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
test_suite = suite_2()
runner.run(test_suite)
同时添加一个__init__.py
使用 python3 -m folder_name.test3 运行它
