pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试。常见示例是在非windows平台上跳过仅限windows的测试或跳过依赖于当前不可用的外部资源(例如数据库)。
xfail意味着您希望测试由于某种原因而失败。一个常见的例子是对功能的测试尚未实施。或尚未修复的错误。当测试通过时尽管预计会失败(标有pytest.mark.xfail),它是一个xpass,将在测试摘要中报告。
pytest计数并分别列出skip和xfail测试。未显示有关跳过 / xfailed测试的详细信息默认情况下,以避免混乱输出。您可以使用-r选项查看与"short"字母对应的详细信息显示在测试进度中
pytest -rxXs #show extra info on xfailed,xpassed,and skipped tests
一、skip
跳过测试函数的最简单方法就是使用跳过装饰器标记它,可以传递一个可选的原因
import pytest
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
assert 1==1
运行结果
Testing started at 11:15 PM ...
/usr/local/bin/python3.6 /Applications/PyCharm.app/Contents/helpers/pycharm/_jb_pytest_runner.py --target test_class007.py::test_the_unknown
Launching py.test with arguments test_class007.py::test_the_unknown in /Users/luozelin/Desktop/pytest/pytest_demo
============================= test session starts ==============================
platform darwin -- Python 3.6.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: /Users/luozelin/Desktop/pytest/pytest_demo
plugins: html-1.21.1, rerunfailures-7.0, metadata-1.8.0collected 1 item
test_class007.py s
Skipped: no way of currently testing this
[100%]
============================== 1 skipped in 0.02s ==============================
Process finished with exit code 0
或者,也可以通过调用来在测试执行或设置期间强制跳过pytest.skip(reason)功能:
#!encoding=utf-8
import pytest
def add(a,b):
return a+b
def test_function():
c=add(1,2)
if c!=4:
pytest.skip("c等于{},但是不等于4,跳过测试")
if __name__=="__main__":
pytest.main('-s test_class007.py')
运行结果
<