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

2022-06-08 15:45:03 浏览数 (1)

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

skip

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

代码语言:javascript复制
pytest.mark.skip(reason=None)
  • reason (str) – Reason why the test function is being skipped.

使用案例:

代码语言:javascript复制
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'])

执行结果:

代码语言:javascript复制
collected 2 items

MyPytest.py stest_skip_02 正常执行
.

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

***Repl Closed***

skipif

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

代码语言:javascript复制
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 以下的版本:

代码语言:javascript复制
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'])

执行结果:

代码语言:javascript复制
collected 2 items

MyPytest.py stest_skipif_02 正常执行
.

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

***Repl Closed***

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

代码语言:javascript复制
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()是装饰器,用在整条用例上,不要混淆哦。看例子:

代码语言:javascript复制
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'])

运行结果:

代码语言:javascript复制
collected 2 items

MyPytest.py stest_skipif_02 正常执行
s

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

***Repl Closed***

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

代码语言:javascript复制
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'])

执行结果:

代码语言:javascript复制
collected 0 items / 1 skipped                                                  

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

Process finished with exit code 0

Skipped: python 3 版本的时候,跳过本模块的用例

0 人点赞