Problem/Motivation

Mentioned by @mstrelan in #3516771: Prevent setup tasks running for tests that are always skipped

A better approach might be to introduce an attribute that can be put on the test method. That way it would be reusable, and it would still be obvious when looking at only the method that it should be skipped. Note that phpunit already has some attributes for this, but they are not quite suitable for these cases

Steps to reproduce

NA

Proposed resolution

Add a new attribute, Skip. Check for the attribute in a new #[Before(...)] function in DrupalTestCase.

Remaining tasks

Review

User interface changes

NA

Introduced terminology

NA

API changes

New attribute

Data model changes

NA

Release notes snippet

NA

Issue fork drupal-3517430

Command icon 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:

Comments

smustgrave created an issue. See original summary.

smustgrave’s picture

mondrake’s picture

Maybe first step would be to file an issue upstream @ https://github.com/sebastianbergmann/phpunit/issues just to see if there's any interest to have such an attribute directly in PHPUnit?

mondrake’s picture

Issue tags: +run-tests.sh

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

mondrake’s picture

dcam made their first commit to this issue’s fork.

dcam’s picture

Title: Investigate adding phpunit attribute for skipping test » Add an attribute for skipping PHPUnit tests
Issue summary: View changes
Status: Active » Needs review

#3615036: Replace markTestSkipped() with the Skip attribute inspired me to try doing this because I thought "This can't be difficult to do. If it isn't, then we shouldn't spend time moving all of the markTestSkipped() calls." It turned out to be fast to implement. I converted two skips, one with a message and one without.

The priority of 200 in the new "Before" function was chosen somewhat arbitrarily to run before anything else.

catch’s picture

This looks great to me. Since we should never have dozens and dozens of skipped methods in core I don't think we need to be too concerned about trying to get support for this upstream. Would be easy enough to convert later if it happens.

dcam’s picture

Would be easy enough to convert later if it happens.

That was my thought too. Also, we can use a solution like this for early skips, the sooner the better. Any solution introduced in PHPUnit would be for a version that we may not be able to add as a dependency for months or years. I think it's better to accept that we might have to deprecate our home grown solution later on. In the meantime we can point Sebastian at this and say "Are you interested in implementing this?"

dcam’s picture

I wrote a change record.

mondrake’s picture

How about just #[Skip]? It’s obvious we are skipping a test here. Would also match with the #[Retry] and #[Repeat] methods coming up with PhpUnit 13.3

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

This looks great! Looks like it already got a bunch of feedback and resolved. Tweaked the CR to Skip but lets do it!

dcam’s picture

Tweaked the CR to Skip but lets do it!

Thank you for doing that! I got pulled into a meeting right as I was wrapping up the changes.

dcam’s picture

How about just #[Skip]?

This sounded like a good idea to me, so I did it. Thank you.

godotislate made their first commit to this issue’s fork.

  • godotislate committed f4a46dd0 on main
    task: #3517430 Add an attribute for skipping PHPUnit tests
    
    By:...
godotislate’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Confirmed Drupal\Tests\Core\Site\SettingsTest::testRealDeprecatedSettings is skipped as expected: https://git.drupalcode.org/project/drupal/-/jobs/11422791 (need to click to view the raw log)

And Drupal\Tests\editor\Kernel\EditorValidationTest::testLabelValidation skipped: https://git.drupalcode.org/project/drupal/-/jobs/11422789

Had a couple minor suggestions and went ahead and applied them:

  • (Optional) -> (optional), lowercase is used consistently through core
  • $this->markTestSkipped -> static::markTestSkipped, because it's a static method
  • $attribute->getArguments()[0] ?? '' -> $attribute->newInstance()->message), easier to read

Committed f4a46dd and pushed to main. Thanks!

Does not apply to 11.x, so will need a separate MR for that. For now, I made a couple small edits to the CR and set the version to 12.0.0, which will need to be updated after the backport.

One suggestion I have for a future enhancement is to add a callable as a second optional parameter to Skip, and that callable could be invoked (in a static context), and the test would be skipped on whether it returns TRUE.

dcam’s picture

...because it's a static method

Probably no one ever noticed because the PHPUnit docs use $this->markTestSkipped(). I think this same PHPUnit coding standard was discovered for some other static function during the mock->stub conversion project.

dcam’s picture

Version: main » 11.x-dev

dcam’s picture

Status: Patch (to be ported) » Needs review

Backported

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Awesome to see this land so quickly. Looks like a good backport.

godotislate’s picture

Status: Reviewed & tested by the community » Needs review

Judging from #3613344: Remove update_fetch_with_http_fallback and #3591513: Deprecate the update_fetch_with_http_fallback setting, I think SettingsTest::testRealDeprecatedSettings isn't supposed to be skipped in 11.x. I have suggestions on the MR to remove. Could someone confirm that and apply the suggestions if correct? Can go back to RTBC after.

mstrelan’s picture

I'm not at my desk but I was involved in that issue and that sounds right, it shouldn't be skipped in 11.x. I guess if it passes when it is unskipped then there is the proof.

godotislate’s picture

Status: Needs review » Reviewed & tested by the community

Removed the skip per #27 and tests pass, so back to RTBC. Will commit once I back at the computer.

  • godotislate committed f7eadfbe on 11.x
    task: #3517430 Add an attribute for skipping PHPUnit tests
    
    By:...
godotislate’s picture

Status: Reviewed & tested by the community » Fixed

Committed f7eadfb and pushed to 11.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • godotislate committed 2d368c39 on 11.x
    task: #3517430 Add missing Skip attribute.
    
    By: smustgrave
    By: mstrelan...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.