This post is listed points that I've worked to make code coverage works for my PHP (Symfony projects). Mostly for reading again in my future but also able to share to everyone.
The VSCode extensions
Name: PHPUnit
Id: emallin.phpunit
Description: Run PHPUnit tests from VSCode.
Version: 4.1.1
Publisher: Elon Mallin
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=emallin.phpunit
Name: Coverage Gutters
Id: ryanluker.vscode-coverage-gutters
Description: Display test coverage generated by lcov or xml - works with many languages
Version: 2.10.4
Publisher: ryanluker
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters
It helps to quickly run test with shortcut, you can have a preset of settings like every setting you could have CLI command or phpunit.xml.dist
.
PHPUnit & Coverage Gutters extension's setting
{
"phpunit.args": [
"--configuration",
"${workspaceFolder}/www/phpunit.xml.dist",
"--coverage-clover",
"var/coverage/cov.xml",
],
"coverage-gutters.showLineCoverage": true,
// Would be very helpful to open Output > coverage-gutters to debug the path in every time we re-run the unit tests
"coverage-gutters.coverageBaseDir": "var/coverage", // equivalent to ${workspaceFolder}/var/coverage
}
To run the unit tests
Press Cmd + Shift + P
to use one of these:
After the unit completed, Cmd + Shift + P
again to use one of these to try with the coverage gutters:
Dealing with PHPUnit mock types
Copy this file to any child folder of the project, better to any ignored folder. Then you can automatically solve the invalid type of VSCode. Which happened when you create mock from a class, and VSCode doesn't know how to detect it as original class or MockObject class, normally it will show as error if you don't do this step.
The code can't get coverage
Here are the check list for solving the problem of not showing code coverage after running test.
- Enable Xdebug for your PHP yet? https://xdebug.org/docs/code_coverage
Install php-code-coverage yet? https://github.com/sebastianbergmann/php-code-coverage
-
PHPUnit configuration: https://docs.phpunit.de/en/9.5/code-coverage-analysis.html
- Add PHPUnit
@cover
tag in test class/functions. - Enable code coverage options for
phpunit.xml.dist
or test command when running with CLI.
- Add PHPUnit
This post are also related to XDebug https://dev.to/arielmejiadev/set-xdebug-coverage-mode-2d9g
Top comments (0)