前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest Mark标记测试用例

pytest Mark标记测试用例

原创
作者头像
霍格沃兹测试开发Muller老师
发布2024-05-14 18:23:56
760
发布2024-05-14 18:23:56
举报

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”

代码语言:python
复制
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)等两种方式。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Mark标记测试用例
    • 跳过(Skip)及预期失败(xFail)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档