pythonpython-2.7pytest

Is there a way to exit a pytest test and continue to the next one?


Say I have 2 tests defined. Is there a way to exit from the first test (for instance if a condition is True) without executing the rest of the test's code and proceed to the following test? (Something like the continue statement from loops, but instead of loops, continue to the next test)


Solution

  • You can always return from your test on basis of some condition, which would make it exit without executing any code that follows.

    You can selectively run tests in pytest for debugging purposes, if that's what you mean -

    py.test mytestfile.py -k <pattern>
    

    Otherwise, the thing is people don't really like to spend too much time fixing tests. So tests need to be very simple and well written even more than the code they are testing, as no one is going to write tests to test the tests. Even if such random behaviour were possible, I won't recommend it.