Problem/Motivation

PHPUnit 13 introduces two useful concepts for re-testing the same test method, that would be particularly useful for re-running tests that are failing randomly:

Besides the differences indicated by the documentation, there are implications on using one vs the other in core test CI runs with run-tests.sh:

  • A test marked #[Repeat] is counted, during test discovery, for the number of repeats, so it impacts the test weight for work allocation purposes in the CI matrix. Also, each repetition is reported individually in the detailed test report. So, it's a bit more 'deterministic' in terms of what resources will be expected to be execute it. The number of repeats threshold can reduce the overall resources as in case of consistent failure, the remaining repeats are not executed once the threshold is reached.
  • A test marked #[Retry] is counted, conversely, always as '1' during test discovery. Only the first successful attempt is reported in the detailed test report, but that includes from a execution time perspective also the sum of the failed attempts. So, it's less predictable in terms of what resources will be expected to be execute it.

This issue is to decide if we want (or not) to recommend one or the other approach for core tests random failures.

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Issue summary: View changes
longwave’s picture

To me #[Retry] is the one that makes most sense given we are trying to defend against flaky tests instead of just find them; I can't think of a case where we explicitly want a test to be repeated, we just want it to pass once and as quickly as possible.

Does the report include the number of retries somewhere? I was thinking whether we could use it in future to detect which tests to fix, but also I guess we just want to eliminate #[Retry] entirely where we can.

edit: oh yes I see from the docs:

There was 1 retried test:

1) ExampleTest::testSomething
1 failed attempt

If we could then aggregate these results somehow then perhaps we could identify the most flaky tests that we want to fix first.

mondrake’s picture

#3 probably makes sense, but

a) the detail of the retried tests is not available on JUnit, only in the text output (maybe in the new OTR output format? to be checked), so it gets swallowed in run-tests.sh detail report
b) if the test eventually succeeds after some failures, we do not get any information on the reason of the failure

OTH, playing around in #3574681: [Nov 2026] Introduce support for PHPUnit 13.5 I found that for not particularly expensive tests, #[Repeat(3, 2)] could be an interesting compromise https://git.drupalcode.org/project/drupal/-/merge_requests/14840/diffs?f...

  • it leads to test failure only if two attempts fail out out of three, which is kind of less likely random
  • all three attempts are accounted for in work allocations to sherds
  • runt-tests.sh detailed result report every attempt
    ---- Drupal\Tests\locale\Functional\LocaleUpdateTest ----
    Status      Duration Info                                                                               
    --------------------------------------------------------------------------------------------------------
    Pass          4.773s testUpdateCheckStatus                                                           
    Pass          4.291s testUpdateImportSourceRemote (repetition 1 of 3)                                
    Pass          3.915s testUpdateImportSourceRemote (repetition 2 of 3)                                
    Pass          3.714s testUpdateImportSourceRemote (repetition 3 of 3)                                
    Pass          3.555s testUpdateImportSourceLocal                                                     
    Pass          3.956s testUpdateImportModeNonCustomized                                               
    Pass          3.744s testUpdateImportModeNone                                                        
    Pass          7.880s testEnableUninstallModule                                                       
    Pass          7.595s testEnableLanguage                                                              
    Pass          7.718s testEnableCustomLanguage     
    
longwave’s picture

We can still parse the full logs if we need to, I assume the junit output just doesn't have any fields for anything like this.

I believe most of the flaky tests are JavaScript and none of them are particularly quick so adding repeat feels like it will slow us down there, but I might be wrong.

pancho’s picture

I would personaly prefer a policy to use #[Repeat] as I don't think we should primarily defend against flaky tests. Over time, we should fix them, one by one, and my experience with #3631339: [random test failure] FieldTest::testModalDialogTitle fails intermittently tells me that it's possible.
Just my two cents, though...

godotislate’s picture

Agreed with #3: we should use #[Retry], because our goal for now is defend against flaky tests. Setting tests to repeat regardless of whether they pass uses CI resources we are trying to reduce the bill on.

We also already have the repeat test job that we can run manually to help identify flaky tests, or check that they've been addressed.

longwave’s picture

Status: Active » Needs review
smustgrave’s picture

+1 to the Retry approach. Can we extend to nightwatch as that's being the PITA right now.

longwave’s picture

mondrake’s picture

#9 no Nightwatch is not PHPUnit based, this is a PHPUnit thing.

mondrake’s picture

Title: [policy, no patch] Usage of #[Retry] and/or #[Repeat] attributes on flaky tests » [policy, no patch] Usage of #[Retry] and/or #[Repeat] attributes on flaky PHPUnit tests

#9 #11 let's make it clear in the issue title.