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

pytest【跳过测试和预期失败】本文介绍了 pytest 框架中用于跳过测试的 skip skipif 和预期失败的 xfail 标记

大家好,欢迎来到IT知识分享网。

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)

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/143694.html

(0)
上一篇 2025-05-01 20:00
下一篇 2025-05-01 20:10

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信