1、先导入HTMLTestRunner模块
2、实例如下
(1)单用例文件执行且生成报告
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import unittest import HTMLTestRunner class Study01(unittest.TestCase): def test01( self ): print "test01" def test02( self ): self .assertEqual( 1 , 2 ,msg = "1 != 2" ) def test03( self ): print "test03" def test04( self ): print "test04" if __name__ = = '__main__' : testcases = [Study01( "test01" ),Study01( "test02" ),Study01( "test03" ),Study01( "test04" )] suit = unittest.TestSuite() suit.addTests(testcases) #测试报告生成 dir = "D:\\test.html" #定义测试报告文件 filename = open ( dir , "wb" ) #"wb"新建或者打开一个二进制文件,写入执行完的数据 runner = HTMLTestRunner.HTMLTestRunner(stream = filename, title = "Testcase Report" , description = u "测试用例明细" ) #调用HTMLTestRunner类定义测试报告内容 runner.run(suit) #调用HTMLTestRunner类下面的run()方法运行用例套件 filename.close() #关闭测试报告文件 |
(2)批量执行用例且生成测试报告
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import unittest import HTMLTestRunner def all_case(): case_dir = "D:\work_doc\pycharm2\python_Basics" #用例存放路径 discover = unittest.defaultTestLoader.discover(case_dir, pattern = "XFS*.py" , top_level_dir = None ) return discover if __name__ = = "__main__" : dir = "d:\\test1.html" filename = open ( dir , "wb" ) runner = HTMLTestRunner.HTMLTestRunner(stream = filename, title = "Test Report" , description = "description" ) runner.run(all_case()) |
3、解释
- wb:只写打开或新建一个二进制文件;只允许写数据。
- stream:测试报告写入文件的存储路径
- title:测试报告的主题
- description:测试报告的描述
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/ZhengYing0813/p/11696869.html