Pytest系列:csdn最最最详细,听不懂你找我。 skip、skipif跳过用例

目录

前言

skip

skipif

skip类或模块

skip文件或目录

小技巧

跳过测试类

跳过方法或测试用例

多个skip时,满足1个条件即跳过

skip赋值给变量,可多处使用装饰器函数的语法格式调用

pytest.skip()方法内跳过


前言

在我们自动化测试过程中,经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过skip用例,如果我们注释掉或删除掉,后面还要进行恢复操作。

① pytest.mark.skip 装饰器可以标记无法在某些平台上运行的测试功能,或者希望失败的测试功能,使其跳过。

②skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过其运行其他测试。 常见示例是在非Windows平台上跳过仅限Windows的测试,或跳过测试依赖于当前不可用的外部资源(例如数据库)。

③xfail意味着您希望测试由于某种原因而失败。 一个常见的例子是对功能的测试尚未实施,或尚未修复的错误。 当测试通过时尽管预计会失败(标记 pytest.mark.xfail ),它是一个xpass,将在测试摘要中报告。

④pytest计数并分别列出skip和xfail测试。 未显示有关跳过/ xfailed测试的详细信息默认情况下,以避免混乱输出。 可以使用 -r选项查看与 short 字母对应的详细信息显示在测试进度中。

⑤有关-r选项的更多详细信息,请运行 pytest -h 

⑥skip跳过成功的情况,标识为  s  ============================= 2 skipped in 0.04s ==============================

总的来讲:skip和skipif允许你跳过不希望运行的测试。

skip

①跳过测试函数的最简单方法是使用跳过装饰器 pytest.mark.skip标记它,可以传递一个可选的跳过用例的原因,形参是 reason 

代码示例:

@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
    ...

②也可以通过在测试方法执行期间跳过pytest.skip(reason=reason)功能。【一般在测试方法中与 if 分支方法共同使用,表示在测试用例执行期间满足某条件从而跳过。(与skip标记不同的是,标记更像是一个烙印,打上后直接跳过)

代码示例:【注意】此时的 pytest.skip(reason=reason) 已经不再是一个装饰器,而是一个函数;此外需要注意的是, pytest.skip(reason=reason) 放置位置对测试用例的的执行至关重要。如果只放在一个测试用例中,则仅仅会跳过这一个测试用例。

def test_function():
    if not valid_config():
        pytest.skip("unsupported configuration")

③还可以使用 pytest.skip(reason=reason,allow_module_level = True) 跳过整个模块级别

import pytest

if not pytest.config.getoption("--custom-flag"):
    pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True)

当在测试用例收集时间内(还未执行测试用例时)无法实现跳过条件时,命令性方法很有用。【当在用例收集的时候因为某些原因导致测试用例无法跳过,使用命令性方法更方便。使用装饰器反而达不到测试用例跳过的需求

skipif

①如果希望有条件地跳过某些内容,则可以使用 pytest.mark.skipif 装饰器代替。

示例:标记测试的示例在Python3.6之前的解释器上运行时要跳过的函数。

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():
    ...

对于较大的测试套件,通常最好有一个专门的模块来定义标记,然后在测试用例模块导入并引用【这样做的好处:方便管理和查看测试用例的标记】;这种方法一致适用于整个测试套件。

skip类或模块

①在类上使用 skipif 标记。

示例:如果 pytest.mark.skipif 装饰器跳过条件为True,则此标记将为该类的每个测试方法生成跳过结果。

@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中的一个已知错误标记可能会导致超类中的意外行为。

②如果要跳过某个测试模块的所有测试方法,可以在全局级别使用 pytestmark 名称。

示例:

# test_module.py

pytestmark = pytest.mark.skipif(...)

skip文件或目录

有时可能需要跳过整个文件或目录:例如,如果测试依赖于特定于Python的版本功能或包含您不希望pytest运行的代码。

小技巧

这是一个快速指南,介绍如何在不同情况下跳过模块中的测试。

1、无条件地跳过模块中的所有测试:

pytestmark = pytest.mark.skip("all tests still WIP")

2、根据某些条件跳过模块中的所有测试

pytestmark = pytest.mark.skipif(sys.platform == "win32", "tests for linux → only"

3、如果缺少某些导入,则跳过模块中的所有测试

pexpect = pytest.importorskip("pexpect")

--------------------------------------------------------------------------------------------------------------------------------------

跳过测试类

③@pytest.mark.skip() 标签:被标记的类中所有方法测试用例都会被跳过

代码示例:

import pytest


@pytest.mark.skip(reason='跳过Test类,会跳过类中所有方法')
class Test(object):
    def test_one(self):
        assert 1 == 1

    def test_two(self):
        print('test_02')
        assert 1 == 2


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

运行结果:

② @pytest.mark.skipif() 标签:被标记的类,当条件为ture时,会被跳过,否则不跳过

代码示例:

import pytest


@pytest.mark.skipif(1 == 1, reason='跳过Test类,会跳过类中所有方法')
class Test(object):
    def test_one(self):
        assert 1 == 1

    def test_two(self):
        print('test_02')
        assert 1 == 2


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

运行结果:

跳过方法或测试用例

想要某个方法或跳过某条用例,在方法上加以下3种装饰器都可以:

 ①@pytest.mark.skip()  :跳过方法或用例,未备注原因。

代码示例:

import pytest


class Test(object):
    @pytest.mark.skip()
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

运行结果:

 ②@pytest.mark.skip(reason='跳过一个方法或一个测试用例') :跳过方法或用例,备注了原因。

示例代码:

import pytest


class Test(object):
    @pytest.mark.skip(reason='跳过一个方法或一个测试用例')
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

运行结果:

 ③@pytest.mark.skipif(1==1,reason='跳过一个方法或一个测试用例') : 当条件满足,跳过方法或用例,备注了原因。

代码示例:

import pytest


class Test(object):
    @pytest.mark.skipif(1 == 1, reason='跳过一个方法或一个测试用例')
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

运行结果:

多个skip时,满足1个条件即跳过

在类和方法上分别加了skip,类中满足条件,方法中未满足条件,所以生效类中skip。

代码示例:

import pytest


@pytest.mark.skipif(1 == 1, reason='多个条件时,有1个条件满足就跳过(类)')
class Test(object):
    @pytest.mark.skipif(1 == 2, reason='多个条件时,有1个条件满足就跳过(方法)')
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

运行结果:

skip赋值给变量,可多处使用装饰器函数的语法格式调用

①无论是 @pytest.mark.skip() 标签还是 @pytest.mark.skipif() 标签,如果你想在多个测试方法上装饰,依次写起来很麻烦的话,你可以选择定义个变量让该变量等于标签,然后在装饰的时候用该变量代替标签。

②这种方法,还可以通过在其他模块中导入的变量的方式,与其他模块共享标签;

赋值: myskip=pytest.mark.skipif(1==1,reason='skip赋值给变量,可多处调用') 

调用: @myskip 

代码示例1:

import pytest

myskip = pytest.mark.skipif(1 == 1, reason='skip赋值给变量,可多处调用')

class Test(object):
    @myskip
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

运行结果1:

代码示例2:

# test_skipif.py

import platform
import pytest


def is_windows():
    if platform.system() != "Linux":
        return True
    elif platform.system() == "Linux":
        return False


only_windows = pytest.mark.skipif(is_windows(), reason="非Linux系统时,用例均跳过")


class TestMyCode:

    @pytest.mark.skipif(platform.system() != "Linux", reason="非Linux系统时,用例均跳过")
    def test_skipif_001(self):
        """表达式"""
        assert platform.system() == "Windows", platform.system()

    @only_windows
    def test_skipif_002(self):
        """装饰器的方式封装"""
        assert platform.system() == "Linux"


if __name__ == '__main__':
    pytest.main([__file__, '-vs'])

运行结果2:

pytest.skip()方法内跳过

①除了通过使用标签的方式,还可以在测试用例中调用 pytest.skip()方法来实现跳过,可以选择传入 msg参数来说明跳过原因。

②如果想要通过判断是否跳过,可以写在if判断语句里。

import pytest


class Test(object):
    def test_one(self):
        pytest.skip(msg='跳过')
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

运行结果:

 

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值