19、pytest通过mark标记测试函数

官方实例

ini 复制代码
[pytest]
markers = 
	slow:marks tests as slow(deselect with '-m "not slow"')
	serial
python 复制代码
# content of test_mark.py
import pytest

@pytest.mark.slow
def test_mark_function():
    print("test_mark_function was invoked")
    assert 0

解读与实操

通过使用pytest.mark帮助器,你可以轻松地在测试函数上设置元数据。你可以在API参考中找到内置标记的完整列表。

创建自定义标记或将标记应用于整个测试类或模块都非常容易。这些标记可以被插件使用,也通过用于使用-m选项在命令行上选择测试。

标记只能应用于测试,对fixture没有影响。

  1. 注册markers
  2. 在测试函数中使用markers
  3. 调用pytest时增加-m <markers名称>

场景应用

对测试类型进行分类,使用自定义marks很方便,由于测试环境、生产环境能执行的用例不同,只需要将用例增加prod、test、或同时增加prod和test,就可以通过marks来决定执行哪些用例。

相关推荐
Yorlen_Zhang19 小时前
Python pytest assert 断言
python·servlet·pytest
纯纯沙口2 天前
pytest - 基础使用教程
pytest
问道飞鱼3 天前
【自动化测试】pytest 语法与核心概念
自动化测试·pytest·playwright
zUlKyyRC3 天前
基于一阶RC模型,FFRLS+EKF算法的电池SOC在线联合估计Matlab程序
pytest
我的xiaodoujiao5 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 40--完善优化 Allure 测试报告显示内容
python·学习·测试工具·pytest
我的xiaodoujiao5 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 41--自定义定制化展示 Allure 测试报告内容
python·学习·测试工具·pytest
nvd115 天前
Pytest 异步数据库测试实战:基于 AsyncMock 的无副作用打桩方案
数据库·pytest
nvd115 天前
深入分析:Pytest异步测试中的数据库会话事件循环问题
数据库·pytest
程序员雷叔6 天前
在postman设置请求里带动态token,看看这两种方法!
selenium·测试工具·单元测试·测试用例·pytest·lua·postman