str0zzapreti / pytest-retry

A simple plugin for retrying flaky tests in CI environments
MIT License
27 stars 6 forks source link

Missing report of the retried test when using `pytest-xdist` #26

Closed francis-clairicia closed 6 months ago

francis-clairicia commented 6 months ago

Hi, I was testing the compatibility of the plugin with pytest-xdist to use it in my project and saw that the feature is missing, so I am reporting it here.

Minimal reproducible example

In test_flaky.py

import pytest

a = 0

@pytest.mark.flaky(retries=3)
def test_flaky() -> None:
    global a

    a += 1
    assert a == 3

Without pytest-xdist:

$> pytest -k "test_flaky"
============================================================================= test session starts ==============================================================================
platform linux -- Python 3.11.6, pytest-7.4.4, pluggy-1.3.0
rootdir: /home/XXXX/Test/flaky-tests
plugins: xdist-3.5.0, retry-1.5.0
collected 1 item                                                                                                                                                               

test_flaky.py R.                                                                                                                                                         [100%]

======================================================================= the following tests were retried =======================================================================
        test_flaky failed on attempt 1! Retrying!
        Traceback (most recent call last):
          File "/home/XXXX/Test/flaky-tests/test_flaky.py", line 11, in test_flaky
            assert a == 3
        AssertionError: assert 1 == 3

        test_flaky failed on attempt 2! Retrying!
        Traceback (most recent call last):
          File "/home/XXXX/Test/flaky-tests/test_flaky.py", line 11, in test_flaky
            assert a == 3
        AssertionError: assert 2 == 3

=========================================================================== end of test retry report ===========================================================================

========================================================================= 1 passed, 1 retried in 0.02s =========================================================================

With pytest-xdist:

$> pytest -n 2 -k "test_flaky"
============================================================================= test session starts ==============================================================================
platform linux -- Python 3.11.6, pytest-7.4.4, pluggy-1.3.0
rootdir: /home/XXXX/Test/flaky-tests
plugins: xdist-3.5.0, retry-1.5.0
2 workers [1 item]      
R.                                                                                                                                                                       [100%]
========================================================================= 1 passed, 1 retried in 0.28s =========================================================================
str0zzapreti commented 6 months ago

@francis-clairicia Thanks for reporting this. Compatibility with pytest-xdist hasn't been a priority of this plugin until now, but it wasn't too difficult to add a server for the main xdist process to collect and build a complete retry report. See https://github.com/str0zzapreti/pytest-retry/pull/29 .

Based on my own experience, I would still recommend parallelizing tests using your CI tool of choice rather than pytest-xdist as it often scales better and usually avoids these sorts of incompatibilities, but I understand that's not always possible! Anyway, once I finish testing this new feature it'll go out with version 1.6.0.

francis-clairicia commented 6 months ago

In my case, I can't override xdist, but I agree that it's best to avoid using it if you don't have to.

str0zzapreti commented 6 months ago

Yup, been there. Let me know if you notice any issues with the new version!