简体   繁体   中英

pytest skip everything if one test fails

What is best way to skip every remaining test if a specific test fails, here test_002_wips_online.py failed, and then there is no point in running further:

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%]

I like to skip all remaining tests and write test report. Should I write to a status file and write a function that checks it?

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

pytest-skipif-reference

You should really look at pytest-dependency plugin: 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

in this example test_d won't be executed if test_b fails

from the docs: 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

I found that calling pytest.exit("error message") if any of my critical predefine tests fail is the most convenient. pytest will finish all post jobs like html-report, screenshots and your error message will be printed at the end:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM