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来决定执行哪些用例。

相关推荐
落魄大学生之流水线上谋生计21 小时前
pytest 入门与实践:从第一个测试到可维护的测试体系
pytest
念越2 天前
Allure 测试报告保姆级教程:从安装到 pytest 集成与报告生成
pytest·allure
念越3 天前
接口自动化测试从入门到实战:接口用例设计、Requests、Pytest、YAML、JSON Schema 与 Allure 报告
python·测试工具·自动化·json·pytest
霍格沃兹测试学院-小舟畅学7 天前
AI+Swagger:一键生成500条pytest接口用例
人工智能·pytest
把头塞进显示器16 天前
电商平台-测试报告
python·selenium·pytest
昔我往昔21 天前
pytest日志问题排查记录
python·pytest
黄昏恋慕黎明24 天前
博客论坛技术迭代
python·pytest
2601_9620782324 天前
Python接口自动化流程(pytest)
python·pytest·接口自动化·测试框架·断言
杨女士YRJ24 天前
python+pytest+01
python·pytest
2601_9620782325 天前
Python接口自动化框架:pytest
python·pytest·接口自动化·测试框架·restfulapi