本文已参与「新人创作礼」活动,一起开启掘金创作之路。
2.13 skip和xfail处理不能成功的测试用例
标记不能在某些平台上运行或您希望失败的测试函数,windows怎么激活以便pytest可以相应地处理它们,并呈现测试会话的摘要,同时保持测试套件的绿appstore色,跳过windows7旗舰版意味着您只有在满足某些条件时才期approve望测试通过,否则pytest应该完全跳过运行测试。
常见的例子是跳过非winwindows11有必要升级吗dows平台上的仅windows测试
xfail意味着您期望测试由于某种原因而失败。还有一个常见的例子是对尚未实现的特性或尚未修复的错误的测试。
当测试通过,尽管预期失败(用pytest.mark.xfail标记)时,它是xpass,将在测试摘要中报告。
2.13APP.1 跳过测试用例
1. 装饰器标记跳过,并传递跳过原因:
import pytest @pytest.mark.skip(reason="no way of currently testing this") def test_the_unknown(): ...
2. pytest.skip 强制性跳过
def test_function(): if not valid_config(): pytest.skip("unsupported configuration")
3. 参数化中跳过用例:
import pytest @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): pytest.skip("跳过用例")
4. 跳过平台是win的所有用例
import sys import pytest if not sys.platform.startswith("win"): pytest.skip("skipping windows-only tests", allow_module_level=True)
5. 有条件的跳过用例
如果您希望有条件地跳过某些内容,下面是一个标记在Pyapproachthon3.10之前的解释器上运行时要跳过的测试函数的例子:
import sys @pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher→") def test_function(): ...
6. 基于某些条件跳过某些测python123平台登录试用例
在模块(py文件)之间共享 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(): pass
# test_myothermodule.py from test_mymodule import minversion @minversion def test_anotherfunction(): pass
7. 跳过一个测试手机是否被监控类windows键是哪个,在类上使用 skipif 标记
import pytest, sys @pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") class TestPosixCalls: def test_function(self): "will not be setup or run under 'win32' platform"
8. 跳过一个模块的所有测试函数,您可以使用全局测试标记
import pytest # test_module.py pytestmark = pytest.mark.skipif(...)
9. 跳过文件或目录:直接pytest后面跟着对应目录的用例就可以了
2.13.2 将测试用例标记为失败
1. 使python培训用xfail标记来指示您windows是什么意思希望测试失败:
@pytest.mark.xfail def test_function(): ...
2. 运行测试过appear程中设置失败
在pappointmentytest.xfailpython基础教程()调用之后没有执行其他代码。这是因为它是通过引发一个已知的异常而在内python安装教程部实现的。
import pytest @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): pytest.xfail("预期失败")
3. 在某个条件、原因下失败
@pytest.mark.xfail(sys.platform == "win32", reason="bug in a 3rd party library") def test_function(): ...
4. 禁止运行标记的用例
@pytest.mark.xfail(run=False) def test_function(): ...
5. 设置strict使xfail的测试用例运行结果为失败
import pytest @pytest.mark.xfail(strict=True) def test_function(): pass
[pytest] xfail_strict=true
6. 相关windows11好用吗代码示例
import pytest xfail = pytest.mark.xfail @xfail def test_hello(): assert 0 @xfail(run=False) def test_hello2(): assert 0 @xfail("hasattr(os, 'sep')") def test_hello3(): assert 0 @xfail(reason="bug 110") def test_hello4(): assert 0 @xfail('pytest.__version__[0] != "17"') def test_hello5(): assert 0 def test_hello6(): pytest.xfail("reason") @xfail(raises=IndexError) def test_hello7(): x = [] x[1] = 1
C:UsersmcDesktoppython基础>pytest -rx test_module.py ======================================================================= test session starts ======================================================================== platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1 rootdir: C:UsersmcDesktoppython基础 collected 7 items test_module.py xxxxxxx [100%] ===================================================================== short test summary info ====================================================================== XFAIL test_module.py::test_hello XFAIL test_module.py::test_hello2 reason: [NOTRUN] XFAIL test_module.py::test_hello3 condition: hasattr(os, 'sep') XFAIL test_module.py::test_hello4 bug 110 XFAIL test_module.py::test_hello5 condition: pytest.__version__[0] != "17" XFAIL test_module.py::test_hello6 reason: reason XFAIL test_module.py::test_hello7 ======================================================================== 7 xfailed in 0.19s ========================================================================
2.13.3 使用参数化跳过/退出
import sys import pytest @pytest.mark.parametrize( ("n", "expected"), [ (1, 2), pytest.param(1, 0, marks=pytest.mark.xfail), pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")), (2, 3), (3, 4), (4, 5), pytest.param(10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k→")), ], ) def test_increment(n, expected): assert n + 1 == expected
C:UsersmcDesktoppython>pytest -vs test_module.py ======================================================================= test session starts ======================================================================== platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1 -- c:python39python.exe cachedir: .pytest_cache rootdir: C:UsersmcDesktoppython collected 7 items test_module.py::test_increment[1-2] PASSED test_module.py::test_increment[1-0] XFAIL test_module.py::test_increment[1-3] XFAIL (some bug) test_module.py::test_increment[2-3] PASSED test_module.py::test_increment[3-4] PASSED test_module.py::test_increment[4-5] PASSED test_module.py::test_increment[10-11] SKIPPED (py2k→) ============================================================= 4 passed, 1 skipped, 2 xfailed in 0.12s ==============================================================
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)