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

相关推荐
努力搬砖的咸鱼2 天前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)4 天前
PyTest框架学习
学习·pytest
程序员杰哥4 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥4 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻5 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿5 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder6 天前
Pytest Fixture 详解
数据库·pytest
not coder6 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂7 天前
(1)pytest简介和环境准备
pytest
not coder7 天前
Pytest Fixture 是什么?
数据库·oracle·pytest