Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail remaining pytest tests if a specific one fails

So I have a directory filled with a bunch of tests written in python with the proper syntax to make sure they run in order.

So let's say I have a test which if fails, currently calls pytest.exit('Exit Message'). The problem with this is that the generated test output in XML only records the tests preceding it. I would prefer that the whole suite is run but are reported as failed if the test mentioned above fails.

A solution I thought of was setting an environment variable in case it fails and then checking that environment variable in the following tests. The issue is that running it with Jenkins, the environment variable set isn't detected and I would prefer a native solution if it exists.

What I have is:

def test_check_connection(self):
    ...
    if Failed:
        pytest.exit('No connectivity')
like image 923
AsadSMalik Avatar asked Dec 05 '14 14:12

AsadSMalik


People also ask

Which pytest argument is used to stop the test execution after a certain number of failures?

PyTest offers a command-line option called maxfail which is used to stop test suite after n test failures.

How do you run failed test cases in pytest?

The plugin provides two command line options to rerun failures from the last pytest invocation: --lf , --last-failed - to only re-run the failures. --ff , --failed-first - to run the failures first and then the rest of the tests.

How do I run a specific test case in pytest?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

How do you run a failed test case in Python?

Debug failed testsGo to Build, Execution, Deployment | Python Debugger and select the Drop into debugger on failed tests checkbox.


1 Answers

I'm not sure I understand your problem correctly. If you want pytest to stop after the first failing test case use the -x option

pytest -x ...

If you want to run all tests and want to know if there were any failures from jenkins check the exit code of the pytest app:

Running pytest can result in six different exit codes:

Exit code 0: All tests were collected and passed successfully

Exit code 1: Tests were collected and run but some of the tests failed

Exit code 2: Test execution was interrupted by the user

Exit code 3: Internal error happened while executing tests

Exit code 4: pytest command line usage error

Exit code 5: No tests were collected

like image 69
Caner Avatar answered Oct 08 '22 05:10

Caner