Pytest系列-使用自定义标记mark(6)

简介

  • pytest 可以支持自定义标记,自定义标记可以把一个 web 项目划分为多个模块,然后指定模块名称执行

Pytest 里面自定义标记

用法:将@pytest.mark.标记名称 放到测试函数或者类上面

使用:

  • 执行时加上 -m 标记名 进行用例筛选,例如加上 -m app ,就执行标记名为app的用例。
  • 如果不运行app相关的用例,则加上 -m "not app"。
  • 需要运行多个标记则加上-m "app or web",代码运行标记为app或者web的用例。
  • 需要运行多个标记的,则加上 -m "app and web",代表用例需要同时包含app和web的标记。

小栗子

python 复制代码
import pytest

@pytest.mark.weibo
def test_weibo():
    print("测试微博")

@pytest.mark.wx
def test_wx():
    print("测试微信")

@pytest.mark.xinlang
class TestStudy:
    def test_demo(self):
        print("测试新浪")

def test_noMark():
    print("没有测试标记")

cmd命令窗口输入: pytest -v -m weibo test_mark.py 执行结果为:

cmd命令窗口输入: pytest -v -m "not weibo" test_mark.py 执行结果为:

如何避免warnings

  • 通过注册标记
  • 通过框架配置文件pytest.ini

通过注册标记

conftest.py 添加如下的代码:

  • 单个注册标记
python 复制代码
def pytest_configure(config):
    config.addinivalue_line("markers", "web:web相关用例")
  • 批量注册标记
python 复制代码
def pytest_configure(config):
    marker_list = ["weibo", "wx" , "xinlang"] # 标签名集合
    for markers in marker_list:
        config.addinivalue_line("markers", markers)

注意:conftest.py 文件放到case目录,添加内容对应的内容

cmd命令窗口输入: pytest -v -m "not weibo" test_mark.py 执行结果为

通过文件pytest.ini标记

创建一个pytest.ini文件

  • 加上自定义mark,如下图
  • 注意:pytest.ini需要和运行的测试用例同一个目录,或在根目录下作用于全局
python 复制代码
[pytest]

markers=
   weibo:this  is weibo page

cmd命令窗口输入: pytest -v -m wx test_mark.py 执行结果为

参考文章

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