[关闭]
@songying 2018-11-20T10:19:04.000000Z 字数 1243 阅读 1002

Skip and xfail: dealing with tests that cannot succeed

pytest


  1. pytest -rxXs
  2. # show extra info on xfailed, xpassed, and skipped tests

Skiping test functions

  1. @pytest.mark.skip(reason="no way of currently testing this")
  2. def test_the_unknown():
  3. ...
  1. def test_function():
  2. if not valid_config():
  3. pytest.skip("unsupported configuration")
  1. import pytest
  2. if not pytest.config.getoption("--custom-flag"):
  3. pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True)

skipif

在某些条件下才进行skip, 那么则使用skipif

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

如果条件为真, 则该test function将会被skip。

  1. import mymodule
  2. minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1,1), reason="at least mymodule-1.1 required")
  3. @minversion
  4. def test_function():
  5. ...
  1. # test_myothermodule.py
  2. from test_mymodule import minversion
  3. @minversion
  4. def test_anotherfunction():
  5. ...

Skip all test functions of a class or module

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

如果条件为真, the marker 将跳过该class中的所有test 方法

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注