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

相关推荐
摘星编程3 天前
AI 帮我写单测:pytest 覆盖率提升 40% 的协作日志
人工智能·pytest·测试驱动开发·代码覆盖率·ai协作开发
lucia_zl6 天前
pytest并发测试,资源问题导致用例失败解决办法
pytest
鱼鱼说测试6 天前
Selenium4+Pytest自动化测试框架实战
pytest
XYiFfang12 天前
【Pytest】解决Pytest中Teardown钩子的TypeError:实例方法与类方法的调用差异
python·pytest
Kingairy14 天前
Pytest 插件:pytest_runtest_protocol
python·pytest
AIZHINAN14 天前
Pytest 插件介绍和开发
测试工具·pytest·插件开发
灰阳阳16 天前
替身演员的艺术:pytest-mock 从入门到飙戏
自动化测试·python·pytest·unit testing·pytest-mock
年年测试20 天前
Playwright与PyTest结合指南
pytest
focksorCr20 天前
pytest 并发执行用例(基于受限的测试资源)
python·pytest