Problem/Motivation
Drupal is using its own code to discover tests to be executed, which along time lead to discrepancies in the tests executed betwen PHPUnit CLI and run-tests.sh.
PHPUnit 10 (Feb 2023) introduced an API to discover the test suites, that can be used instead. Also, PHPUnit 10 introduced attributes to replace the annotations that were previously used to indicate test configuration to PHPUnit. PHPUnit 11 (Feb 2024) is triggering deprecations if it detects tests that use annotations. PHPUnit 12 (Feb 2025) no longer supports annotations.
Drupal's TestDiscovery class, that builds the tests to be executed by run-tests.sh, mandates tests to use @group annotations. It fails if only #[Group(...)] attributes are used.
Proposed resolution
- Deprecate
TestDiscovery::getTestClasses()and related methods. - Introduce a like-for-like replacement that uses PHPUnit's API for the discovery, and enriches the information returned by the API to provide same info as
TestDiscovery::getTestInfo(). - Make the new code ready for PHpUnit's 10+ attributes instead of annotations.
- In the process, fix some bugs of
TestDiscovery::getTestClasses()that were identified:- for each test class, it only reports groups present in the class-level annotations: this means that method level annotations do not qualify test classes for a group, potentially missing the execution of some tests. PHPUnit is more accurate in bubbling up method-level annotations/attributes to the class
- it does not check duplication of @group annotations that specify the same group. PHPUnit make them unique.
- when filtering by test suite, it reports some test groups with an empty array, since in any case it iterates all test directories and files. PHPUnit only scans the directories indicated in phpunit.xml for each test suite, providing more accurate results.
- Very interesting side effect of this - since PHPUnit discovery actually executes the dataproviders, and includes in the discovery list as many test cases as the number of datasets provided for a test method, we can now be precise in the number of test cases run for each test class, and use that information in run-tests.sh instead of the current approximate count.
Remaining tasks
User interface changes
nope
API changes
nope
Data model changes
nope
Release notes snippet
PHPUnit 11 is now the default version. It's still possible to downgrade to PHPUnit 10 in case of need, but in that case it's recommended to use PHPUnit's CLI for running tests, and avoid using Drupal's run-tests.sh script that may have gaps in discovering tests. Drupal tests now can use PHPUnit's 10+ attributes instead of the legacy annotations. New tests MUST use attributes.
Issue fork drupal-3497431
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3497431-followup
changes, plain diff MR !12382
- 3497431-deprecate-testdiscovery-test
changes, plain diff MR !10809
Comments
Comment #3
mondrakeComment #4
mondrakeComment #5
mondrakeWe are hitting a PHPUnit bug here, The file attribute of node of XML test list can be wrong. So we still need to read the file of a test class from the classloader.
Comment #6
mondrakeComment #7
mondrakeI think we can still avoid the classloader. On it.
Comment #8
mondrakeVery interesting side effect of this - since PHPUnit discovery actually executes the dataproviders, and includes in the discovery list as many test cases as the number of datasets provided for a test method, we can now be precise in the number of test cases run for each test class, and use that information in run-tests.sh instead of the current approximate count.
Comment #9
mondrakeComment #11
catchThis looks really good and cleans up quite a lot of mess.
I went ahead and added @group #slow to mainly kernel tests that are now ending up towards the end of the run. This is not a fault in the ordering logic at all, it's just a side effect that some tests that happened to run earlier in a test run now could run later either purely because other tests moved around, or subtle changes in the method counting logic.
The debugging logic at the top of the test runs is great and I wonder if we should just keep that all the time.
Comment #12
mondrakeI was thinking in a follow up we could introduce a “weight“ to #slow (i.e. #slow300, #slow200, etc) and further tune the start sequence.
Comment #13
catch@mondrake I think at the moment this is OK because we have less #slow tests than we run tests concurrently per-job (e.g. we might have 5 slow tests but start 8 at the same time), however it took a while for that to be the case and it might not be the case again, so definitely worth an issue. Even now it might save a few seconds which is worth having.
Comment #14
mondrakeWe can leverage on PHPUnit API also to determine the test description, instead of doing separate parsing. On it.
Comment #15
mondrakeDone #14.
Comment #16
mondrakeFiled #3504223: Introduce a “weight“ to #slow tests re #12 and #13.
Comment #17
mondrakemerged with HEAD after commit of #3497327: Extend PhpUnitTestDiscoveryTest to test also PHPUnit API
Comment #18
mondrakeComment #19
mondrakeThanks for review @mstrelan, added some of your suggestions and replied inline to some other ones; NW to address the remaining comments.
Comment #20
mondrakeComment #21
mondrakePostponed on #3521815: Fix test annotations for tests introduced by #3485896
Comment #22
mondrakeComment #23
mondrakeFound some issues while testing this in combination with #3418267: Support PHPUnit 11 in Drupal 11. Given that one is RTBC, let's postpone on that.
Comment #24
mondrakeComment #25
larowlanBlocker is in
Comment #26
mondrakeRebased. Since when this was
RTBClast reviewed, added logic for determining group metadata information under PHPUnit 11.edit - sorry, this was not RTBCed yet
Comment #27
catchAdding this as a release priority because it will allow #[Group] to be used in contrib in combination with run-tests.sh (at least when 11.1 support is dropped).
Comment #28
mondrakeThanks for reviews!
I'd love to add
@phpstan-type, but I am reluctant because of #3082239: Forbid limited length primary and unique keys, allow only in indexes and #3376270: Support PHPStan's annotations for local type aliases and array shapes.Comment #29
larowlanNo worries, it's a nice to have and shouldn't hold this up
Comment #30
mondrakeremoved duplicate
@group #slowannotationsComment #31
mondrakeMade some updates to the CR to provide some input how to convert tests from annotation to attributes. Would be lovely to get a review there too.
Comment #32
larowlanCR looks great, nice one 🙌
Comment #33
godotislateThere are failing tests in the latest build that are not familiar to me as intermittent failures, but they probably need a re-run attempt at least.
Comment #34
mondrakeOnly merged with head, now tests are green. There's fortuity in randomness of test failures.
Comment #35
godotislateI see only one unresolved MR comment from @larowlan about the file naming, but that comment was answered and seems to make sense to me. RTBC.
Comment #38
catchOK I read through this again. It's a really nice clean-up of test discovery.
Personally I am excited about the improvements to test ordering, both for core test suite performance and also because it unblocks potential improvements in contrib as described in #3469828: Lower default concurrency to 8.
Committed/pushed to 11.x and cherry-picked to 11.2.x, thanks!
Comment #40
catchI think this may have broken pipelines, e.g. https://git.drupalcode.org/issue/drupal-3046670/-/pipelines/505134 it's not possible to see the default test job. Reverting for now.
Comment #44
mondrakeCan't figure out what would be impacting pipelines here - the MR one after revert and rebase seems to work fine. Pipeline config is not touched apart from the addition of a parameter when calling
run-tests.sh.Comment #47
catchYeah it doesn't make sense to me either - going to try again and trigger a couple of pipelines on different MRs in case it was a one-off.
Comment #48
catchSeems OK now, I think it was a false alarm/ gitlab itself having a problem.
Comment #49
mondrakeDo we need publishing the CR?
Comment #50
larowlanChange record appears to already be published 👍
Comment #51
mondrakeFiled #3526499: PhpUnitApiGetTestClassesTest and PhpUnitApiFindAllClassFilesTest need to execute PHPUnit discovery before TestDiscovery after finding an issue while working on the followup conversions from annotations to attributes.
Comment #52
mondrakeComment #53
mondrakeComment #54
fjgarlin commentedNote that this has broken testing for 11.2.x+ pipelines for all contrib.
Related issue in "gitlab_templates": #3526778: Tests not running or failing when using run-tests.sh in 11.2.0-dev
Modules are either getting
MissingGroupExceptionorERROR: No valid tests were specified.Key projects like Project Browser are affected #3527063: Update for changes required by D11.2, but again, this is all contrib testing NEXT_MINOR via
run-tests.shComment #57
catchYes let's revert for now - hopefully we have enough to go on from the failed runs to figure out what's missing here.
Comment #58
mondrakeIn next iteration, I would consider doing:
1) Bump PHPUnit to 11 in the lock file, instead of keeping it locked to 10. #3418267: Support PHPUnit 11 in Drupal 11 has been in for 3 weeks now and AFAICS no issue have been reported about that. PHPUnit 11 also support PHP 8.3, which is minimum for D11. We can still keep 10 in the composer.json in case anyone has the need to downgrade for incompatibility (which, again, was not found yet).
2) Include #3526499: PhpUnitApiGetTestClassesTest and PhpUnitApiFindAllClassFilesTest need to execute PHPUnit discovery before TestDiscovery which was meant to be a followup of this, but since now it’s open again, better do straight ahead.
Then, if #3526778: Tests not running or failing when using run-tests.sh in 11.2.0-dev willl still not be resolved, it should be (theoretically) only a matter of listing the relevant directories in phpunit.xml.dist - and that would require another core fix if contrib is meant to run with core configuration.
I’d also suggest to commit initially only to 11.x to allow proper checking in gitlab templates, and only eventually backport to 11.2.x upon complete validation.
Comment #59
catch@mondrake that all sounds good. I think defaulting to phpunit 11 is fine
Comment #60
andypostOne more strong reason is that PHPUnit 10 no longer has bugfix support (since Feb'25) https://phpunit.de/supported-versions.html
So moving to 11 is required step to start transition to 12
Comment #61
mondrakeI do not understand why the CSpell job fail given that the file reported is included in the ignored paths in .cspell.json.
Then waiting for feedback to question in #3527063-8: Update for changes required by D11.2 to see if we do need to edit the
<directory>sections ofphpunit.xml.distto accomodate contrib testing.Comment #62
mondrakeSolved the CSpell problem.
Comment #63
mondrakeDone #58.
Comment #64
mondrakeApparently the syntax to pick the test files in the /module subdirectory was failing somehow, changed after some thorough manual testing documented in https://drupal.slack.com/archives/CGKLP028K/p1748635336355999
This should hopefully fix the issue in #3527063: Update for changes required by D11.2 with no changes to need happen there (once this is back in).
Comment #65
ghost of drupal pastCurrently you can enable test modules with a simple setting , it's extremely useful for debugging is that kept?
Comment #66
mondrake@ghost of drupal past sorry I do not understand your question. Can you please elaborate?
Comment #67
catchThere's no change to module discovery here, only the test classes themselves.
Comment #68
godotislateOK, confirmed that the changes for the build in #3527555: test issue, ignore for Project browser are passing (phpstan issues there are unrelated), and the changes there are the same as the changes in the MR 10809 here, other than the use of globstar patterns core/.phpunit-next.xml here. But that file isn't used in the PB build per https://git.drupalcode.org/project/drupal/-/merge_requests/10809#note_51..., so everything LGTM.
Comment #71
catchDiscussed this with @xjm - since this was committed for the beta, then reverted during the beta, and the actual change since is tiny, and it unblocks several other things, going ahead and committing it again relatively last minute during rc. Would be good peace of mind to see a successful contrib test run before we get anywhere near tagging 11.2.0 next week, but generally very glad to see this in!
Committed/pushed to 11.x and cherry-picked to 11.2.x, thanks!
Comment #72
mondrakeImage Effects seems to work fine Test with 11.2.0-rc2
Comment #73
mondrakeProject Browser seems to work too Test with 11.2.0-rc2
Comment #74
mondrakeUpdated release note snippet.
Comment #76
jonathan1055 commentedI know this was going to be committed to 11.x but was surprised to see it also committed to 11.2.x
As soon as Gitlab Templates makes the shift from 11.1 to 11.2 as 'current core version' this will break some existing contrib pipelines, see https://git.drupalcode.org/project/scheduler/-/jobs/5557961 This phpunit job uses the documented
@groupto divide the tests into parallel streams. I know we can also add the#[group ]metadata, but until contrib has a chance to do that then some pipelines will break. Maybe that's acceptable, but I just wanted to let you know, and to understand the process for Contrib changes.Comment #77
mondrakeThe new code can manage both @group annotations and #[Group] attributes, otherwise 99% of core would be failing. So contrib is not requested to add the attribute. The linked tests in #72 and #73 prove it anyway.
So it must be something else, looking.
Comment #78
mondrakeThe error is
Drupal\Core\Test\Exception\MissingGroupException: Missing group metadata in test class Drupal\Tests\Composer\Generator\BuilderTest in /builds/project/scheduler/web/core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php:304which is very very strange as that test (which is a core test btw) clearly DOES have a
@group Metapackageannotation and it runs no problem in core.Gut feeling is that the contrib is playing with the pwd of the job and PHPUnit/autoloading cannot find the class.
Comment #79
jonathan1055 commentedThe actual command that runs the test in these jobs is
sudo -u www-data -H -E php web/core/scripts/run-tests.sh --php '/usr/local/bin/php' --color --keep-results --concurrency 32 --repeat '1' --sqlite 'sites/default/files/.sqlite' --dburl mysql://drupaltestbot:drupaltestbotpw@database/mysql --url http://localhost/web --xml /builds/project/scheduler/web/sites/default/files/simpletest --verbose --non-html scheduler_jsIt is the final argument, 'scheduler_js' in the above example, which in 11.1 runs the tests annotated with
@group scheduler_jsbut which fails at 11.2. This method of selectively running tests is not covered in the tests in #72 and #73Image Effects in #72 tested
--directory modules/custom/image_effectsand Project Browser in #73 tests--types PHPUnit-FunctionalJavascript --module project_browser.Comment #80
catchThe main reason to commit this to 11.2 is to allow contrib to start using attributes to support future updates to phpunit 11 and higher.
If we can make #79 work with a quick follow-up that would be great, but otherwise I think we'd need to revert this from 11.2 before we tag 11.2.0 (but probably leave it in 11.x) this time.
Comment #81
mondrakeI am looking into this. Knowing the inners, it's very strange as that exception is thrown after the test list has been already built by PHPUnit, which is common with the other two tests.
Comment #82
mondrakeImage Effects runs tests with pwd =
/builds/project/image_effectsProject Browser runs tests with pwd =
/builds/issue/project_browser-3530110Comment #83
mondrakeOpened #3530183: Test Scheduler at 11.2.x-dev with patched PhpUnitTestDiscovery.php in Scheduler's module issue queue to try and understand what's going on.
Comment #84
xjmBut with test changes, isn't the breakage broken regardless of whether it's in the release branch or not? Like 11.x will break things just as hard if it's broken, no? If what it's breaking are contrib pipelines?
Edit: If it's breaking things, reverting before it's in a tagged minor is important, I agree. Just I think that (sadly) it would also need to be reverted from 11.x again as well, unless we just wanted to leave it around a little bit for aggressively enforced debugging or something. :)
Comment #85
mondrake#3530183: Test Scheduler at 11.2.x-dev with patched PhpUnitTestDiscovery.php has a debug patch for core that now makes tests pass. Most important would now be to understand why PHPUnit sometimes does not pass a filled in Test object; then we can make a core patch.
Do I need to open a separate issue or an additional MR in this issue, eventually?
Comment #86
mondrakeFiled #3530202: Introduce PHPUNIT_FAIL_ON_PHPUNIT_DEPRECATION in GitLab Templates issue queue.
Comment #88
mondrakeWe might have surfaced a bug upstream (PHPUnit's test suites builder seems to be adding empty TestSuite sub-objects to the main TestSuite object under some unclear circumstances), but this minimal MR!12382 fix seems to let the overall testing discovery complete in that case. #3530183: Test Scheduler at 11.2.x-dev with patched PhpUnitTestDiscovery.php is now passing with the equivalent of the MR as a patch.
Will try to see if I can report upstream, but that will require quite some investigation before being able to - building a self-contained minimal patch reproducing the issue is not going to be a simple one.
Comment #89
mondrakeCopy/pasting here more details I added in #3530183-8: Test Scheduler at 11.2.x-dev with patched PhpUnitTestDiscovery.php.
Comment #90
mondrakeSo as detailed in #3530183-13: Test Scheduler at 11.2.x-dev with patched PhpUnitTestDiscovery.php, the issue reported in #76 is due to gaps present elsewhere, that the PHPUnit discovery process legitimately cannot deal with.
In the MR, now, an implementation of PHPUnit's Tracer is collecting warnings thrown by PHPUnit during discovery, and reporting them in the run-tests.sh output so they can be actioned. The discovery process is now completed instead of failing as it was identified in #76, but the actual list of tests to be executed will be missing those tests that errored during discovery.
Comment #91
catchThe MR looks tidy to me, and the new error output is good. I think we should go ahead and commit this as a follow-up to both 11.x and 11.2.x.
Comment #92
mondrakeI was on the verge of self-RTBC already, #91 confirms, so let's get going.
Comment #95
catchYes this is more or less the reason. It turned out the last round of failures was surfacing specific issues discovering tests in specific modules due to things like non-static data providers and missing directories etc.
I think we probably need a new CR here that modules that previously had silent test discovery errors will now get a loud error with useful information, however I don't feel like I understand the intricacies of the specific errors found to write a useful summary.
Committed/pushed to 11.x and cherry-picked to 11.2.x, thanks!
Moving to needs work for the new/updated change record.
Comment #97
jonathan1055 commentedI had just written the below, simultaneously, but copy/pasting here anyway.
It seems that PHPUnit 11 is obviously a lot more picky about test discovery than PHPUnit 10, which easily found all the
@group mygroupand did not complain at all.But yes I agree, get this in 11.2.x then those tests at Next Minor will start running again. If the output clearly lists the skipped tests and the reasons then at least we have something to work on. But if they are skipped due to 3rd-party module problems then that will mean we lose test coverage until they can be fixed. In the earlier 11.2.x work, those were merely reported as deprecations in 3rd-party modules wich could be ignored and the tests ran. Anyway, we have to move forward, so yes RTBC+1. Good work mondrake, thank you for taking this on.
Comment #98
mondrakeI'll work on a CR. Re #97:
to me this is not going to mean lost test coverage. We need to distinguish test discovery from test execution. Test discovery looks for ALL the test classes available (based on some pre-constraints sometimes, e.g. if you specifically indicate a directory to search in, it will be limited to that), then filters only those that need to be executed.
In the case of the wrong dataprovider in address and token modules of the related issue, these are reported as failing when building the test list during discovery, but are not executed because the filter is
@group scheduler_xxx. So, the scheduler tests are not really affected. They would fail loud on tests executed on those modules with PHPUnit 11 (time to apply the static modifier...), or if, from the scheduler module, you'd run tests for@group tokenor@group address. But that's because the dataprovider must be static, not for the discovery.Comment #99
jonathan1055 commentedAh thank you for explaining that. I see now that your comment in #90 "but the actual list of tests to be executed will be missing those tests that errored during discovery." is actually the "list of tests discovered will be missing ..." but the full list for the intended module should still be run (providing the errors are only in 3rd-party modules). That sounds perfect.
Comment #100
mondrakeAdded CR https://www.drupal.org/node/3530388, appreciate review and publishing.
Comment #101
mstrelan commentedThis issue added some new deprecations to the baseline. I've opened #3530453: Remove deprecated use of Assert::isType() to fix them.
Comment #102
mondrake@jonathan1055 kindly reviewed the new CR, I published it. Tentatively marking this fixed as it looks to me there's nothing else to do.
Comment #103
xjmThanks all for the hotfix and related followup work!
Retroactively bumping priority.
Comment #104
jonathan1055 commentedNow that this has been re-committed to 11.2.x I presume the idea is to fix all the Core tests so that they do not produce PHPUnit test runner deprecations? Contrib tests get deprecation warnings for Core classes and methods that don't have the new attributes yet. For example a javascript test that extends
use Drupal\FunctionalJavascriptTests\WebDriverTestBasegets warings that thefailOnJavaScriptErrors()method contains doc-comment metadata.See https://git.drupalcode.org/project/scheduler/-/jobs/5596390#L670
Here is the failOnJavaScriptErrors() method in webDriverTestBase
I've not found any open core issues to start takling this task yet, or maybe I missed it. I can open an issue if this is the plan.
Comment #105
catch#3527934: Optimize phpunit.xml.dist for PHPUnit 11 while retaining support for PHPUnit 10 or #3530113: Allow indicating alternative phpunit.xml than core's when testing via run-tests.sh are probably the first steps towards that - e.g. we would stop running core tests against phpunit 10 at all. Until we do that I don't think we can remove deprecated usages in the test themselves because they'd be needed for phpunit 10. I don't know if there's a specific issue for that bit though or not.
Comment #106
jonathan1055 commentedThanks for the info. But you don't have to remove the deprecated
@metadata just yet, you only need to add the equivalent#[ ]version to prevent the deprecation messages. Both can exist, and the tests run at both PHPUnit11 and PHPUnit10 without a problem. At least, they do with Contrib@groupand@dataprovider, because I've done that here.Comment #107
mondrakeThere are a few issues already towards converting metadata from annotations to attributes, see related #3446380: [no-commit] Define a Rector rule to convert test annotations to attributes, #3446693: Convert test annotations to attributes in Drupal/Test/Component, #3526490: Convert test annotations to attributes in Drupal/BuildTests. But they are currently children of the original #3418267: Support PHPUnit 11 in Drupal 11. It would be probably be the case now to open a new meta for the conversions (there are 000s of files to convert), and reparent those to it.
When it comes to the deprecations, with PHPUnit 11 we need to distinguish between SUT deprecations and PHPUnit runner deprecations - the former are the 'traditional' ones, the latter are internal to PHPUnit. Core, when running tests through run-tests.sh, currently DISABLES PHPUnit runner deprecations by setting the env variable
PHPUNIT_FAIL_ON_PHPUNIT_DEPRECATIONto FALSE. This leads to SUT deprecations being reported but no PHPUnit ones. #3530202: Introduce PHPUNIT_FAIL_ON_PHPUNIT_DEPRECATION aims at introducing the same capability in GitLab Templates; contrib using run-tests.sh can anyway already use it if needed.Comment #108
mondrakeRe #105
per se PHPUnit 10 can perfectly process attributes only, so if tests are run via PHPUnit CLI, no problem to have attributes only in contrib. See https://www.drupal.org/project/sophron for example. The problem is if tests are run via the run-tests.sh script BEFORE Drupal 11.2: in that case the 'old' TestDiscovery process is in place, and that one is not capable of dealing with test classes that lack the
@groupannotation.Comment #109
catchAh OK that makes things a lot easier then, thanks for the clarification.