test suite 

* 测试套件,理解成测试用例集
* 一系列的测试用例,或测试套件,理解成测试用例的集合和测试套件的集合
* 当运行测试套件时,则运行里面添加的所有测试用例
 

test runner 

* 测试运行器
* 用于执行和输出结果的组件
 

test suite、test runner基础使用

单元测试类
1 # 创建单元测试类,继承unittest.TestCase 2 class testCase(unittest.TestCase): 3 4 #
测试case 5 def test_01(self): 6 print("test01") 7 8 def test_03(self): 9
print("test03") 10 11 def test_04(self): 12 print("test04") 13 14 def
test_05(self):15 print("test05")
 

主函数
1 if __name__ == '__main__': 2 # 实例化测试套件 3 suite = unittest.TestSuite() 4 #
实例化第二个测试套件 5 suite1 = unittest.TestSuite() 6 # 添加测试用例 - 方式一 7
suite.addTest(testCase('test_03')) 8 suite.addTest(testCase('test_01')) 9
suite1.addTest(testCase('test_03')) 10 suite1.addTest(testCase('test_01')) 11 #
添加测试用例 - 方式二 12 testcase = (testCase('test_05'), testCase('test_04')) 13
suite.addTests(testcase)14 # 测试套件添加测试套件 15 suite.addTest(suite1) 16 #
实例化TextTestRunner类 17 runner = unittest.TextTestRunner() 18 # 运行测试套件 19
runner.run(suite)
 

运行结果
1 test03 2 test01 3 test05 4 test04 5 test03 6 test01 7 ...... 8
---------------------------------------------------------------------- 9 Ran 6
testsin 0.000s 10 11 OK
 

包含知识点

* 使用测试套件时,测试用例的执行顺序可以自定义,按照添加的顺序执行
* 有两种添加测试用例的方式,推荐方式二,代码更少更快捷
* addTests(tests) ,传入的 tests 可以是list、tuple、set
* 添加的测试用例格式是:单元测试类名(测试用例名) 
* 使用测试套件执行测试用例的大致步骤是:实例化TestSuite - 添加测试用例 - 实例化TextTestRunner - 运行测试套件
* 测试套件也可以添加测试套件
 

测试用例批量执行

单元测试类文件



前三个文件是包含了单元测试类的文件,第四个文件是负责运行所有单元测试类,不包含测试用例

列举某个单元测试类文件代码
1 # 创建单元测试类,继承unittest.TestCase 2 class testCase02(unittest.TestCase): 3 4
# 测试case 5 def test_07(self): 6 print("testCase02 test07") 7 8 def
test_06(self): 9 print("testCase02 test06") 10 11 def test_11(self): 12 print("
testCase02 test11")
 

test_run.py 代码

批量运行测试用例方式一:
1 import unittest 2 from learn.unittestLearning import test_case02 3 from
learn.unittestLearning.test_case03import testCase03 4 5 if __name__ == '
__main__': 6 # 通过模块 7 testcase02 =
unittest.TestLoader().loadTestsFromModule(test_case02) 8 # 通过单元测试类 9
testcase03 = unittest.TestLoader().loadTestsFromTestCase(testCase03) 10 #
通过模块字符串 11 testcase04 = unittest.TestLoader().loadTestsFromName('
learn.unittestLearning.test_case04') 12 # 测试用例集 13 tests = [testcase02,
testcase03, testcase04]14 # 创建测试套件 15 suite = unittest.TestSuite(tests) 16 #
运行测试套件 17 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 (详细模式):测试结果会显示每个测试用例的所有相关的信息
 

批量运行测试用例方式二(推荐!!):
1 import unittest 2 3 if __name__ == '__main__': 4 # 需要运行的单元测试文件目录 5 test_path
='./' 6 # 实例化defaultTestLoader 7 discover =
unittest.defaultTestLoader.discover(start_dir=test_path, pattern="test_case*.py"
)8 # 运行测试用例集 9 unittest.TextTestRunner().run(discover)
优点:是不是简洁。。是不是很快??只需三行代码!!

包含知识点

* start_dir :写需要运行的单元测试文件目录
* pattern :单元测试文件的匹配规则,默认是 test*.py ,可根据自己的命名规则修改此正则
* discover()方法可自动根据测试目录start_dir 匹配查找测试用例文件 test*.py
 ,并将查找到的测试用例组装到测试套件,因此可以直接通过 run() 方法执行 discover
 

批量执行测试用例的结果
1 testCase02 test06 2 testCase02 test07 3 testCase02 test11 4 testCase03
test05 5 testCase03 test08 6 testCase03 test12 7 testCase04 test02 8
testCase04 test04 9 testCase04 test13 10 ......... 11
----------------------------------------------------------------------12 Ran 9
testsin 0.000s 13 14 OK
 

 
 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信