long running py.test stop at first failure

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?


Solution 1:

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

See the pytest documentation.

Solution 2:

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:

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.