#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 has-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)
Change History (14)
#2
@
4 years 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 included 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.
#6
@
4 years ago
- Keywords needs-dev-note added
Marking as needs-dev-note
to mention the note in [48847].
Loosely related to #46373