打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
pytest + yaml 框架 -36.mark 标记功能实现

# 前言

pytest可以支持对用例自定义标记, 可以把用例按自己的需要归类标记,比如按用例优秀级,标记一些smoke冒烟测试用例。

pytest 标记基本使用

test_m.py 用例内容

import pytest

@pytest.mark.smoke
def test_login():
pass

def test_something():
pass

@pytest.mark.smoke
def test_another():
pass

执行的时候加-m 参数

(venv) D:\demo\untitled_mark>pytest test_m.py -m smoke
================== test session starts ====================
platform win32 -- Python 3.8.5, pytest-7.3.1, pluggy-1.0.0
rootdir: D:\demo\untitled_mark
configfile: pytest.ini
plugins: allure-pytest-2.13.2, Faker-18.10.1, yaml-yoyo-1.3.0
collected 3 items / 1 deselected / 2 selected

test_m.py .. [100%]

================ 2 passed, 1 deselected in 0.13s ================

yaml 用例中加 mark 标记

yaml 用例中支持2个地方加标记

  • config 中使用mark, 作用是当前yaml 文件中的全部用例打上标记

  • case 用例中加mark,只针对单个用例打上标记

需注意的是一个用例可以打多个标记,mark 关键字可以是一个字符串,如果是多个标记,可以用mark: name1, name2mark: [name1, name2] 两种格式
test_m.yml

config:
name: yy
mark: www

test_a1:
name: a11
print: "xx111"

test_a2:
name: a22
print: "xx22"

config 中加标记,对test_a1 和 test_a2 都会打上标记

test_n.yml

config:
name: yy2

test_a3:
mark: [www, aaa]
name: a333
print: "xx333"

test_a4:
-
name: a444
mark: aaa
print: "xx444"

1.执行标记为 www 的用例

>pytest -m www -s

运行结果

collected 4 items / 1 deselected / 3 selected                                                                           

test_m.yml xx111
.xx22
.
test_n.yml xx333
.

================ 3 passed, 1 deselected in 0.31s ================

2.执行标记为 aaa 的用例

>pytest -m aaa -s

运行结果

collected 4 items / 2 deselected / 2 selected                                                                           

test_n.yml xx333
.xx444
.

================= 2 passed, 2 deselected in 0.31s =================

3.执行标记了www并且也标记aaa的用例

>pytest -m "www and aaa" -s

4.执行没有标记www的用例

>pytest -m "not www" -s

5.执行标记了www或aaa的用例

>pytest -m "www or aaa" -s

2023年第 15期《Python接口自动化+Playwright 》课程,6月24号开学(课程全面升级!)

2020年第三期《python接口自动化+测试开发》课程,4月5号开学(火热报名中!)

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
超详细的 pytest 教程 (三) 之用例标记和测试执行篇
pytest入门 -2-分组执行
pytest文档15-使用自定义标记mark
pytest---用例执行参数
Python 单元测试框架:Pytest
代码质量保证-单元测试框架pytest
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服