WordPress.org

Make WordPress Core

Opened 6 months ago

Closed 3 months ago

Last modified 3 weeks ago

#50267 closed defect (bug) (fixed)

Getting code coverage from phpunit?

Reported by: mmarco9 Owned by: SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: minor Version: 5.1
Component: Build/Test Tools Keywords: has-patch needs-dev-note
Focuses: Cc:

Description

I would like to obtain test coverage by using phpunit. I am passing the parameter --coverage-text=cov.txt, however the output tells me of 0% code coverage. I am wondering if I am doing something wrong, or if it's not possible to obtain code coverage on these tests.
I appreciate any help!

Attachments (3)

cov.txt (206 bytes) - added by mmarco9 6 months ago.
50267-1-fix-config.patch (3.7 KB) - added by jrf 3 months ago.
Fix the code coverage configuration in phpunit.xml.dist
50267-2-fix-invalid-covers-tags.patch (9.2 KB) - added by jrf 3 months ago.
Fix invalid code coverage tags

Download all attachments as: .zip

Change History (13)

@mmarco9
6 months ago

#1 @jrf
3 months ago

Loosely related to #46373

#2 @jrf
3 months ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 5.6
  • Version set to 5.1

@mmarco9 I've looked into this and you are completely right.

The standard phpunit.xml.dist and tests/phpunit/multisite.xml files, both set the WP_RUN_CORE_TESTS constant.

With the WP_RUN_CORE_TESTS constant in place, the source code used for the unit tests is in the build directory created by running npm run build, not in the src directory.

This is loosely related to the changes made in #43055.

I'm attaching two patches to this ticket:

Patch 1

A patch fixing code coverage setup in the phpunit.xml.dist file to point to the build directory which makes the config consistent with the WP_RUN_CORE_TESTS being set to 1 in the same config file.

This patch includes updating the list, as added in #43067 / #43240 / [42665], to be in line with the current code base and excluding the plugins and themes included with WP Core from being considered for code-coverage.

If people locally have a phpunit.xml file which changes the WP_RUN_CORE_TESTS constant to 0, they would also - in their local file - have to change the code coverage setup to use the src directory.

Patch 2

A patch to fix a number of malformed @covers tags. These will throw warnings in higher PHPUnit versions and will at some point no longer record the coverage properly (I haven't dug in for the exact PHPUnit version numbers).

As per the documentation of the `@covers` tag, global functions being covered should be prefixed with ::. Also, the () behind the function name is redundant.

This patch is a follow-up on #39235.

I'd also recommend for tests covering classes to be explicit about the fact that these are global classes by adding a \ at the start, i.e. @covers \SomeClass, not @covers SomeClass, but that is not included in the current patch.

@SergeyBiryukov These are the two patches I mentioned yesterday.

Last edited 3 months ago by jrf (previous) (diff)

@jrf
3 months ago

Fix the code coverage configuration in phpunit.xml.dist

@jrf
3 months ago

Fix invalid code coverage tags

#3 @SergeyBiryukov
3 months ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#4 @SergeyBiryukov
3 months ago

In 48847:

Build/Test Tools: Update code coverage setup in the phpunit.xml.dist file to point to the build directory.

This makes the configuration consistent with the WP_RUN_CORE_TESTS constant being set to 1 in the same config file.

Additionally, update the list to be in line with the current codebase, excluding the plugins and themes bundled with WordPress core from being considered for code coverage.

Note: If you have a local phpunit.xml file that changes the WP_RUN_CORE_TESTS constant to 0, you should also - in the same file - change the code coverage setup to use the src directory.

Follow-up to [42665], [43311].

Props jrf, mmarco9.
See #50267.

#5 @SergeyBiryukov
3 months ago

In 48848:

Tests: Correct a number of malformed @covers tags.

As per the documentation of the @covers tag, global functions being covered should be prefixed with ::. Also, the () after the function name is redundant.

Follow-up to [39918].

Props jrf.
See #50267.

#6 @SergeyBiryukov
3 months ago

  • Keywords needs-dev-note added

Marking as needs-dev-note to mention the note in [48847].

#7 @SergeyBiryukov
3 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

This ticket was mentioned in Slack in #core by sncoker. View the logs.


6 weeks ago

This ticket was mentioned in Slack in #core by desrosj. View the logs.


3 weeks ago

This ticket was mentioned in Slack in #core by daisyo. View the logs.


3 weeks ago

Note: See TracTickets for help on using tickets.