pytest【跳过测试和预期失败】

本文介绍了pytest框架中用于跳过测试的skip、skipif和预期失败的xfail标记。skip和skipif装饰器允许跳过不想执行的测试,skipif支持基于Python表达式的条件判断。xfail则表示预期测试会失败,分为实际失败(XFAIL)和未失败但预期失败(XPASS)两种情况。文章通过示例展示了这些标记的使用效果,并强调了添加明确跳过理由的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pytest自身有一些内置标记:skip 、skipif 、xfail等;sikp和skipif:是允许你跳过不希望运行的测试 ;xfail:是运行测试,但我们预期它会失败。

要跳过某个测试,只需要简单的在测试函数上方添加:@pytest.mark.skip()装饰器即可

import pytest


@pytest.mark.skip(reason='跳过测试')
@pytest.mark.smoking
def test_run_pass():
    expected = (1, 2, 3)
    assert expected == (1, 2, 3)

@pytest.mark.get_tests
@pytest.mark.get_testing
@pytest.mark.smoking
def test_run_fail():
    expected = (1, 2, 3)
    assert expected == (1, 2, 3)

运行后的展示效果:

F:\TESTING\BlogPosts\ReadPytest>pytest -v test_two.py
================================================================================== test session starts ===================================================================================
collected 2 items                                                                                                                                                                         

test_two.py::test_run_pass SKIPPED                                                                                                                                                  [ 50%]
test_two.py::test_run_fail PASSED                                                                                                                                                   [100%]

============================================================================== 1 passed, 1 skipped in 0.05s ==============================================================================

如果要添加跳过的条件时,这时应当使用skipif()来代替skip()

skipif()的判断条件可以是任何Python表达式

import pytest


class TestThree:

    _version = '2.5.0'
    expected_result = [i for i in range(1, 5)]

    @pytest.mark.get_testing
    def test_01_three_pass(self):
        actual_result = tuple(i for i in range(1, 5))
        assert actual_result != self.expected_result

    @pytest.mark.skipif(_version == '2.5.0', reason='跳过测试')
    def test_02_three_fail(self):
        actual_result = tuple(i for i in range(1, 5))
        assert actual_result == self.expected_result

    def test_03_four_pass(self):
        actual_result = [i for i in range(1, 5)]
        assert actual_result == self.expected_result

运行后的展示效果:

F:\TESTING\BlogPosts\ReadPytest>pytest -v test_three.py
================================================================================== test session starts ===================================================================================
collected 3 items                                                                                                                                                                         

test_three.py::TestThree::test_01_three_pass PASSED                                                                                                                                 [ 33%]
test_three.py::TestThree::test_02_three_fail SKIPPED                                                                                                                                [ 66%]
test_three.py::TestThree::test_03_four_pass PASSED                                                                                                                                  [100%]

============================================================================== 2 passed, 1 skipped in 0.03s ==============================================================================


无论是skip()还是skipif(),都可以写跳过理由,尽管没有强制要求,但是写上也是方便以后代码更好阅读,最好理由都写得清清楚楚 。

使用 -rs(查看测试函数跳过的原因) 命令后,运行后的展示效果:
STING\BlogPosts\ReadPytest>pytest -v -rs
================================================================================== test session starts ===================================================================================
collected 8 items                                                                                                                                                                         

test_one.py::test_recursion_depth SKIPPED                                                                                                                                           [ 12%]
test_three.py::TestThree::test_01_three_pass PASSED                                                                                                                                 [ 25%]
test_three.py::TestThree::test_02_three_fail SKIPPED                                                                                                                                [ 37%]
test_three.py::TestThree::test_03_four_pass PASSED                                                                                                                                  [ 50%]
test_two.py::test_run_pass SKIPPED                                                                                                                                                  [ 62%]
test_two.py::test_run_fail PASSED                                                                                                                                                   [ 75%]
ch1/test_five.py::TestFive::test_01_five_pass PASSED                                                                                                                                [ 87%]
ch2/test_six.py::TestSix::test_01_six_pass PASSED                                                                                                                                   [100%]

================================================================================ short test summary info =================================================================================
SKIPPED [1] test_one.py:17: 跳过测试
SKIPPED [1] test_three.py:22: 跳过测试
SKIPPED [1] test_two.py:12: 跳过测试
============================================================================== 5 passed, 3 skipped in 0.07s ==============================================================================

skip()和skipif()标记,测试时会直接跳过,而不被执行;xfail标记,则是运行测试,但我们预期它会失败

import pytest


class TestThree:

    expected_result = [i for i in range(1, 5)]

    @pytest.mark.xfail(reason='跳过测试')
    @pytest.mark.get_testing
    def test_01_three_pass(self):
        actual_result = tuple(i for i in range(1, 5))
        assert actual_result != self.expected_result

    @pytest.mark.xfail(reason='跳过测试')
    def test_02_three_fail(self):
        actual_result = tuple(i for i in range(1, 5))
        assert actual_result == self.expected_result

    def test_03_four_pass(self):
        actual_result = [i for i in range(1, 5)]
        assert actual_result == self.expected_result

test_01_three_pass()断言的实际结果和预期结果不相等,实际也不相等的,所以必定是运行成功
test_02_three_fail() 断言后的实际结果和预期结果相等,实际是不相等的,所以必定是运行失败

运行后的展示效果:

F:\TESTING\BlogPosts\ReadPytest>pytest test_three.py
================================================================================== test session starts ===================================================================================
collected 3 items                                                                                                                                                                         

test_three.py Xx.                                                                                                                                                                   [100%]

======================================================================== 1 passed, 1 xfailed, 1 xpassed in 0.06s =========================================================================

运行成功后我们看到了 X和x,X代表XPASS,表示“ 预期失败,但实际运行并没有失败 ”;x代表XFAIL ,表示“ 预期失败,实际上也失败了 ”


下面我再加上 --verbose选项,来逐行看下信息
F:\TESTING\BlogPosts\ReadPytest>pytest -v test_three.py
================================================================================== test session starts ===================================================================================
collected 3 items                                                                                                                                                                         

test_three.py::TestThree::test_01_three_pass XPASS                                                                                                                                  [ 33%]
test_three.py::TestThree::test_02_three_fail XFAIL                                                                                                                                  [ 66%]
test_three.py::TestThree::test_03_four_pass PASSED                                                                                                                                  [100%]

======================================================================== 1 passed, 1 xfailed, 1 xpassed in 0.10s =========================================================================

运行成功后直接看到了 XPASS、XFAIL 两个标记,意思就和上面的是一致的,就不再多作解释 。


以上就是pytest中跳过和预期失败测试,如总结有不当之处,还请多多赐教,始终相信你的努力,终会在某一天得到回报!!!

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值