pytest系列教程——5、跳过测试

对于那些尚未开发完成的测试,最好的处理方式就是略过而不执行测试。
按正向的思路,我们只要通过标记指定要测试的就可以解决这个问题;但有时候的处境是我们能进行反向的操作才是最好的解决途径,即通过标记指定要跳过的测试。

skip

Pytest 使用特定的标记 pytest.mark.skip完美的解决了这个问题。官方Api说明:

pytest.mark.skip(reason=None)
  • reason (str) – Reason why the test function is being skipped.

使用案例:

import pytest


@pytest.mark.skip(reason='跳过执行测试')
def test_skip_01():
    print("test_skip_01 跳过执行")


def test_skip_02():
    print("test_skip_02 正常执行")


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

执行结果:

collected 2 items

MyPytest.py stest_skip_02 正常执行
.

======================== 1 passed, 1 skipped in 0.07s =========================

***Repl Closed***

skipif

skipif则加入了condition,为测试函数指定被忽略的条件。

pytest.mark.skipif(condition, *, reason=None)
  • condition (bool or str) – True/False if the condition should be skipped or a condition string.
  • reason (str) –Reason why the test function is being skipped.

举个例子,比如我希望测试代码运行在python3.0 以下的版本:

import pytest
import sys

major_version = sys.version_info.major


@pytest.mark.skipif(major_version >= 3, reason='当前python版本号大于3,跳过执行测试')
def test_skipif_01():
    print("test_skipif_01 跳过执行")


def test_skipif_02():
    print("test_skipif_02 正常执行")


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

执行结果:

collected 2 items

MyPytest.py stest_skipif_02 正常执行
.

======================== 1 passed, 1 skipped in 0.07s =========================

***Repl Closed***

pytest.skip() 和@pytest.mark.skip()区别

pytest.skip(reason[, allow_module_level=False, msg=None])[source]
  • reason (str) – The message to show the user as reason for the skip.
  • allow_module_level (bool) – Allows this function to be called at module level, skipping the rest of the module. Defaults to False.
  • msg (Optional[str]) – Same as reason, but deprecated. Will be removed in a future version, use reason instead.

*官网的最新文档(https://docs.pytest.org/en/7.1.x/reference/reference.html?highlight=skip#pytest-skip)解释说用reason 后续替代msg参数。根据自己现有的版本进行参数尝试(我的pytest版本是6.2.5)

pytest.skip() 可以用在测试用例中执行跳过,而@pytest.mark.skip()是装饰器,用在整条用例上,不要混淆哦。看例子:

import pytest
import sys

major_version = sys.version_info.major


@pytest.mark.skipif(major_version >= 3, reason='当前python版本号大于3,跳过执行测试')
def test_skipif_01():
    print("test_skipif_01 跳过执行")


def test_skipif_02():
    print("test_skipif_02 正常执行")
    assert(1 == 1)
    
    #后面不再执行
    pytest.skip()
    assert(1 == 2)


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

运行结果:

collected 2 items

MyPytest.py stest_skipif_02 正常执行
s

============================= 2 skipped in 0.07s ==============================

***Repl Closed***

pytest.skip() 也可以通过allow_module_level来控制跳过用例的作用范围。当allow_module_levelTrue的情况下,则整个模块的用例都不执行。举例子:

import pytest
import sys


major_version = sys.version_info.major

if major_version == 3:
    pytest.skip(msg='python 3 版本的时候,跳过本模块的用例', allow_module_level=True)


def test_skipif_01():
    print("test_skipif_01 正常执行")
    assert(1 == 1)


class Test_Skip():
    def test_skip_03(self):
        pass

    def test_skip_04(self):
        pass


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

执行结果:

collected 0 items / 1 skipped                                                  

============================= 1 skipped in 0.02s ==============================

Process finished with exit code 0

Skipped: python 3 版本的时候,跳过本模块的用例
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件测试技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值