Fail remaining pytest tests if a specific one fails

自作多情 提交于 2019-12-07 07:40:05

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/27318493/fail-remaining-pytest-tests-if-a-specific-one-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!