long running py.test stop at first failure

PythonPytest

Python Problem Overview


I am using pytest, and the test execution is supposed to run until it encounters an exception. If the test never encounters an exception it should continue running for the rest of time or until I send it a SIGINT/SIGTERM.

Is there a programmatic way to tell pytest to stop running on the first failure as opposed to having to do this at the command line?

Python Solutions


Solution 1 - Python

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

See the pytest documentation.

Solution 2 - Python

pytest has the option -x or --exitfirst which stops the execution of the tests instanly on first error or failed test.

pytest also has the option --maxfail=num in which num indicates the number of errors or failures required to stop the execution of the tests.

pytest -x            # if 1 error or a test fails, test execution stops 
pytest --exitfirst   # equivalent to previous command
pytest --maxfail=2   # if 2 errors or failing tests, test execution stops

Solution 3 - Python

You can use addopts in pytest.ini file. It does not require invoking any command line switch.

# content of pytest.ini
[pytest]
addopts = --maxfail=2  # exit after 2 failures

You can also set env variable PYTEST_ADDOPTS before test is run.

If you want to use python code to exit after first failure, you can use this code:

import pytest

@pytest.fixture(scope='function', autouse=True)
def exit_pytest_first_failure():
    if pytest.TestReport.outcome == 'failed':
        pytest.exit('Exiting pytest')

This code applies exit_pytest_first_failure fixture to all test and exits pytest in case of first failure.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattView Question on Stackoverflow
Solution 1 - PythonPraveen YalagandulaView Answer on Stackoverflow
Solution 2 - PythonlmiguelvargasfView Answer on Stackoverflow
Solution 3 - PythonSilentGuyView Answer on Stackoverflow