运行模式 主要应用于三个基础:
TestSuite
,Testcase
,Test
:
- 单个
Test
,多个Test
- 单个
Testcase
, 多个Testcase
- 单个
TestSuite
, 多个TestSuite
- 混合模式
准备测试目录文件:
test1
-- test_a.py
-- test_b.py
test2
-- test_c.py
-- test_d.py
-- main.py
test_a.py( 其他测试文件类似)
import unittest
class TestA(unittest.TestCase):
def test_1(self):
self.assertIs(1,1)
def test_2(self):
self.assertIs(1,1)
if __name__ == "__main__":
unittest.main()
main.py
import unittest, test1.main1, test2.main2,
if __name__ == "__main__":
suite = unittest.TestSuite()
suite.addTest(suite1)
suite.addTest(suite2)
runner = unittest.TextTestRunner()
runner.run(suite)
1 单个Test
runner = unittest.TextTestRunner()
case = TestA('test_1')
runner.run(case)
2 多个Test
suite = unittest.TestSuite()
suite.addTest(TestA('test_1'))
suite.addTest(TestB('test_1'))
runner = unittest.TextTestRunner()
runner.run(suite)
3 单个TestCase
runner = unittest.TextTestRunner()
runner.run(TestA)
4 多个TestCase
suite = unittest.TestSuite()
all_cases = unittest.defaultTestLoader.discover('./test', pattern='test*.py')
for case in all_cases:
suite.addTests(case)
runner = unittest.TextTestRunner()
runner.run(suite)
5 单个TestSuite,使用如上2,4
6 多个TestSuite
# 已得到suite1,suite2
suite = unittest.TestSuite()
suite.addTest(suite1)
suite.addTest(suite2)
runner = unittest.TextTestRunner()
runner.run(suite)
7 混合模式,使用TestSuite,添加不同的用例及套件即可,例子不再写了:)
报告结果截图如下
-- Github
地址 https://github.com/mingyuanHub/python-game-test
网友评论