Allow run-tests.sh to generate coverage reports

Created on 23 May 2018, almost 6 years ago
Updated 3 May 2024, 19 days ago

Problem/Motivation

The testbot is built with phpdbg. This allows us to generate coverage reports from PHPUnit with minimal performance penalty. This can be seen in the testbot's native test runs: https://dispatcher.drupalci.org/job/DrupalCI_CI_Ci_ci/403/

The problem: run-tests.sh can't natively handle coverage, and we don't want to run tests twice just for a coverage report.

We also have a situation where if we ran the phpunit tool to generate a full coverage report, it would take a whole day, or maybe longer. So the advantage of run-tests.sh in this regard is concurrency.

Proposed resolution

run-tests.sh invokes simpletest_script_run_one_test() which can use phpdbg instead of php if we want it to generate a coverage report.

We then have PHPUnit generate coverage using --coverage-php (named .cov files by convention).

At the end of the test run, we use phpcov to aggregate the .cov files into a Clover XML file.

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Needs work

Version

11.0 🔥

Component
PHPUnit 

Last updated about 6 hours ago

Created by

🇺🇸United States Mile23 Seattle, WA

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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.

  • 🇬🇧United Kingdom jonathan1055

    Thanks for looking at this. Just bumping a comment as it's been 3 years since the last one. Things have probably changed a bit in that time, but having any sort of code coverage report would be an excellent feature to have, for core and contrib.

  • Status changed to Needs review over 1 year ago
  • 🇺🇸United States Mile23 Seattle, WA

    Here's a re-roll-ish for 10.1.x. When I run it, phpcov runs out of memory for a very small set of tests.

    If anyone wants to pick this up, they're welcome to it. :-) I don't have much time to dedicate to it these days.

    Setting to needs review to get a test run, but really the next person to come along is welcome to set it back to needs work.

  • First commit to issue fork.
  • 🇺🇦Ukraine voleger Ukraine, Rivne

    Moved patch #38 into MR
    Added new items to CSpell dictionary

  • 🇬🇧United Kingdom jonathan1055

    Nice work, thanks @mile23 and @voleger. Three other observations:
    a) The dispatcher log seems to have run parts of the assessment twice. Is this intentional, or just that the log has duplicated the output?
    b) Can we avoid running phpstan on these tests, as that output is unrelated to the MR
    c) How can we actually test this here? Can it be run on a small contrib module, or a small subset of core tests?

  • 🇧🇪Belgium mallezie Loenhout

    Just FYI

    B) The phpstan errors. These are actually caused because the phpstan library and phpstan-drupal library are updated in this MR. This seems out of scope here. Also some other libraries seem updated, which seems also out of scope.

  • 🇺🇸United States Mile23 Seattle, WA

    Re: @jonathan1055 #43.c:

    This issue adds a feature that isn't on any maintainer's roadmap, as far as I can tell.

    Once upon a time there was #2624926: Refactor run-tests.sh for Console component. which would refactor run-tests.sh and make it easier to test the test runner, but that's fallen way behind.

    Core maintainers have expressed an interest in having coverage reports, but clearly it's not a priority.

    One can always run tests using the phpunit test runner and generate coverage for the parts of code you're interested in. For all of Drupal Core this would take a very long time, but it's doable. It's also doable per-module, for instance, for your contrib module or what-have-you.

  • Status changed to Needs work over 1 year ago
  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇫🇷France andypost

    according to https://php.watch/articles/php-code-coverage-comparison

    the fastest driver is pcov and it already works with PHP 8.3 (perf of xdebug 3 vs phpdbg is nearly the same nowadays)

  • 🇫🇷France andypost

    @voleger having now gitlab.ci we can prepare special php-image option to enable pcov for tests and report it to gitlab

  • 🇫🇷France andypost

    As of PhpUnit 10 only pcov and xdebug 3 are supported 🌱 [meta] Support PHPUnit 10 in Drupal 11 Active

  • 🇺🇸United States neclimdul Houston, TX

    seems like with gitlab we could start moving toward just using phpunit directly? it would make this pretty trivial and could better integrate with all the other features gitlab provides

  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    I'm trying to set up coverage in https://git.drupalcode.org/project/distributions_recipes/-/merge_request... for a fork of core for an initiative. Ideally to use gitlab's built in coverage I'd be able to add --coverage-text --colors=never to our phpunit command and coverage: '/^\s*Lines:\s*\d+.\d+\%/' to the phpunit gitlab job but PHPUnit is unable to detect a code coverage tool - I guess looking at https://php.watch/articles/php-code-coverage-comparison#phpdbg maybe we can just run it using phpdbg... will see.

  • 🇫🇷France andypost

    I think it will be easier to add disabled by default pcov to all images so it can be enabled with

    Will work to provide -dev images to unblock testing #3404084: Add PCOV extension to images

  • 🇫🇷France andypost

    PHP 8.3 images now has pcov and tests showed that its usage increasing execution time ~30%

    The CR https://www.drupal.org/node/3442363

    So now core can explore usage

  • 🇫🇷France andypost

    8.1 and 8.2 images also updated to have pcov and xdebug preinstalled but not enabled

  • 🇺🇸United States neclimdul Houston, TX

    I suspect the Xdebug problem was just it running in the wrong mode. We could probably add the environment variable or set the default config to always run in coverage mode. I couldn't find the failed test though to confirm this.

    Is there a advantage to adding pcov? Xdebug has always worked well enough for me.

  • 🇫🇷France andypost

    @neclimdul you can see changes and pipelines https://git.drupalcode.org/project/distributions_recipes/-/merge_request...

    basically

        - docker-php-ext-enable xdebug
        - XDEBUG_MODE=coverage sudo --preserve-env=XDEBUG_MODE,SIMPLETEST_DB,SIMPLETEST_BASE_URL,BROWSERTEST_OUTPUT_DIRECTORY,BROWSERTEST_OUTPUT_FILE,MINK_DRIVER_ARGS_WEBDRIVER -u www-data vendor/bin/phpunit -dzend_extension=xdebug -dxdebug.mode=coverage -v -c phpunit.xml.dist --coverage-text --colors=never --log-junit junit-reports/recipe-tests.xml
    
  • 🇺🇸United States neclimdul Houston, TX

    Yeah, that looks right. I was suggesting we just add that environment variable to the default container config or the CI config since its unlikely anyone would want any other xdebug mode in CI.

    on the php directives, looks like you where passing '-dzend_extension=xdebug -dxdebug.mode=coverage' to phpunit script but you'll need to pass them to php like php -dzend_extension=xdebug -dxdebug.mode=coverage ./vendor/bin/phpunit.

  • 🇫🇷France andypost

    Somehow test suite starting to fail (good example phpdbg -qrr ...phpunit) when PHP executable pointed

    Moreover XDebug suggests to use filters for coverage and it should be faster as promoted

  • 🇺🇸United States neclimdul Houston, TX

    You can't run the entire test suite with coverage, it will definitely die. When I ran coverage reports every commit during the 8.x release cycle, I'd only run the unit tests because it doesn't really make sense to run the other tests. They don't provide @covers, and they don't cover specific pieces of code but features. Also, because like you'd have a million tests populating "coverage" for the bootstrap but none of them testing anything.

    We actually do the filtering in core its just been renamed in xdebug 3 for obvious reasons.

      <coverage>
        <include>
          <directory>./includes</directory>
          <directory>./lib</directory>
          <directory>./modules</directory>
          <directory>../modules</directory>
          <directory>../sites</directory>
        </include>
        <exclude>
          <directory>./modules/*/src/Tests</directory>
          <directory>./modules/*/tests</directory>
          <directory>../modules/*/src/Tests</directory>
          <directory>../modules/*/tests</directory>
          <directory>../modules/*/*/src/Tests</directory>
          <directory>../modules/*/*/tests</directory>
          <directory suffix=".api.php">./lib/**</directory>
          <directory suffix=".api.php">./modules/**</directory>
          <directory suffix=".api.php">../modules/**</directory>
        </exclude>
    </coverage>
    

    Aside from all that, in general I wouldn't surprised if even unit tests for core had problems. I stopped running coverage reports because they'd constantly get broken because core wasn't running them so they don't get reviews and commits would constantly break them in weird ways. That's why I'm here! :-D

  • 🇦🇺Australia alex.skrypnyk Melbourne

    xdebug will die on large codebases. pcov won't. try running the whole suite with pcov and compare the coverage scope and run speed (should be 10x faster than xdebug)

Production build 0.69.0 2024