龙空技术网

软件测试丨Pytest学习笔记-Mark标记、Skip跳过、xFail预期失败

测试人666 246

前言:

眼前大家对“pytest print”大体比较着重,看官们都想要知道一些“pytest print”的相关文章。那么小编同时在网摘上网罗了一些对于“pytest print””的相关资讯,希望大家能喜欢,大家一起来学习一下吧!

本文为霍格沃兹测试开发学社学员笔记分享

原文链接:pytest Mark标记、Skip跳过、xFail预期失败 L2 - 学习笔记 - 测试人社区

1、使用 Mark 标记测试用例Mark:标记测试用例场景:只执行符合要求的某一部分用例 可以把一个web项目划分多个模块,然后指定模块名称执行。解决: 在测试用例方法上加 @pytest.mark.标签名执行: -m 执行自定义标记的相关用例pytest -s test_mark_zi_09.py -m=webtest pytest -s test_mark_zi_09.py -m apptest pytest -s test_mark_zi_09.py -m "not ios"

import pytestdef double(a):    return a * 2# 测试数据:整型@pytest.mark.intdef test_double_int():    print("test double int")    assert 2 == double(1)# 测试数据:负数@pytest.mark.minusdef test_double1_minus():    print("test double minus")    assert -2 == double(-1)# 测试数据:浮点数@pytest.mark.floatdef test_double_float():    assert 0.2 == double(0.1)@pytest.mark.floatdef test_double2_minus():    assert -0.2 == double(-0.1)@pytest.mark.zerodef test_double_0():    assert 0 == double(0)@pytest.mark.bignumdef test_double_bignum():    assert 200 == double(100)@pytest.mark.strdef test_double_str():    assert 'aa' == double('a')@pytest.mark.strdef test_double_str1():    assert 'a$a$' == double('a$')复制代码

警告解决办法

2、pytest 设置跳过、预期失败Mark:跳过(Skip)及预期失败(xFail)这是 pytest 的内置标签,可以处理一些特殊的测试用例,不能成功的测试用例skip - 始终跳过该测试用例skipif - 遇到特定情况跳过该测试用例xfail - 遇到特定情况,产生一个“期望失败”输出Skip 使用场景调试时不想运行这个用例标记无法在某些平台上运行的测试功能在某些版本中执行,其他版本中跳过比如:当前的外部资源不可用时跳过如果测试数据是从数据库中取到的,连接数据库的功能如果返回结果未成功就跳过,因为执行也都报错解决 1:添加装饰器@pytest.mark.skip@pytest.mark.skipif解决 2:代码中添加跳过代码pytest.skip(reason)

import pytest@pytest.mark.skipdef test_aaa():    print("代码未开发完")    assert True@pytest.mark.skip(reason="存在bug")def test_bbb():    assert Falseimport pytest## 代码中添加 跳过代码块 pytest.skip(reason="")def check_login():    return Falsedef test_function():    print("start")    # 如果未登录,则跳过后续步骤    if not check_login():        pytest.skip("unsupported configuration")    print("end")复制代码
import sysimport pytestprint(sys.platform)# 给一个条件,如果条件为true,就会被跳过@pytest.mark.skipif(sys.platform == 'darwin', reason="does not run on mac")def test_case1():    assert True@pytest.mark.skipif(sys.platform == 'win', reason="does not run on windows")def test_case2():    assert True@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")def test_case3():    assert True复制代码
xfail 使用场景与 skip 类似 ,预期结果为 fail ,标记用例为 fail用法:添加装饰器@pytest.mark.xfail
import pytest# 代码中标记xfail后,下部分代码就不惠继续执行了def test_xfail():    print("*****开始测试*****")    pytest.xfail(reason='该功能尚未完成')    print("测试过程")    assert 1 == 1# xfail标记的测试用例还是会被执行的,fial了,标记为XFAIL,通过了标记为XPASS,起到提示的作用@pytest.mark.xfaildef test_aaa():    print("test_xfail1 方法执行")    assert 1 == 2xfail = pytest.mark.xfail@xfail(reason="bug 110")def test_hello4():    assert 0

标签: #pytest print