Pytest+html,Pytest+allure配置及使用

1. pytest官网

https://docs.pytest.org/en/stable/
可以使用pip安装

	pip install pytest  # 安装稳定版本 pip install pytest==6.2.1

2. html报告,需要安装html插件

pip install pytest-html

3. allure报告,安装和配置

官方网站https://docs.qameta.io/allure/。allure解析测试结果文件,然后生成多维度数据展示的测试报告。配置如下:

3.1 pip安装allure

pytest生成可供allure解析的测试结果文件,但需要安装allure插件,使用pip安装
注:测试结果文件由必须得是allure能够解析的。

pip install allure-pytest

3.2 手动安装allure工具

下载地址:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.17.2/
windows系统下,我使用的是zip包,将下载的压缩包解压到本地路径

3.3 配置环境变量

将allure的bin目录配置到环境变量path中

4. allure生成测试报告

  • 在Python目录下新建allure报告相关的python package,例如:allure-report-files。注:一定创建python package而不是Directory,不然无法解析测试结果文件。

  • 在pytest命令加上参数:–alluredir=/tmp/my_allure_results,此路径是相对rootdir的路径
    main.py

    import pytest
    
    # 收集用例并执行测试用例
    pytest.main(["-s","-v","--html=PythonBase_01.html",
             "--alluredir=allure-report-files"])
    
  • 运行main.py,pytest执行完用例会生成测试结果文件
    Pytest+html,Pytest+allure配置及使用_第1张图片

    4.1 allure serve生成测试报告

  • 可以在pycharm直接打开终端,cd到当前main.py文件所在的目录下,使用allure命令生成报告:

    allure serve allure-report-files # allure-report-files为测试结果文件路径
    

    Pytest+html,Pytest+allure配置及使用_第2张图片

  • 解析完测试结果文件,生成报告后,将自动打开默认浏览器,打开:allure报告,即可查看多维度的测试报告结果
    Pytest+html,Pytest+allure配置及使用_第3张图片

4.2 allure generate生成html测试报告

–clean-alluredir参数:每次执行case生成新的测试结果之前,将清空上次的结果
allure generate命令:生成 HTML 格式的测试结果报告,如下图可以使用浏览器打开

import os
import pytest

if __name__ == '__main__':
    pytest.main(['-v', '--alluredir', './result', '--clean-alluredir'])
    os.system('allure generate ./result/ -o ./report_allure/ --clean')

Pytest+html,Pytest+allure配置及使用_第4张图片

5. html报告使用方式

在pytest命令加上参数:–html=报告路径.html,运行main.py后,在rootdir目录下看到PythonBase_01.html,手动使用浏览器打开此文件,可查看测试结果。
main.py

import pytest
	
# 收集用例并执行测试用例
pytest.main(["-s","-v","--html=PythonBase_01.html",
             "--alluredir=allure-report-files"])

你可能感兴趣的:(接口测试,html,python,前端)