当前位置: 首页 > news >正文

pytest结合Allure生成测试报告

文章目录

  • 1.Allure配置安装
  • 2.使用
    • 基本命令
    • 报告美化
      • 1.**前置条件**
      • 2.**用例步骤**
      • **3.标题和描述**
      • **4.用例优先级**
  • 3.进阶用法
    • allure+parametrize参数化
    • parametrize+ids
    • parametrize+@allure.title()
  • 4.动态化参数
  • 5.环境信息
    • **方式一**
    • **方式二**
  • 6.用例失败截图

1.Allure配置安装

要求:

​ 1.需要安装jdk环境

​ 2.下载Allure安装包,下载地址:Allure下载地址

​ 3.解压后放置英文路径下并配置环境变量PATH

image-20240221181923327

验证:allure --version

image-20240221182246655

​ 4.安装allure-pytest插件pip install allure-pytest

验证:pip show allure-pytest

image-20240221182447358

2.使用

基本命令

1.使用allure时需要在cmd中执行,可以使用os.system命令省去在cmd窗口中再次执行一次;

import osimport pytestdef test_01():print('开始登录')def test_02():print('上传数据')def test_03():print('数据浏览')if __name__ == '__main__':pytest.main(['-sv','test_allure01.py','--alluredir', './result'])#pytest.main(['test_allure01.py','--alluredir', './result','--clean-alluredir'])os.system('allure generate ./result -o ./report --clean')
  • '-sv','test_allure01.py'表示执行指定的测试文件并在控制台中输出;

    ​ 注:使用‘-sv’后,测试报告中就不会再记录控制台的信息了;

  • '--alluredir', './result'表示创建allure报告的路径,allure生成的报告文件都是json文件

image-20240222100412162

  • '--clean-alluredir'表示清除allure之前生成的json数据,如果不加这个参数会导致:如果对某些用例进行注释或删除,生成的测试报告仍会有之前的记录数据;

  • os.system('allure generate ./result -o ./report --clean')表示在main中转换allure生成的报告文件为html格式;

  • ./result -o :开始执行上面allure 生成的json文件

  • ./report :生成的html格式报告存放路径

  • –clean:清除之前生成的报告,但是不会清除文件,记录仍会保留

  • 使用pytest -h可以查看相关参数

    image-20240223094030734

    –alluredir=DIR 在指定目录中生成 Allure 报告(目录可能不存在)

    –clean-alluredir 如果存在,则清理 alluredir 文件夹

    –allure-no-capture 不将 pytest 捕获的日志/标准输出/标准错误附加到报告中

    –inversion=INVERSION 运行不在测试计划中的测试

执行完后会生成很多文件,右键浏览器中打开index.html文件

image-20240222100828308

报告美化

1.前置条件

前置条件只要在测试用例中使用到,测试报告中会自动添加上用例前置函数的名称;

conftest.py

import pytest@pytest.fixture
# @allure.title('前置准备')  # 可以修改fix01的名字
def fix01():print('用例准备前置工作')

test_allure01.py

import osimport allure
import pytest@allure.step('步骤1:执行登录')
def login():print('开始登录')print('输入账号')print('输入密码')@allure.step('步骤2:上传数据')
def upload_data():print('点击上传按钮')print('上传数据')def test_01(fix01):# 测试用例中调用登录业务login()def test_02():# 调用登录业务upload_data()

image-20240222102748961

image-20240222141721279

2.用例步骤

使用装饰器@allure.step()在需要说明的测试步骤函数上装饰后,在测试用例的测试步骤上就能显示出来;

@allure.step('步骤1:执行登录')会带上函数中的传参和对应的值

@allure.step("登陆")
def log(user, pwd):print(user)print(pwd)@pytest.fixture
def fix01():log('zz', '123')print('用例准备前置工作')

image-20240222135754308

``with allure.step(‘步骤1:执行登录’)`不会带上函数里面的传参

def log(user, pwd):with allure.step('用户名'):print(user)with allure.step('密码'):print(pwd)@pytest.fixture
def fix01():log('zz', '123')print('用例准备前置工作')

image-20240222135955772

3.标题和描述

import osimport allure
import pytest@allure.step('步骤1:执行登录')
def login():print('开始登录')print('输入账号')print('输入密码')@allure.step('步骤2:上传数据')
def upload_data():print('点击上传按钮')print('上传数据')class TestUpload:@allure.story("一级模块")  # 表示一级模块,同名的一级模块会把用例归纳在一起@allure.title("登录")  # 用例的title@allure.issue("http://127.0.0.1:8080/zentao/buge-login.html", name=
http://www.lryc.cn/news/304885.html

相关文章:

  • Linux--ACL权限管理
  • Xcode中App图标和APP名称的修改
  • Spring 手动实现Spring底层机制
  • CSV数据导入到ClickHouse数据库
  • 第十二天-ppt的操作
  • 计算机网络-网络层,运输层,应用层
  • Python爬虫学习
  • 台式电脑黑屏无法开机怎么办 电脑开机黑屏的解决方法
  • 【Docker】初学者 Docker 基础操作指南:从拉取镜像到运行、停止、删除容器
  • 突破编程_C++_面试(数组(1))
  • 基于springboot+vue的靓车汽车销售网站(前后端分离)
  • 【知识整理】Git Commit Message 规范
  • HarmonyOS学习--三方库
  • 【服务器数据恢复】FreeNAS+ESXi虚拟机数据恢复案例
  • 【GPT-2】论文解读:Language Models are Unsupervised Multitask Learners
  • 基于机器学习、遥感和Penman-Monteith方程的农田蒸散发混合模型研究_刘燕_2022
  • 博客 cn 站搭建 v3 v3.1
  • 2024全国水科技大会暨流域水环境治理与水生态修复论坛(六)
  • Python实战:读取MATLAB文件数据(.mat文件)
  • spring boot3登录开发-3(账密登录逻辑实现)
  • Django后端开发——ORM
  • AI模型训练的初步整理
  • 【Java从入门到精通】Java Number Math 类
  • SQL字符集
  • openssl 生成nginx自签名的证书
  • adb push 使用
  • 【Docker】构建pytest-playwright镜像并验证
  • 算法沉淀——穷举、暴搜、深搜、回溯、剪枝综合练习四(leetcode真题剖析)
  • 如何在java中使用 Excel 动态函数生成依赖列表
  • 07 MyBatis之高级映射 + 懒加载(延迟加载)+缓存