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中跳过和预期失败测试,如总结有不当之处,还请多多赐教,始终相信你的努力,终会在某一天得到回报!!!