pytest-dev / pytest-rerunfailures

a pytest plugin that re-runs failed tests up to -n times to eliminate flakey failures
Other
369 stars 82 forks source link

Can you identify if a particular is a re-run or not? #231

Closed tking16 closed 10 months ago

tking16 commented 11 months ago

Hi, I have a quick general question. I'd like to start a screen recording only when a test has failed and has is on a rerun. Is there some way to identify that a test is being reran? I tried request.config.option.lf but it just returns false whether a test is being run for the first time or not!

icemac commented 11 months ago

I do not know about a the possibility to identify that a re-run is currently in progress. There is item.execution_count, see https://github.com/pytest-dev/pytest-rerunfailures/blob/c6ff082a36e927b27fd8418737f49e116d03a22a/pytest_rerunfailures.py#L606C1-L607C1 But you are probably not able to access item during a test run.

The-Compiler commented 10 months ago

But you are probably not able to access item during a test run.

You can, via the request fixture and request.node - e.g.:

def test_rerun(request):
    if request.node.execution_count == 1:
        1/0

results in:

test_x.py::test_rerun RERUN
test_x.py::test_rerun PASSED
icemac commented 10 months ago

Thank you @The-Compiler for sharing your knowledge. I assume the question as answered and will set the issue to resolved.