Can I skip a test (or mark it as inconclusive) in pytest while it is running?

Question:

I am aware of pytest‘s decorators to mark tests as to be skipped (conditionally). However, all those are evaluated before the test starts.

I have a couple of tests that require a user interaction (those are not run on CI obviously), and if that interaction is not provided, I would like to mark these tests as, well, anything but "pass" or "fail". "Skipped" would be fine, as would be "inconclusive" or "canceled" or whatever.

Is that possible?

Asked By: bers

||

Answers:

In pytest, it is possible to dynamically skip a test during its execution by employing the pytest.mark.skip decorator within the test itself when specific conditions are not met. If you have tests demanding user interaction and wish to label them as something other than a straightforward "pass" or "fail" when such interaction is absent, you can implement an in-test conditional assessment.

In cases where the essential interaction is unavailable, you can invoke pytest.skip("Indicate reason for skipping") within the test function. This approach allows you to signify that the test was bypassed due to the absence of prerequisites for a definite pass or fail outcome, and the result will be logged as "skipped" in the test summary.

Answered By: BamGbam

It turns out you can call pytest.skip() from inside a test as well.

try:
    ...
except some.TimeoutException:
    pytest.skip("Timeout waiting for user interaction")

works fine.

Answered By: bers
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.