test suite
测试套件, 理解成测试用例集
一系列的测试用例, 或测试套件, 理解成测试用例的集合和测试套件的集合
当运行测试套件时, 则运行里面添加的所有测试用例
test runner
测试运行器
用于执行和输出结果的组件
test suite,test runner 基础使用
单元测试类
- # 创建单元测试类, 继承 unittest.TestCase
- class testCase(unittest.TestCase):
- # 测试 case
- def test_01(self):
- print("test01")
- def test_03(self):
- print("test03")
- def test_04(self):
- print("test04")
- def test_05(self):
- print("test05")
主函数
- if __name__ == '__main__':
- # 实例化测试套件
- suite = unittest.TestSuite()
- # 实例化第二个测试套件
- suite1 = unittest.TestSuite()
- # 添加测试用例 - 方式一
- suite.addTest(testCase('test_03'))
- suite.addTest(testCase('test_01'))
- suite1.addTest(testCase('test_03'))
- suite1.addTest(testCase('test_01'))
- # 添加测试用例 - 方式二
- testcase = (testCase('test_05'), testCase('test_04'))
- suite.addTests(testcase)
- # 测试套件添加测试套件
- suite.addTest(suite1)
- # 实例化 TextTestRunner 类
- runner = unittest.TextTestRunner()
- # 运行测试套件
- runner.run(suite)
运行结果
- test03
- test01
- test05
- test04
- test03
- test01
- ......
- ----------------------------------------------------------------------
- Ran 6 tests in 0.000s
- OK
包含知识点
使用测试套件时, 测试用例的执行顺序可以自定义, 按照添加的顺序执行
有两种添加测试用例的方式, 推荐方式二, 代码更少更快捷
addTests(tests) , 传入的 tests 可以是 list,tuple,set
添加的测试用例格式是: 单元测试类名 (测试用例名)
使用测试套件执行测试用例的大致步骤是: 实例化 TestSuite - 添加测试用例 - 实例化 TextTestRunner - 运行测试套件
测试套件也可以添加测试套件
测试用例批量执行
单元测试类文件
前三个文件是包含了单元测试类的文件, 第四个文件是负责运行所有单元测试类, 不包含测试用例
列举某个单元测试类文件代码
- # 创建单元测试类, 继承 unittest.TestCase
- class testCase02(unittest.TestCase):
- # 测试 case
- def test_07(self):
- print("testCase02 test07")
- def test_06(self):
- print("testCase02 test06")
- def test_11(self):
- print("testCase02 test11")
test_run.py 代码
批量运行测试用例方式一:
- import unittest
- from learn.unittestLearning import test_case02
- from learn.unittestLearning.test_case03 import testCase03
- if __name__ == '__main__':
- # 通过模块
- testcase02 = unittest.TestLoader().loadTestsFromModule(test_case02)
- # 通过单元测试类
- testcase03 = unittest.TestLoader().loadTestsFromTestCase(testCase03)
- # 通过模块字符串
- testcase04 = unittest.TestLoader().loadTestsFromName('learn.unittestLearning.test_case04')
- # 测试用例集
- tests = [testcase02, testcase03, testcase04]
- # 创建测试套件
- suite = unittest.TestSuite(tests)
- # 运行测试套件
- unittest.TextTestRunner(verbosity=2).run(suite)
包含知识点
loadTestsFromTestCase(testCaseClass) :testCaseClass 输入单元测试类, 但需要先 import
loadTestsFromModule(module, pattern=None) :module 输入单元测试类所在模块, 也需要 import
loadTestsFromName(name, module=None) :name 是一个 string, 需满足以下格式: module.class.method , 可以只到输入到 class
verbosity : 表示测试结果的信息详细程, 一共三个值, 默认是 1
0 (静默模式): 你只能获得总的测试用例数和总的结果 比如 总共 100 个 失败 20 成功 80
1 (默认模式): 非常类似静默模式 只是在每个成功的用例前面有个 . 每个失败的用例前面有个 F
2 (详细模式): 测试结果会显示每个测试用例的所有相关的信息
批量运行测试用例方式二 (推荐!!):
- import unittest
- if __name__ == '__main__':
- # 需要运行的单元测试文件目录
- test_path = './'
- # 实例化 defaultTestLoader
- discover = unittest.defaultTestLoader.discover(start_dir=test_path, pattern="test_case*.py")
- # 运行测试用例集
- unittest.TextTestRunner().run(discover)
优点: 是不是简洁.. 是不是很快?? 只需三行代码!!
包含知识点
start_dir : 写需要运行的单元测试文件目录
pattern : 单元测试文件的匹配规则, 默认是 test*.py , 可根据自己的命名规则修改此正则
discover() 方法可自动根据测试目录 start_dir 匹配查找测试用例文件 test*.py , 并将查找到的测试用例组装到测试套件, 因此可以直接通过 run() 方法执行 discover
批量执行测试用例的结果
- testCase02 test06
- testCase02 test07
- testCase02 test11
- testCase03 test05
- testCase03 test08
- testCase03 test12
- testCase04 test02
- testCase04 test04
- testCase04 test13
- .........
- ----------------------------------------------------------------------
- Ran 9 tests in 0.000s
- OK
来源: https://www.cnblogs.com/poloyy/p/12235652.html