自动化测试(四):pytest结合allure生成测试报告

Allure 报告框架的名称

allure: noun [ U ] 诱惑;魅力;吸引力

文章目录

  • [1. allure下载](#1. allure下载)
  • [2. pytest框架使用allure](#2. pytest框架使用allure)
  • [3. 生成allure报告](#3. 生成allure报告)

1. allure下载

下载前需要先安装JDK,这里可以参考自动化测试(二)
Allure下载路径https://github.com/allure-framework/allure2/releases

解压后放到指定文件夹,配置系统路径:

  1. win+R,control,系统与安全→系统→高级系统设置→环境变量→系统变量
  2. 选中Path,新建系统变量

    统统确认。
  3. 测试是否安装成功:cmd中allure --version,成功则显示版本型号。在pycharm的terminal中运行时记得配置完路径后重启一下pycharm使得环境生效,否则报错。

2. pytest框架使用allure

在pytest.ini中配置allure,比如下面的最下方配置 allure 报告

python 复制代码
[pytest]
# 设置测试文件的搜索路径
testpaths = ./test_Google_tran
# 设置测试文件的命名模式
python_files = test_*.py *_test.py
# 设置测试函数的命名模式
python_functions = test_*
# 设置测试类的命名模式
python_classes = Test*
# 设置测试日志的级别
log_cli = true
log_cli_level = INFO
log_file = test_log.txt
log_file_level = DEBUG

# 配置 allure 报告
addopts = -vs --alluredir=./allure-results  --clean-alluredir

此时执行后就会在对应的alluredir下生成:

3. 生成allure报告

方法一:使用os.system系统调用allure

run.py

python 复制代码
import os
import time

import pytest

if __name__ == "__main__":
    pytest.main(['--capture=sys'])
    time.sleep(3)  # 确保生成了allure临时文件
    os.system("allure generate ./allure-results -o ./reports --clean")

打开LearningAutoTesting\reports\index.html即可看到生成报告:

注意allure的图标是可以修改的,allure report+时间这一行文字也是可以修改的:
针对一次报告自定义窗口
针对所有报告修改窗口配置

相关推荐
开源优测9 小时前
什么是pytest.ini及如何在Pytest中应用以提升配置效率
pytest
摸鱼仙人~1 天前
ImportError: cannot import name ‘FixtureDef‘ from ‘pytest‘
conda·pytest·fastapi
唐古乌梁海1 天前
【pytest-jira】自动化用例结合jira初版集成思路
pytest·jira
小码哥说测试3 天前
高效执行自动化用例:分布式执行工具pytest-xdist实战!
自动化测试·软件测试·功能测试·jmeter·pytest·postman·测试工程师
予早3 天前
pytest asyncio 支持插件 pytest-asyncio
pytest
爱学测试的雨果5 天前
Pytest自动化测试框架关联/参数化实战
前端·pytest
测试杂货铺5 天前
Pytest快速入门
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
莲动渔舟6 天前
pytest测试专题 - 2.1 一种推荐的测试目录结构
自动化测试·python自动化测试·测试用例·pytest
莲动渔舟6 天前
pytest测试专题 - 1.2 如何获得美观的测试报告
自动化测试·pytest·接口测试·黑盒测试·白盒测试