(二)pytest自动化测试框架之添加测试用例步骤(@allure.step())

news2024/10/6 6:44:57

前言

在编写自动化测试用例的时候经常会遇到需要编写流程性测试用例的场景,一般流程性的测试用例的测试步骤比较多,我们在测试用例中添加详细的步骤会提高测试用例的可阅读性。

allure提供的装饰器@allure.step()是allure测试报告框架非常有用的功能,它能帮助我们在测试用例中对测试步骤进行详细的描述。

@allure.step的使用例子:

实现一个购物的场景:1.登录;2.浏览商品;3.将商品加入到购物车中;4.下单;5.支付订单;

# file_name: test_allure_step.py


import pytest
import allure


@allure.step
def login():
    """
    执行登录逻辑
    :return:
    """
    print("执行登录逻辑")


@allure.step
def scan_good():
    """
    执行浏览商品逻辑
    :return:
    """
    print("执行浏览商品逻辑")


@allure.step
def add_good_to_shopping_car():
    """
    将商品添加到购物车
    :return:
    """
    print("将商品添加到购物车")


@allure.step
def generator_order():
    """
    生成订单
    :return:
    """
    print("生成订单")


@allure.step
def pay():
    """
    支付订单
    :return:
    """
    print("支付订单")


def test_buy_good():
    """
    测试购买商品:
    步骤1:登录
    步骤2:浏览商品
    步骤3:将商品加入到购物车中
    步骤4:下单
    步骤5:支付
    :return:
    """
    login()
    scan_good()
    add_good_to_shopping_car()
    generator_order()
    pay()

    with allure.step("断言"):
        assert 1


if __name__ == '__main__':
    pytest.main(['-s', 'test_allure_step.py'])

执行命令:

> pytest test_allure_step.py --alluredir=./report/result_data

> allure serve ./report/result_data

查看测试报告展示效果:

image

从报告中可以看到,我们事先通过@allure.step()定义好的步骤都展示在测试用例test_buy_good()下了。

@allure.step支持嵌套,step中调用step

# file_name: steps.py


import allure


@allure.step
def passing_step_02():
    print("执行步骤02")
    pass

测试用例:

# file_name: test_allure_step_nested.py


import pytest
import allure

from .steps import passing_step_02  # 从外部模块中导入


@allure.step
def passing_step_01():
    print("执行步骤01")
    pass


@allure.step
def step_with_nested_steps():
    """
    这个步骤中调用nested_step()
    :return:
    """
    nested_step()

@allure.step
def nested_step_with_arguments(arg1, arg2):
    pass

@allure.step
def nested_step():
    """
    这个步骤中调用nested_step_with_arguments(),并且传递参数
    :return:
    """
    nested_step_with_arguments(1, 'abc')


def test_with_imported_step():
    """
    测试@allure.step()支持调用从外部模块导入的step
    :return:
    """
    passing_step_01()
    passing_step_02()


def test_with_nested_steps():
    """
    测试@allure.step()支持嵌套调用step
    :return:
    """
    passing_step_01()
    step_with_nested_steps()
    passing_step_02()


if __name__ == '__main__':
    pytest.main(['-s', 'test_allure_step_nested.py'])

执行命令:

pytest test_allure_step_nested.py --alluredir=./report/result_data

allure serve ./report/result_data

查看测试报告展示效果:

image

从上面的结果中可以看到:

  • @step可以先保存到其他模块中,在测试用例中需要用到的时候导入就可以了;
  • @step也支持在一个step中嵌套调用其他的step;嵌套的形式在测试报告中以树形展示出来了;

@allure.step支持添加描述且通过占位符传递参数

# file_name: test_allure_step_with_placeholder.py


import pytest
import allure


@allure.step('这是一个带描述语的step,并且通过占位符传递参数:positional = "{0}",keyword = "{key}"')
def step_title_with_placeholder(arg1, key=None):
    pass


def test_step_with_placeholder():
    step_title_with_placeholder(1, key="something")
    step_title_with_placeholder(2)
    step_title_with_placeholder(3, key="anything")


if __name__ == '__main__':
    pytest.main(['-s', 'test_allure_step_with_placeholder.py'])

执行命令:

pytest test_allure_step_with_placeholder.py --alluredir=./report/result_data

allure serve ./report/result_data

查看测试报告展示效果:

image

从上面的执行结果中可以看到,@allure.step()是支持输入描述的,并且支持通过占位符向描述中传递参数。

在conftest.py文件中定义@allure.step

conftest.py文件:

# file_name: conftest.py


import pytest
import allure


@pytest.fixture()
def fixture_with_conftest_step():
    conftest_step()


@allure.step("这是一个在conftest.py文件中的step")
def conftest_step():
    pass

测试用例:

# file_name: test_allure_step_in_fixture_from_conftest.py


import pytest
import allure


@allure.step
def passed_step():
    pass


def test_with_step_in_fixture_from_conftest(fixture_with_conftest_step):
    passed_step()


if __name__ == '__main__':
    pytest.main(['-s', 'test_allure_step_in_fixture_from_conftest.py'])

执行命令:

pytest test_allure_step_in_fixture_from_conftest.py --alluredir=./report/result_data

allure serve ./report/result_data

查看测试报告展示效果:

从运行结果中可以看到,在fixture中定义的step会在setup和teardown单独以树形结构展示出来。

这可能是B站最详细的pytest自动化测试框架教程,整整100小时,全程实战!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1234609.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

如今 Android 开发都要转去做鸿蒙开发了吗?

近期,华为的鸿蒙(Harmony OS)操作系统引起了广泛的关注,一是被编写进了许多大学课程;二是不少互联网大厂在为布局鸿蒙系统而“招兵买马”。像美团、京东、网易、今日头条……等知名的互联网大厂,都已经发布…

ubuntu linux C/C++环境搭建

目录 前言 1.1 vim安装与配置 ​编辑 1.2 vim配置 1.3 gcc g编译器的安装 与gdb调试器的安装 1.4 写个C/C程序测试一下 1.6 vscode安装 1.7 vscode插件下载​编辑 前言 在开始C之前,我们需要搭建好C的开发环境,我这里使用的操作系统是ubuntu Linux&a…

股票基础数据(二)

二. 股票基础数据 文章目录 二. 股票基础数据一. 查询股票融资信息数据二. 查询所有的股票信息三. 查询所有的股票类型信息四. 根据类型查询所有的股票数据信息五. 查询股票当前的基本信息六. 查询股票的K线图, 返回对应的 base64 信息七. 展示股票的K线图数据, 对应的是数据信…

JVM对象创建与内存分配

对象的创建 对象创建的主要流程: 类加载推荐博客:JVM类加载机制详解 类加载检查 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已被加载、解析…

STL中set的基本概念与使用

1 定义 1.1 set内元素唯一 1.2 set内元素默认升序排序 1.3 set内元素增&#xff0c;删&#xff0c;查时间复杂度都是O(logn) 2 使用 2.1 声明 set<int> mySet;2.2 插入元素 /*插入元素*/mySet.insert(5);mySet.insert(4);mySet.insert(3);mySet.insert(2);mySet.in…

autojs(意图篇之startActivity)

使用以下函数可以直接打开指定页面 app.startActivity({packageName:包名,className:活动页面类名,root:true })下面的问题是包名与类目如何获取&#xff1a; 打开所取页面运行如下代码&#xff1a; log("包名:"currentPackage()) log("活动页面类名&#xff1…

【shell】条件语句

一、测试 1.1文件测试test test命令是内部命令 test的语法 test 条件表达式 [ 条件表达式 ] test 选项 文件 -d &#xff1a;判断是否是目录 -f &#xff1a;判断是否是普通文件 -b &#xff1a;判断是否是块设备 -c &#xff1a;判断是否是字符设备 -e &#xff1a;判断是否…

阿里 OSS鉴权访问文件

如果OSS文件设置保护&#xff0c;需要鉴权才能访问&#xff0c;添加请求头鉴权&#xff0c;SDK方法如上&#xff1b; 将鉴权信息和地址、时间返回给前端&#xff0c;前端直接从oss上读取 String filePath "/admin/2023/6/183569314928918546.png"; RequestMessage…

MindSpore基础教程:使用 MindCV和 Gradio 创建一个图像分类应用

MindSpore基础教程&#xff1a;使用 MindCV和 Gradio 创建一个图像分类应用 官方文档教程使用已经弃用的MindVision模块&#xff0c;本文是对官方文档的更新 在这篇博客中&#xff0c;我们将探索如何使用 MindSpore 框架和 Gradio 库来创建一个基于深度学习的图像分类应用。我…

场景中的解剖学方向标记_vtkAnnotatedCubeActor

开发环境&#xff1a; Windows 11 家庭中文版Microsoft Visual Studio Community 2019VTK-9.3.0.rc0vtk-example参考代码 demo解决问题&#xff1a;显示标记当前视角、空间的方位&#xff0c;关键对象vtkAnnotatedCubeActor: vtkAnnotatedCubeActor 是一个混合3D 演员&#xf…

Python如何实现模板方法设计模式?什么是模板方法设计模式?Python 模板方法设计模式示例代码

什么是模板方法&#xff08;Template Method&#xff09;设计模式&#xff1f; 模板方法&#xff08;Template Method&#xff09;是一种行为型设计模式&#xff0c;它定义了一个算法的骨架&#xff0c;将一些步骤延迟到子类中实现。这种模式允许子类为一个算法的特定步骤提供…

远程桌面访问MATLAB 2018B,提示License Manger Error -103,终极解决方案

通过远程桌面方位Windows Server系统下的MATLAB2018B&#xff0c;报错License Manger Error -103&#xff0c;Crack文件夹下的dll文件已经替换&#xff0c;同时也已经输出了lic文件&#xff0c;但是仍然无法打开。但是在本地桌面安装就没有问题。初步怀疑MATLAB的License使用机…

Java实现象棋算法

象棋算法包括搜索算法、评估函数和剪枝算法。以下是一个简单的实现&#xff1a; 搜索算法&#xff1a;使用极大极小值算法&#xff0c;即每个玩家都会做出最好的选择&#xff0c;考虑到对方也会做出最好的选择&#xff0c;所以需要搜索多层。 public int search(int depth, i…

UE5 操作WebSocket

插件&#xff1a;https://www.unrealengine.com/marketplace/zh-CN/product/websocket-client 参考&#xff1a;http://dascad.net/html/websocket/bp_index.html 1. 安装Plugings 2.测试websocket服务器 http://www.websocket-test.com/ 3.连接服务器 如果在Level BP里使用&a…

武汉站--ChatGPT/GPT4科研技术应用与AI绘图及论文高效写作

2023年随着OpenAI开发者大会的召开&#xff0c;最重磅更新当属GPTs&#xff0c;多模态API&#xff0c;未来自定义专属的GPT。微软创始人比尔盖茨称ChatGPT的出现有着重大历史意义&#xff0c;不亚于互联网和个人电脑的问世。360创始人周鸿祎认为未来各行各业如果不能搭上这班车…

传输层协议 - TCP(Transmission Control Protocol)

文章目录&#xff1a; TCP 协议关于可靠性TCP 协议段格式序号与确认序号六个标志位16位窗口大小 确认应答&#xff08;ACK&#xff09;机制超时重传机制连接管理机制连接建立&#xff08;三次握手&#xff09;连接终止&#xff08;四次挥手&#xff09;TIME_WAIT 状态CLOSE_WAI…

5.2 Windows驱动开发:内核取KERNEL模块基址

模块是程序加载时被动态装载的&#xff0c;模块在装载后其存在于内存中同样存在一个内存基址&#xff0c;当我们需要操作这个模块时&#xff0c;通常第一步就是要得到该模块的内存基址&#xff0c;模块分为用户模块和内核模块&#xff0c;这里的用户模块指的是应用层进程运行后…

经典双指针算法试题(一)

&#x1f4d8;北尘_&#xff1a;个人主页 &#x1f30e;个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上&#xff0c;不忘来时的初心 文章目录 一、移动零1、题目讲解2、讲解算法原理3、代码实现 二、复写零1、题目讲解2、讲解算法原理3、…

Spring-IOC-@Import的用法

1、Car.java package com.atguigu.ioc; import lombok.Data; Data public class Car {private String cname; }2、 MySpringConfiguration2.java package com.atguigu.ioc; import org.springframework.context.annotation.Bean; import org.springframework.context.annotatio…

VBA技术资料MF85:将工作簿批量另存为PDF文件

我给VBA的定义&#xff1a;VBA是个人小型自动化处理的有效工具。利用好了&#xff0c;可以大大提高自己的工作效率&#xff0c;而且可以提高数据的准确度。我的教程一共九套&#xff0c;分为初级、中级、高级三大部分。是对VBA的系统讲解&#xff0c;从简单的入门&#xff0c;到…