Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest 8.2.2 breaks pytest-rerunfailures for tests that inherit unittest.TestCase #12424

Closed
mdmintz opened this issue Jun 5, 2024 · 4 comments · Fixed by #12436
Closed

pytest 8.2.2 breaks pytest-rerunfailures for tests that inherit unittest.TestCase #12424

mdmintz opened this issue Jun 5, 2024 · 4 comments · Fixed by #12436
Labels
plugin: unittest related to the unittest integration builtin plugin type: regression indicates a problem that was introduced in a release which was working previously

Comments

@mdmintz
Copy link

mdmintz commented Jun 5, 2024

pytest 8.2.2 breaks pytest-rerunfailures for tests that inherit unittest.TestCase

The code: (placed in new_file.py)

from unittest import TestCase

class MyTestClass(TestCase):
    def test_base(self):
        self.fail()

Run command:

pytest new_file.py --reruns=1

Expected output: (Using pytest==8.2.1 / previous version)

self = <examples.new_file.MyTestClass testMethod=test_base>

    def test_base(self):
>       self.fail()
E       AssertionError: None

new_file.py:5: AssertionError

Regression output: (Using pytest==8.2.2 / latest version)

self = <TestCaseFunction test_base>

    def runtest(self) -> None:
        from _pytest.debugging import maybe_wrap_pytest_function_for_tracing
    
        testcase = self.instance
>       assert testcase is not None
E       AssertionError

The error occurs during the rerun of the test.

The failure line should be self.fail(). Instead, it's assert testcase is not None.
This causes the rerun to never happen. (The test only ran the first time)

Tested on a Mac. Here's the pip list:

Package              Version
-------------------- -------
iniconfig            2.0.0
packaging            24.0
pip                  24.0
pluggy               1.5.0
pytest               8.2.2
pytest-rerunfailures 14.0
@hb2638
Copy link

hb2638 commented Jun 6, 2024

Thank you for filing this with succinct replication steps. I spent hours yesterday trying to figure out why our CI/CD was failing

@bluetech
Copy link
Member

bluetech commented Jun 6, 2024

We can probably accommodate rerunfailures here. I will take a look.

@bluetech bluetech added type: regression indicates a problem that was introduced in a release which was working previously plugin: unittest related to the unittest integration builtin plugin labels Jun 6, 2024
@hb2638
Copy link

hb2638 commented Jun 7, 2024

I think switching pytest_runtest_makereport to the "new style" hook will make the issue go away

BEFORE

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    result = outcome.get_result()
    if result.when == "setup":
        # clean failed statuses at the beginning of each test/rerun
        setattr(item, "_test_failed_statuses", {})

        # create a dict to store error-check results for each stage
        setattr(item, "_terminal_errors", {})

    _test_failed_statuses = getattr(item, "_test_failed_statuses", {})
    _test_failed_statuses[result.when] = result.failed
    item._test_failed_statuses = _test_failed_statuses

    item._terminal_errors[result.when] = _should_hard_fail_on_error(item, result)

AFTER

@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport(item, call):
    result = yield
    if result.when == "setup":
        # clean failed statuses at the beginning of each test/rerun
        setattr(item, "_test_failed_statuses", {})

        # create a dict to store error-check results for each stage
        setattr(item, "_terminal_errors", {})

    _test_failed_statuses = getattr(item, "_test_failed_statuses", {})
    _test_failed_statuses[result.when] = result.failed
    item._test_failed_statuses = _test_failed_statuses

    item._terminal_errors[result.when] = _should_hard_fail_on_error(item, result)
    return result

@hb2638
Copy link

hb2638 commented Jun 7, 2024

I think switching pytest_runtest_makereport to the "new style" hook will make the issue go away

BEFORE

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    result = outcome.get_result()
    if result.when == "setup":
        # clean failed statuses at the beginning of each test/rerun
        setattr(item, "_test_failed_statuses", {})

        # create a dict to store error-check results for each stage
        setattr(item, "_terminal_errors", {})

    _test_failed_statuses = getattr(item, "_test_failed_statuses", {})
    _test_failed_statuses[result.when] = result.failed
    item._test_failed_statuses = _test_failed_statuses

    item._terminal_errors[result.when] = _should_hard_fail_on_error(item, result)

AFTER

@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport(item, call):
    result = yield
    if result.when == "setup":
        # clean failed statuses at the beginning of each test/rerun
        setattr(item, "_test_failed_statuses", {})

        # create a dict to store error-check results for each stage
        setattr(item, "_terminal_errors", {})

    _test_failed_statuses = getattr(item, "_test_failed_statuses", {})
    _test_failed_statuses[result.when] = result.failed
    item._test_failed_statuses = _test_failed_statuses

    item._terminal_errors[result.when] = _should_hard_fail_on_error(item, result)
    return result

disregard, the issue is still there :^(

ramosbugs added a commit to unflakable/unflakable-python that referenced this issue Jun 8, 2024
PyTest 8.2.2 broke support for unittest retries, which should be fixed
in 8.2.3 (see pytest-dev/pytest#12424).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: unittest related to the unittest integration builtin plugin type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants