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

前言

pytest可以支持对用例自定义标记, 可以把用例按自己的需要归类标记,比如按用例优秀级,标记一些smoke冒烟测试用例。
备注:从v1.2.4 以后新版本不再公开,新功能内部 VIP 学员可以使用,公开版本仅解决bug, 不提供新功能了。

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

网易云完整视频课程https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)

posted @ 2023-06-09 22:13  上海-悠悠  阅读(250)  评论(0编辑  收藏  举报