Pytest学习(八) - 自定义标记mark的使用

一、前言:

pytest 支持自定义标记,自定义标记方便用例模块划分,也可以理解方便管理,配合标记参数 -m使用

二、示例代码

# -*- coding: utf-8 -*-
# @Time    : 2020/11/15 9:51
# @Author  : longrong.lang
# @FileName: test_mark.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
import pytest


@pytest.mark.mylogin
def test_login():
    print('登录成功!')


@pytest.mark.query
def test_query():
    print('检索商品成功!')


@pytest.mark.addcart
def test_addcart():
    print('加入购物车')


if __name__ == '__main__':
    # pytest -s -m query test_mark.py
    pytest.main(['-s', '-m query', 'test_mark.py'])

三、命令行执行

pytest -s -m query test_mark.py

可能看到这的同学会问了,为什么不用main方法执行,我代码里虽然写了,但是main执行就变成了了全部执行,这点我也很迷糊,为什么不行呢。有解决的同学,请在文末留言,我也学习下。
运行结果如下:

四、解决运行的warnings提示

创建一个pytest.ini文件,文件需要和运行的测试用例同一个目录,或在根目录下作用于全局。
加上自定义mark,内容如下:

[pytest]
markers =
    mylogin: this is mylogin page
    query: this is query page
    addcart: this is addcart page

五、如果不想标记query的用例

执行命令如下:

pytest -s -m "not query" test_mark.py

运行结果如下:

六、执行多个自定义标记的用例

执行命令如下:

pytest -s -m "query or mylogin" test_mark.py

运行结果如下:

系列参考文章:
https://www.cnblogs.com/poloyy/category/1690628.html

posted @ 2020-11-15 13:39  久曲健  阅读(652)  评论(0编辑  收藏  举报