How to run php block several times and combine coverage report?

My tested code is full of finite static classes. and although we cannot reorganize it for better tests, I have an intermediate solution that conducts several small tests on its own process. and everything is working fine. but I do not get a coverage report as it overwrites another.

I am currently creating a report in clover, but I am very open to other reports.

my tests only work when phpunit runs like:

/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 this will cause each run to clear the report from the previous one. Therefore, I receive only a report for the latter. And if I try to run them since phpunit expects to generate a full report, I have a 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 will not work above even with --process-isolation --no-globals-backup , because that’s not exactly what they mean ...). This is NOT another question about how to properly handle isolation on a php block. I work it fine several times, I just need a full coverage report :)

Is there a way to make tests work correctly (i.e. on multiple processes to avoid the hidden space of a global class declaration) as the first block of code, but still has an exhaustive report on code coverage?

Thank you!

+5
source share
2 answers

Schleys's answer did not help me. The unserialize () method throws an error in my case: Error at offset ...

However, there is a very simple way to achieve this:

Download phpcov as described here .

Have phpunit to generate php coverage reports with:

 phpunit --coverage-php coverage/cov/foo.cov tests/foo phpunit --coverage-php coverage/cov/bar.cov tests/bar 

You now have 2 .cov files. Then in your favorite shell:

 phpcov merge --clover clover.xml /coverage/cov 

It will generate a single clover.xml report file.

+7
source

You will need a script solution, I do not know about the possibility of adding phpunit together from other runs. But phpunit code makes this not too painful.

You want to use the --coverage-php option to get a coverage report. This will simplify their combination using a PHP script. This option displays a file with the serialized PHP_CodeCoverage object. This object has a merge method that you can use to combine 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 had problems including PHPUnit objects due to the .phar file that is now used to distribute PHPUnit. You might want to learn this one . Gist for your own automatic inclusion.

Your script will 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); 
+1
source

Source: https://habr.com/ru/post/1215313/


All Articles