霍格沃兹测试开发学社

《Python测试开发进阶训练营》(随到随学!)
2023年第2期《Python全栈开发与自动化测试班》(开班在即)
报名联系weixin/qq:2314507862

pytest Mark标记测试用例

Mark标记测试用例

  • 场景:只执行符合要求的某一部分用例,可以把一个web项目划分为多个模块,然后指定模块名称执行。
  • 解决:在测试用例方法上加@pytest.mark.标签名
  • 执行:-m执行自定义标记的相关用例

pytest -s test_command_param.py -m=webtest
pytest -s test_command_param.py -m apptest
pytest -s test_command_param.py -m “not ios”

import pytest

def double(a):
    return a * 2

# 测试数据:整型
@pytest.mark.int
def test_double_int():
    print("test double int")
    assert 2 == double(1)

# 测试数据:负数
@pytest.mark.minus
def test_double_minus():
    print("test double minus")
    assert -2 == double(-1)

# 测试数据:浮点数
@pytest.mark.float
def test_double_float():
    print("test double float")
    assert 0.2 == double(0.1)

@pytest.mark.float
def test_double2_minus():
    print("test double float")
    assert -10.2 == double(0.2)

@pytest.mark.zero
def test_double_0():
    assert 10 == double(0)

@pytest.mark.bignum
def test_double_bignum():
    assert 200 == double(100)

@pytest.mark.str
def test_double_str():
    assert 'aa' == double('a')

@pytest.mark.str
def test_double_str1():
    assert 'a$a$' == double('a$')

跳过(Skip)及预期失败(xFail)

这是pytest的内置标签,可以处理一些特殊的测试用例,不能成功的测试用例等

skip:始终跳过该测试用例。采用添加装饰器@pytest.mark.skip,或者添加跳过代码pytest.skip(reason)等两种方式。

skipif:遇到特定情况,跳过该测试用例。采用添加装饰器@pytest.mark.skipif的方式。

xFail:遇到特定情况,产生一个“期望失败”的输出。采用添加装饰器@pytest.mark.xfail,或者添加跳过代码pytest.xfail(reason)等两种方式。

posted @ 2024-05-14 18:23  霍格沃兹测试开发学社  阅读(1)  评论(0编辑  收藏  举报