使用unittest框架的脚本执行完成后,会生成一个html格式的报告
这个报告是提前制作了一个html的模板,然后将对应的内容写入到模板中,并生成一个最终的报告,这个报告模板在通过 pip install BeautifulReport后,就会在下面路径中存在:
C:Program FilesPython37Libsite-packagesBeautifulReport emplate,这个html模板可以将里面的一些表格属性名称修改为适合自己的名称,例如:
1 <body class="gray-bg"> 2 <div class="row border-bottom white-bg dashboard-header"> 3 <div class="col-sm-12 text-center"> 4 <span style="${title}">自动化测试报告</span> 5 </div> 6 </div> 7 <div class="wrapper wrapper-content animated fadeInRight"> 8 <div class="row"> 9 <div class="col-sm-12"> 10 <div class="ibox float-e-margins"> 11 <div class="ibox-title"> 12 <h5 style="${sub-title}">报告汇总</h5> 13 <div class="ibox-tools"> 14 <a class="collapse-link"> 15 <i class="fa fa-chevron-up"></i> 16 </a> 17 <a class="close-link"> 18 <i class="fa fa-times"></i> 19 </a> 20 </div> 21 </div> 22 <div class="ibox-content"> 23 <div class="row"> 24 <div class="col-sm-6 b-r" style="height:350px"> 25 <form class="form-horizontal"> 26 <div class="form-group"> 27 <label class="col-sm-2 control-label text-info">测试目的:</label> 28 <div class="col-sm-5"> 29 <span class="form-control" id="testName"></span> 30 </div> 31 </div> 32 <div class="form-group"> 33 <label class="col-sm-2 control-label text-info">用例总数:</label> 34 <div class="col-sm-5"> 35 <span class="form-control" id="testAll"></span> 36 </div> 37 </div> 38 <div class="form-group"> 39 <label class="col-sm-2 control-label text-navy">用例通过:</label> 40 <div class="col-sm-5"> 41 <span class="form-control" id="testPass"></span> 42 </div> 43 </div> 44 <div class="form-group"> 45 <label class="col-sm-2 control-label text-danger">用例失败:</label> 46 <div class="col-sm-5"> 47 <span class="form-control text-danger" id="testFail"></span> 48 </div> 49 </div> 50 <div class="form-group"> 51 <label class="col-sm-2 control-label text-warning">用例跳过:</label> 52 <div class="col-sm-5"> 53 <span class="form-control text-warning" id="testSkip"></span> 54 </div> 55 </div> 56 <div class="form-group"> 57 <label class="col-sm-2 control-label text-info">开始时间:</label> 58 <div class="col-sm-5"> 59 <span class="form-control" id="beginTime"></span> 60 </div> 61 </div> 62 <div class="form-group"> 63 <label class="col-sm-2 control-label text-info">运行时间:</label> 64 <div class="col-sm-5"> 65 <span class="form-control" id="totalTime"></span> 66 </div> 67 </div> 68 </form> 69 </div> 70 <div class="col-sm-6"> 71 <div style="height:350px" id="echarts-map-chart"></div> 72 </div> 73 </div> 74 </div> 75 </div> 76 </div> 77 </div>
那么通过这个报告生成,都可以做哪些呢,比较指定存储路径,报告主题等,下面直接看BeautifulReport代码,发现要对这个类实例化时,必须要先传入一个suite(也就是测试用例集),然后调用这个类的report方法进行报告生成时,可以传入哪些参数:description, filename: str = None, report_dir='.', log_path=None, theme='theme_default',除去 log_path废弃后,可以有4个参数进行传入,每个参数的具体用法在代码中都有详细说明,这里不再重复。
1 class BeautifulReport(ReportTestResult, PATH): 2 img_path = 'img/' if platform.system() != 'Windows' else 'img\' 3 4 def __init__(self, suites): 5 super(BeautifulReport, self).__init__(suites) 6 self.suites = suites 7 self.report_dir = None 8 self.title = '自动化测试报告' 9 self.filename = 'report.html' 10 11 def report(self, description, filename: str = None, report_dir='.', log_path=None, theme='theme_default'): 12 """ 13 生成测试报告,并放在当前运行路径下 14 :param report_dir: 生成report的文件存储路径 15 :param filename: 生成文件的filename 16 :param description: 生成文件的注释 17 :param theme: 报告主题名 theme_default theme_cyan theme_candy theme_memories 18 :return: 19 """ 20 if log_path: 21 import warnings 22 message = ('"log_path" is deprecated, please replace with "report_dir" ' 23 "e.g. result.report(filename='测试报告_demo', description='测试报告', report_dir='report')") 24 warnings.warn(message) 25 26 if filename: 27 self.filename = filename if filename.endswith('.html') else filename + '.html' 28 29 if description: 30 self.title = description 31 32 self.report_dir = os.path.abspath(report_dir) 33 os.makedirs(self.report_dir, exist_ok=True) 34 self.suites.run(result=self) 35 self.stopTestRun(self.title) 36 self.output_report(theme) 37 text = ' 测试已全部完成, 可打开 {} 查看报告'.format(os.path.join(self.report_dir, self.filename)) 38 print(text)
下面列举调用这个模块的实现方法:
1 # -*- coding:utf-8 -*- 2 ''' 3 # @Time : 2019/12/3 16:50 4 # @Author : nihaoya 5 # @FileName: WeiBo_test.py 6 # @Software: PyCharm 7 ''' 8 import os 9 import time 10 import unittest 11 from BeautifulReport import BeautifulReport as bf 12 13 class WeiBo(unittest.TestCase): 14 此处省略 15 16 if __name__ == "__main__": 17 suite = unittest.TestLoader().loadTestsFromTestCase(WeiBo) 18 run = bf(suite) 19 run.report(filename=u"微博测试报告_" + time.strftime("%Y~%m~%d %H~%M~%S"), description=u"以游客形式浏览微博", report_dir="report", theme="theme_memories")