pytest 中支持报告的的插件有很多,比如pytest-html, pytest-allure.
pytest-html 使用
安装
通过pip 进行安装,或者使用 pycharm 进行安装
pip install pytest-html
使用
基本使用
"""执行 testcases 下所有的测试用例,执行完成之后生成对应的测试报告测试报告希望放到项目的根目录下的 reports目录"""import pytestif __name__ == '__main__':# 使用pytest 运行 testcases目录下所有的测试用例,运行完成之后生成测试报告文件,文件名为report.htmlpytest.main(['testcases','--html=report.html', '--self-contained-html'])
运行main.py 文件, 执行成功之后,可以看到生成的测试报告。
使用浏览器打开报告文件。可以看到整体的执行情况
添加时间
"""执行 testcases 下所有的测试用例,执行完成之后生成对应的测试报告测试报告希望放到项目的根目录下的 reports目录每次运行的时候生成的报告报告文件后跟上日期_时间"""from common.utils import get_project_rootimport pytestimport osimport timeif __name__ == '__main__':# 创建reports目录# 获取项目的根目录root = get_project_root()# 路径拼接reports = os.path.join(root,"reports")if not os.path.exists(reports):# 创建目录os.mkdir(reports)#在reports目录创建文件。文件名为了避免重复,文件名后跟时间filename = time.strftime("%Y_%m_%d_%H_%M_%S") # 年 月 日 时 分 秒reportfile = os.path.join(reports,"report_"+filename+".html")print(reportfile)# 使用pytest 运行 testcases目录下所有的测试用例,运行完成之后生成测试报告文件,文件名为report.htmlpytest.main(['testcases',f'--html={reportfile}', '--self-contained-html'])
运行,可以看到生成的报告会自动加上时间
