Remove the requirement for @group annotation in all tests

Created on 7 December 2017, about 7 years ago
Updated 27 August 2024, 4 months ago

The @group annotation is required in test classes, at least because without it drupal.org's test runner fails.

It's not required for PHPUnit -- for example, I can run my tests with the phpunit command and it doesn't care about classes not having a @group.

For most modules, the @group annotation merely repeats the module name. This violates DRY.

D.org documentation suggests running PHPUnit tests locally with the phpunit command, which doesn't care about not having a @group. Therefore, developers can run tests locally, have them pass, and then find their patches fail tests on d.org. This is poor DX.

๐Ÿ“Œ Task
Status

Needs review

Version

11.0 ๐Ÿ”ฅ

Component
PHPUnitย  โ†’

Last updated about 3 hours ago

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น
  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    Example: https://git.drupalcode.org/project/examples/-/jobs/2454428

    Drupal\Core\Test\Exception\MissingGroupException: Missing @group annotation in Drupal\Tests\examples\Unit\TraitsTest in /builds/project/examples/web/core/lib/Drupal/Core/Test/TestDiscovery.php:343
    
    Stack trace:
    #0 /builds/project/examples/web/core/lib/Drupal/Core/Test/TestDiscovery.php(170): Drupal\Core\Test\TestDiscovery::getTestInfo('Drupal\\Tests\\ex...', '/**\n * Tests fo...')
    #1 /builds/project/examples/web/core/scripts/run-tests.sh(912): Drupal\Core\Test\TestDiscovery->getTestClasses(NULL, Array, 'modules/custom/...')
    #2 /builds/project/examples/web/core/scripts/run-tests.sh(168): simpletest_script_get_test_list()
    #3 {main}
    

    The test class was missing @group.

  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    Why not just assigning a default group? I'd be easy to change from this:

        if (empty($annotations['group'])) {
          // Concrete tests must have a group.
          throw new MissingGroupException(sprintf('Missing @group annotation in %s', $classname));
        }
    

    To this:

        if (empty($annotations['group'])) {
          // Concrete tests must have a group. Use "default" if none is set.
          $annotations['group'] = "default";
        }
    

    I'll create an MR in case this wants to be considered.

  • Merge request !9259Default group if none given. โ†’ (Open) created by fjgarlin
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin
  • Pipeline finished with Failed
    4 months ago
    Total: 67s
    #259021
  • Pipeline finished with Canceled
    4 months ago
    Total: 66s
    #259028
  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    The above suggestion is ready for review in the MR.

  • Pipeline finished with Failed
    4 months ago
    Total: 627s
    #259029
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น
  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    The unit test failures seem legitimate as the exception is no longer thrown; there are dailing tests that do not report details and need investigation.

  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    Yeah, I check the errors and there are some tests testing for this missing group, so rather than going after fixing tests, etc. I guess we should ask whether this issue is desired or not.

    Do we want to force all classes to have a @group (whether it's via dockblock or annotation)? It might be a reason for this requirement, so it'd be great to know before continuing. If there is a legit reason, then we can close because it works as designed.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    To me this is a legitimate issue. @group is only needed in TestDiscovery which is only needed by run-tests.sh which is a Drupal-specific tool. The fix IMHO is good because it only affects TestDiscovery, and allows developers to prevent using a Drupal-specific feature if theyโ€™re OK to run tests in the PHPUnit CLI only.

  • Pipeline finished with Failed
    4 months ago
    Total: 67s
    #259105
  • Pipeline finished with Failed
    4 months ago
    Total: 681s
    #259108
  • Pipeline finished with Success
    4 months ago
    Total: 504s
    #259136
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    In that case, I made a few more changes and it is ready for review again.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    > Why not just assigning a default group? I'd be easy to change from this:

    What's the point?

    Can we just fix the thing that complains when there's no group?

  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    The part complaining about it is now fixed with the changes in the MR @joachim.

    There was some logic in case it was a trait, or abstract class... that's why there is the "default" group still added for actual tests, and no "group" at all for other cases.

    With the changes in the MR, you should be able to have an actual test without the "group" annotation and it should still run ok.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    I don't understand the need to add a 'default' group. What happens if we don't add it?

  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    From what I see in the code, the whole list of tests is a multiarray sorted by group, so not adding any group would break the array structure.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom jonathan1055

    The current coding standards state that @group is required โ†’ .

    If this is being changed, then shouldn't that be agreed with the Coding Standards team first? as per the process on https://www.drupal.org/project/coding_standards โ†’

    I found two Coding Standards issue relating to this:
    #2297541: [policy, no patch] Secondary/additional test @group names โ†’
    #2325985: No documentation about @group @coversDefaultClass @covers โ†’

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Sooner or later we'll be removing test annotation on behalf of attributes - see ๐Ÿ“Œ TestDiscovery expects @group annotations, fails with #[Group()] attributes Active which BTW could use some review.

    I would suggest to postpone this on
    a) that issue
    b) conversion of core tests to use of attributes
    c) then we could add a PHPStan rule for core to ensure that core tests have the #[Group] attribute to comply with the coding standards, and avoid the current hard failure in run-tests.sh

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    > From what I see in the code, the whole list of tests is a multiarray sorted by group

    Where is that in the code?

    > The current coding standards state that @group is required.

    I expect they state that because of the requirement in the code which this issue is dealing with. We need to remove the hard requirement before we can remove the standard.

    > then we could add a PHPStan rule for core to ensure that core tests have the #[Group] attribute to comply with the coding standards

    No, because the whole goal of this issue is to AVOID having to have a pointless group for a test!

  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    Re first part of #30

    In core/lib/Drupal/Core/Test/TestDiscovery.php:

        // Sort the groups and tests within the groups by name.
        uksort($list, 'strnatcasecmp');
        foreach ($list as &$tests) {
          uksort($tests, 'strnatcasecmp');
        }
    

    Then in core/scripts/run-tests.sh, all the calls are like $groups = $test_discovery->getTestClasses($args['module']); and the result is accessed like foreach ($groups as $group => $tests) {...

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mile23 Seattle, WA

    If you want to run all the tests for one module, you can say --group that_module when you run PHPUnit. It's quite useful.

    +1 on #29.

    Postponing on what we learn in ๐Ÿ“Œ TestDiscovery expects @group annotations, fails with #[Group()] attributes Active

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    > If you want to run all the tests for one module, you can say --group that_module when you run PHPUnit. It's quite useful.

    I think it's simpler to use the path to the module. You get path autocomplete on the command line that way.

Production build 0.71.5 2024