如果一项测试失败,pytest跳过所有内容

时间:2019-08-12 13:22:37

标签: python pytest

如果特定测试失败(这里test_002_wips_online.py失败,则没有必要继续运行),跳过所有剩余测试的最佳方法是什么:

tests/test_001_springboot_monitor.py::TestClass::test_service_monitor[TEST12] PASSED [  2%]
tests/test_002_wips_online.py::TestClass::test01_online[TEST12] FAILED   [  4%]
tests/test_003_idpro_test_api.py::TestClass::test01_testapi_present[TEST12] PASSED [  6%]

我想跳过所有剩余的测试并编写测试报告。 我应该写一个状态文件并写一个检查它的函数吗?

@pytest.mark.skipif(setup_failed(), reason="requirements failed")

pytest-skipif-reference

3 个答案:

答案 0 :(得分:2)

您应该真正看一下pytest-dependency插件:https://pytest-dependency.readthedocs.io/en/latest/usage.html

import pytest

@pytest.mark.dependency()
def test_b():
    pass

@pytest.mark.dependency(depends=["test_b"])
def test_d():
    pass

在此示例中,如果test_b失败,则不会执行test_d

答案 1 :(得分:2)

来自文档:http://pytest.org/en/latest/usage.html#stopping-after-the-first-or-n-failures

pytest -x            # stop after first failure
pytest --maxfail=2    # stop after two failures

答案 2 :(得分:0)

我发现,如果任何关键的预定义测试失败,则调用pytest.exit("error message")是最方便的。 pytest将完成所有发布工作,例如html报告,屏幕截图,并且您的错误消息将在末尾显示:

!!!!!!!!!!! _pytest.outcomes.Exit: Critical Error: wips frontend in test1 not running !!!!!!!!!!!
===================== 1 failed, 8 passed in 92.72 seconds ======================