简体   繁体   中英

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')

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

You could try setting a global variable in a test and referencing it in a later test like so:

def test_check_connection(self):
    ...
    if Failed:
        global failed_conn_check = True
    else:
        global failed_conn_check = False

Then at the end of the tests:

    ...
    if failed_conn_check:
        raise SomeException
    ...

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