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

pytest_runtest_logreport does not report "teardown" when using pytest-rerunfailures #237

Open BeyondEvil opened 9 months ago

BeyondEvil commented 9 months ago

Description

When using the pytest-rerunfailures plugin and marking a test as "flaky", the pytest_runtest_logreport-hook is only called for the last occurrence of a "teardown" despite it still being run (evident in the output below).

pip list

Package Version
exceptiongroup 1.1.3
iniconfig 2.0.0
packaging 23.2
pip 21.2.4
pluggy 1.3.0
pytest 7.4.2
pytest-rerunfailures 12.0
setuptools 58.1.0
tomli 2.0.1

Python: 3.9.10 OS: osx 12.3 (M1 MacBook Pro)

Example:

# conftest.py
import pytest

@pytest.hookimpl(trylast=True)
def pytest_runtest_logreport(report):
    print("\nWHEN: ", report.when)

# test_reportlog.py
import pytest

@pytest.fixture
def fix():
    assert True
    yield
    print("\nTEARDOWN CALLED")
    assert True

@pytest.mark.flaky(reruns=1)
def test_logreport(fix):
    assert False

Output:

$ pytest -s
============================= test session starts ==============================
platform darwin -- Python 3.9.10, pytest-7.4.2, pluggy-1.3.0
rootdir: /Users/itsme/dev/pytest-logreport-rerunfailure
plugins: rerunfailures-12.0
collected 1 item

test_reportlog.py 
TEARDOWN CALLED

WHEN:  setup
R
WHEN:  call

TEARDOWN CALLED

WHEN:  setup
F
WHEN:  call

WHEN:  teardown

=================================== FAILURES ===================================
________________________________ test_logreport ________________________________

fix = None

    @pytest.mark.flaky(reruns=1)
    def test_logreport(fix):
>       assert False
E       assert False

test_reportlog.py:12: AssertionError
=========================== short test summary info ============================
FAILED test_reportlog.py::test_logreport - assert False
========================== 1 failed, 1 rerun in 0.02s ==========================
BeyondEvil commented 9 months ago

Changing the example code to fail in the teardown instead:

import pytest

@pytest.fixture
def fix():
    assert True
    yield
    print("\nTEARDOWN CALLED")
    assert False

@pytest.mark.flaky(reruns=1)
def test_logreport(fix):
    assert True

yields the expected output:

============================= test session starts ==============================
platform darwin -- Python 3.9.10, pytest-7.4.2, pluggy-1.3.0
rootdir: /Users/jimbrannlund/dev/pytest-logreport-rerunfailure
plugins: rerunfailures-12.0
collected 1 item

test_reportlog.py 
TEARDOWN CALLED

WHEN:  setup
.
WHEN:  call
R
WHEN:  teardown

TEARDOWN CALLED

WHEN:  setup
.
WHEN:  call
E
WHEN:  teardown

==================================== ERRORS ====================================
_____________________ ERROR at teardown of test_logreport ______________________

    @pytest.fixture
    def fix():
        assert True
        yield
        print("\nTEARDOWN CALLED")
>       assert False
E       assert False

test_reportlog.py:8: AssertionError
=========================== short test summary info ============================
ERROR test_reportlog.py::test_logreport - assert False
===================== 2 passed, 1 error, 1 rerun in 0.02s ======================

So maybe this works as intended (even if I don't quite understand why)?

RonnyPfannschmidt commented 9 months ago

https://github.com/pytest-dev/pytest-rerunfailures/blob/96bbc4fd81e81216205eb7662da7ef234077569a/pytest_rerunfailures.py#L555C24-L555C24

RonnyPfannschmidt commented 9 months ago

so as far as i can tell this is a issue with rerunfailures, not pytest-core

BeyondEvil commented 9 months ago

so as far as i can tell this is a issue with rerunfailures, not pytest-core

Looks like I don't have the right permissions to move this to pytest-rerunfailures.