全网最牛自动化测试框架系列之pytest(9)-标记用例(指定执行、跳过用例、预期失败)_pytest

49 阅读4分钟

方法一,在项目根目录新建pytest.ini,并在其中注册、管理标签。示例如下:

[pytest]

markers =
    smoke: marks test as smoke
    login
    order: 下单场景

这里定义了三个标签,分别是:smoke、login、order,冒号后面的是标签说明,可不加。

方法二,在conftest.py中定义钩子函数进行标签注册,格式如下:

def pytest_configure(config):
    marker_list = [
        "smoke: marks test as smoke",
        "login",
        "order: 下单场景"
    ]
    for marker in marker_list:
        config.addinivalue_line("markers", marker)

方法二需注意定义的格式,不能轻易修改函数名及入参。

使用方法

import pytest

# 标记测试函数
@pytest.mark.smoke
def test_01():
    print("执行test_01")

def test_02():
    print("执行test_02")

    
# 标记测试类
@pytest.mark.order
class TestOrder:
    
    def test_order(self):
        print("下单")

    def test_pay(self):
        print("支付")

        
# 多个标签 
@pytest.mark.smoke
@pytest.mark.login
def test_login():
    print("登录")

给测试类打标签,还有另外一种方式,如下:

# 标记测试类(单个标签)
class TestOrder:
    
    # 给类中的所有测试方法打上order标签
    pytestmark = pytest.mark.order
    
    def test_order(self):
        print("下单")

    def test_pay(self):
        print("支付")
    
    
# 标记测试类(多个标签)
class TestOrder:
    
    # 给类中的所有测试方法打上order、smoke标签
    pytestmark = [pytest.mark.order, pytest.mark.smoke]
    
    def test_order(self):
        print("下单")

    def test_pay(self):
        print("支付")

同样可以使用pytestmark标记模块,给模块中所有的测试类、测试函数打上标签,如下:

import pytest

# 模块中的所有测试函数、测试类都会被打上order、smoke标签
pytestmark = [pytest.mark.order, pytest.mark.smoke]

def test_01():
    print("执行test_01")

def test_02():
    print("执行test_02")


class TestOrder:
    def test_order(self):
        print("下单")

    def test_pay(self):
        print("支付")

执行方法

执行的时候加上参数-m标签名即可。

命令行

# 执行被标记为smoke的用例
pytest -m smoke

# 执行被标记为smoke且被标记为login的用例
pytest -m "smoke and login"

# 执行被标记为smoke或login的用例
pytest -m "smoke or login"

代码执行

# 执行被标记为smoke的用例
pytest.main(['-m smoke'])

# 执行被标记为smoke或order的用例
pytest.main(["-m", "smoke or order"])

# 执行被标记为smoke同时被标记为login的用例
pytest.main(["-m", "smoke and login"])

# 执行被标记为smoke且不被标记为login的用例
pytest.main(["-m", "smoke and not login"])

这里需要注意,在测试模块中直接使用pytest.main()执行当前模块中的被打标签的用例是无效的,这样会执行当前模块中的所有测试用例。如下示例:

import pytest

# 标记测试函数
@pytest.mark.smoke
def test_01():
    print("执行test_01")

def test_02():
    print("执行test_02")

    
# 标记测试类
@pytest.mark.order
class TestOrder:
    
    def test_order(self):
        print("下单")

    def test_pay(self):
        print("支付")

        
# 多个标签 
@pytest.mark.smoke
@pytest.mark.login
def test_login():
    print("登录")
    
if __name__ == '__main__':
    pytest.main(['-s', '-m smoke'])

运行该模块,结果如下:

从结果中可以看出,虽然代码中写了只执行标记为smoke的用例,但所有5条用例都被执行了,不能进行过滤。

我们需要将执行代码分离出来,放在单独的执行模块里面,如放在run.py,代码如下:

# run.py

import pytest

if __name__ == '__main__':
    pytest.main(["-s", "-m", "smoke or order"])

运行结果如下:

从结果可以看出来,这里只运行了标记为smokeorder的测试用例。

标记跳过

有时候我们需要跳过某些测试用例不去执行,如代码更新后老的用例不需要执行了,或者在某些特定场景下不需要执行某些用例,这时就需要给对应的测试用例做标记跳过处理。

pytest中提供了两种标记跳过的方法,如下:

  1. 直接跳过,@pytest.mark.skip(reason="跳过原因"),reason可写可不写。
  2. 条件跳过,即满足某个条件则跳过不执行,@pytest.mark.skipif(b>3, reason="跳过原因")

示例如下:

import pytest

@pytest.mark.skip(reason="不需要执行test_01")
def test_01():
    print("执行test_01")

@pytest.mark.skip(2>1, reason="如果2大于1则跳过不执行")
def test_02():
    print("执行test_02")
    
    
if __name__ == '__main__':
    pytest.main(['-s'])

运行结果:

从运行结果中可以看到,这2条用例都被跳过。如果想要 跳过测试类 或 测试模块,方法同上面给测试类、测试模块打标签的方法一样,不做过多说明。

xfail(标记为预期失败)