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)等两种方式。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示