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

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