跳过用例skip

1、装饰器,放在函数前面,跳过用例 @pytest.mark.skip(reason="no way of currently testing this")

import pytest

def test1():
    print('操作1')
    print("-----------------------------------------------")

@pytest.mark.skip(reason="no way of currently testing this")
def test12():
    print('操作2')
    print("-----------------------------------------------")


def test3():
    print('操作3')

    print("-----------------------------------------------")


if __name__ == '__main__':
    pytest.main(['-s', "text.fix2.py"])

2、放在函数里面,只控制某条用例

import pytest


def test_123():
    pytest.skip("Not implemented")
    assert 1 == 0


def test_234():
    assert 1 == 1

3、跳过某个模块

@pytest.importskip("模块名")

4、根据版本去控制跳过某个模块

@pytest.importskip("模块名",minversion="version_num")

5、如果您希望有条件地跳过某些内容,则可以使用skipif代替

import sys
@pytest.mark.skipif(sys.version_info < (3,6),
reason="requires python3.6 or higher")
def test_function():
    ...

如果条件在收集期间评估为True,则将跳过测试函数,具有指定的原因使用-rs时出现在摘要中。

您可以在模块之间共享skipif标记。参考以下案例

# content of test_mymodule.py
import mymodule
minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1,1),
reason="at least mymodule-1.1 required")
@minversion
def test_function():
    ...

您可以导入标记并在另一个测试模块中重复使用它:

# test_myothermodule.py
from test_mymodule import minversion
@minversion
def test_anotherfunction():
    ...

对于较大的测试套件,通常最好有一个文件来定义标记,然后一致适用于整个测试套件。

或者,您可以使用条件字符串而不是布尔值,但它们之间不能轻易共享它们支持它们主要是出于向后兼容的原因

6、skip类或模块

@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows")
class TestPosixCalls(object):
    def test_function(self):
        "will not be setup or run under 'win32' platform"

警告:强烈建议不要在使用继承的类上使用skipif。 pytest中的一个已知错误标记可能会导致超类中的意外行为。

 

转载于:https://www.cnblogs.com/guo2733/p/10536060.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值