Pytest框架 — 13、Pytest的标记(四)(分组执行)

1、前言

在自动化测试工作中我们有时候并不需要测试所有的测试用例,比如在冒烟测试阶段,我们只需要测试基本功能是否正常就可以了。在 pytest中提供了 mark标记功能来实现分组执行。

2、mark的使用

步骤:

pytest.ini中添加 markers

[pytest] # 固定的section名
markers= # 固定的option名称,注意缩进。
    标签名1: 标签名的说明内容。
    标签名2: 不写也可以
    标签名N

示例:

[pytest]
markers =
    smoke: 冒烟测试
    back

通过 @pytest.mark.标记名标记要测试的用例。
示例:

import pytest

@pytest.mark.smoke
def test_1():
    print("测试1")

@pytest.mark.back
def test_2():
    print("测试2")

def test_3():
    print("测试3")

通过在命令行增加 -m参数指定要测试的分组。
示例:
执行冒烟用例: pytest -vs xxx.py -m smoke

"""
执行结果
collected 3 items / 2 deselected / 1 selected

mark/mark/mark.py::test_1 测试1
PASSED
"""

执行回归用例: pytest -vs xxx.py -m back

"""
执行结果
collected 3 items / 2 deselected / 1 selected

mark/mark/mark.py::test_2 测试2
PASSED
"""

3、扩展

import pytest

@pytest.mark.back
def test_register():
    # 注册
    print("注册")

@pytest.mark.smoke
@pytest.mark.back
def test_login():
    # 登录
    print("登录")

@pytest.mark.back
def test_logout():
    # 注销
    print("注销")

@pytest.mark.smoke
def test_add_cart():
    # 加购物车
    print("加购物车")

@pytest.mark.smoke
def test_place_order():
    # 下单
    print("下单")

@pytest.mark.smoke
@pytest.mark.pay
def test_pay_order():
    # 支付订单
    print("支付订单")

在执行时支持通过 and, or, or来连接多个标记,如下

  • pytest -vs -m "smoke or pay" xxx.py,此时只有登录,加购物车,下单,支付订单这4个用例执行
  • pytest -vs -m "smoke and pay" xxx.py,此时只有支付订单这个用例执行
  • pytest -vs -m "not smoke" xxx.py,此时只有注册,注销这2个用例执行
import pytest

@pytest.mark.smoke
class TestLogin:
    def test_login(self):
        print("登录")

Original: https://www.cnblogs.com/qishuaiRisen/p/16607892.html
Author: 睡觉大王Risen
Title: Pytest框架 — 13、Pytest的标记(四)(分组执行)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/809524/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球