app自动化生成测试报告

1.首先导入from BeautifulReport import BeautifulReport

参考:https://www.cnblogs.com/may18/p/10445162.html

2.生成报告文件夹目录

3.生成报告代码:

from BeautifulReport import BeautifulReport
import os
import unittest
import time
#当前脚本所在文件真实路径
cur_path=os.path.dirname(os.path.realpath(__file__))
def add_case(caseName='case',reportName='report',rule='test*.py'):
#加载所有测试用例
case_path = os.path.join(cur_path,caseName)#用例文件夹
#文件夹不存在创建一个文件夹
if not os.path.exists(case_path):
os.mkdir(case_path)
#定义discover加载所有测试用例 case_path:执行用例的目录 pattern:匹配脚本名称的规则 top_level_dir:默认为None
discover=unittest.defaultTestLoader.discover(case_path,pattern=rule,top_level_dir=None)
return discover
def run_case(all_case,nth=0):
#第二步执行所有用例,并把结果写入到HTML测试报告中
now = time.strftime("%Y_%m_%d_%H_%M_%S")
filename = "result_" + str(now)
BeautifulReport(all_case).report(filename=filename, description='测试用例执行情况:', log_path='report')
def get_report_file(report):
# 第三步:获取最新的测试报告
lists = os.listdir(report)
print(lists)
lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report, fn)))
print(u"最新测试生成的报告:" + lists[-1])
# 找到生成最新的报告文件
report_file = os.path.join(report, lists[-1])
return report_file
if __name__ == "__main__":
all_cases = add_case()
run_case(all_cases)

记录生命的成长,愿岁月不会辜负每一个勇往向前的人
原文地址:https://www.cnblogs.com/tc2019/p/12014703.html