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

2024-09-28 21:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

如果某个特定的测试失败了,那么跳过每个剩余测试的最佳方法是什么_在线.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


Tags: 方法pytestpytestservicetestsmonitoronline
3条回答

从文档: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

你应该看看pytest依赖插件: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

在本例中,如果测试\u b失败,则不会执行测试\u d

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

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

相关问题 更多 >