My tested code is full of final static classes. and while we can't refactor it for better tests, i have a intermediate solution that runs several small tests on it's own process. and all works fine. but i get no coverage report as one overwrite the other.
I'm currently generating the report in clover, but i am very open to other reports.
my tests only work when phpunit is run as:
/home/y/bin/phpunit --coverage-html clover --coverage-clover clover/report.xml --include-path src/ tests/aTest.php
OK (1 test, 1 assertions)
/home/y/bin/phpunit --coverage-html clover --coverage-clover clover/report.xml --include-path src/ tests/bTest.php
OK (1 test, 1 assertions)
/home/y/bin/phpunit --coverage-html clover --coverage-clover clover/report.xml --include-path src/ tests/cTest.php
OK (1 test, 1 assertions)
But that will result in each run wipping the report from the previous. So i only get a report for the last one. And if i try to run them as phpunit expects to generate the full report, i have the failure because all my classes include their own static ones.
/home/y/bin/phpunit --coverage-html clover --coverage-clover clover/report.xml --include-path src/ tests/
. (first test pass)
PHP Fatal error: Cannot redeclare class Something
make[1]: *** [phpunit_run] Error 255
(but the above will fail even with --process-isolation --no-globals-backup
because are not exactly what they mean...) -- This is NOT yet another question on how to get properly process isolation on php unit. i'm fine running it several times, i just want a full coverage report :)
Is there any way to get the tests to run correctly (i.e. on several process to avoid tainted global class declaration space) like the first code block, but still have comprehensive code coverage report?
thank you!
Schleis answer did not work for me. The unserialize() method throws an error in my case: Error at offset...
However there is a very easy way to achieve this:
Download phpcov as described here.
Have phpunit generate php coverage reports with:
phpunit --coverage-php coverage/cov/foo.cov tests/foo
phpunit --coverage-php coverage/cov/bar.cov tests/bar
Now you have 2 .cov files. Then in your favourite shell:
phpcov merge --clover clover.xml /coverage/cov
It will generate one clover.xml report file.
You will have to script the solution, I am not aware of an option to have phpunit add together coverage from other runs. But the phpunit code makes this not too painful.
You will want to use the --coverage-php
option for getting the coverage report. This will make it easier to combine them all using a php script. This option outputs a file with a serialized PHP_CodeCoverage object. This object has a method merge
that can be used to combine the reports. Then you can use the report object of the appropriate type to generate the output for you.
https://github.com/sebastianbergmann/php-code-coverage/tree/master/src/CodeCoverage/Report
I did have issues with including PHPUnit objects due to the .phar file that is now being used to distribute PHPUnit. You may want to look into using this .gist for your own autoinclude.
Your script would look something like this:
$fileList = ['file1', 'file2', 'file3']; //or read from dir, however you get the list.
$coverageReports = [];
foreach($fileList as $file) {
$coverageReports[] = unserialize(file_get_contents($file));
}
$mainReport = new PHP_CodeCoverage(); //Could also use one of the other ones that were generated.
foreach($coverageReports as $coverageReport) {
$mainReport->merge($coverageReport);
}
//Change this object depending on the format you want.
$htmlReport = new PHP_CodeCoverage_Report_HTML();
$htmlReport->process($mainReport);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With