make it possible to pass a file parameter to phpunit

Created on 12 August 2025, about 2 months ago

Problem/Motivation

The CI is set up to run all of a module's tests.

But when testing locally, you want to run a single test that's failing to debug it or try a fix.

It's not possible to pass the file/folder parameter to phpunit, because the script already has it, it's $DRUPAL_PROJECT_FOLDER in this line:

        sudo -u www-data -H -E vendor/bin/phpunit $PHPUNIT_OPTIONS --bootstrap $PWD/$_WEB_ROOT/core/tests/bootstrap.php $DRUPAL_PROJECT_FOLDER --log-junit $CI_PROJECT_DIR/junit.xml $WHAT_TO_RUN $_PHPUNIT_EXTRA || EXIT_CODE=$?

I've managed to pass a --filter param by adding this to .gitlab-ci.yml:

  _PHPUNIT_EXTRA: "--filter=testMethod"

but that's not ideal, as phpunit filters every class it can find in the given space, so if multiple test classes have that method name, it'll run all of them.

It would be useful to be able to pass a filter/folder param into the gitlab-ci-local command, to override the $DRUPAL_PROJECT_FOLDER parameter.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Component

gitlab-ci

Created by

🇬🇧United Kingdom joachim

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

Comments & Activities

  • Issue created by @joachim
  • 🇪🇸Spain fjgarlin

    Will that override just be the name of the file to run?

  • 🇬🇧United Kingdom joachim

    What I'd ideally like is for

    > gitlab-ci-local phpunit path/to/folder-or-file

    to behave the same way as on an OS:

    > vendor/bin/phpunit path/to/folder-or-file

    For that to work, the path/to/folder-or-file has to replace $DRUPAL_PROJECT_FOLDER, because that's the file parameter that phpunit gets.

  • 🇪🇸Spain fjgarlin

    What we could do is define a _PHPUNIT_TEST_SINGLE_FILE variable, which defaults to empty, then the call could be:
    sudo -u www-data -H -E vendor/bin/phpunit $PHPUNIT_OPTIONS --bootstrap $PWD/$_WEB_ROOT/core/tests/bootstrap.php $DRUPAL_PROJECT_FOLDER$_PHPUNIT_TEST_SINGLE_FILE --log-junit $CI_PROJECT_DIR/junit.xml $WHAT_TO_RUN $_PHPUNIT_EXTRA || EXIT_CODE=$?

    If somebody fills _PHPUNIT_TEST_SINGLE_FILE, it'll be something like /path/to/file.php.

    Can the same be done for run-tests.sh?

  • 🇬🇧United Kingdom joachim

    > Can the same be done for run-tests.sh?

    I think if someone's running a single test locally, they won't be using the concurrency feature, so run-tests.sh wouldn't matter.

  • 🇬🇧United Kingdom jonathan1055

    We do have $_PHPUNIT_TESTGROUPS which filters on the @group defined in the test class.
    See https://git.drupalcode.org/project/gitlab_templates/-/blob/main/includes...

    I know that many test classes could have the same group, but if you are debugging one file locally then just temporarily edit that test file to have a distinct value for the @group. This also has the benefit that is works for both $_PHPUNIT_CONCURRENT:1 and $_PHPUNIT_CONCURRENT:0

  • 🇪🇸Spain fjgarlin

    I think it'd be a great idea to add the above information to https://project.pages.drupalcode.org/gitlab_templates/jobs/phpunit/

    I think if someone's running a single test locally, they won't be using the concurrency feature, so run-tests.sh wouldn't matter.

    It's not about the concurrency, it's about how they have configured their pipelines. gitlab-ci-local phpunit is running the GitLab "phpunit" CI job, not the binary. So if the project is configured to run with run-tests.sh, that's what will be used. It seems that a --file parameter can be passed, so the suggestion on #4 could be on one part of the if/else, and using this param should be on the other.

  • 🇬🇧United Kingdom jonathan1055

    Re the first comment in #7, yes it is on my list of documentation updates to add $_PHPUNIT_TESTGROUPS to the phpunit page.

Production build 0.71.5 2024