自动化测试(四):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+时间这一行文字也是可以修改的:
针对一次报告自定义窗口
针对所有报告修改窗口配置

相关推荐
nbwenren2 天前
2026实测:Gemini 3.1 Pro 从需求文档到 pytest 测试用例一条龙教程
测试用例·pytest
我的xiaodoujiao2 天前
API 接口自动化测试详细图文教程学习系列16--项目实战演练3
python·学习·测试工具·pytest
我的xiaodoujiao2 天前
API 接口自动化测试详细图文教程学习系列15--项目实战演练2
python·学习·测试工具·pytest
技术钱3 天前
PyTest配置与API测试用例
servlet·测试用例·pytest
weixin_419658315 天前
pytest 零基础入门实战指南
python·pycharm·pytest
姚青&8 天前
软件测试基础概念
单元测试·pytest
爆更小哇11 天前
pytest集成Allure报告教程
python·测试工具·pytest·接口测试·allure
gCode Teacher 格码致知11 天前
Python提高:pytest的简单案例-由Deepseek产生
python·pytest
gCode Teacher 格码致知12 天前
Python提高: unittest和 pytest的使用方法-由Deepseek产生
开发语言·python·pytest