-
-
Notifications
You must be signed in to change notification settings - Fork 377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add Cobertura report format #734
Conversation
Here's a rather long example of a Cobertura report from some Java project: https://gist.github.com/apetro/fcfffb8c4cdab2c1061d |
Now to refactor and think about some aspects of the report that may have to change.
Now it passes tests, but is ugly as heck. Next up is refactoring, but before that happens I would like to know the following:
As in:
Not sure what Also still not sure what to do with |
I do not need support for Cobertura (but am curious to learn why you need it) and do not have the time to do the research required to answer these questions, sorry. |
*/ | ||
class CoberturaTest extends TestCase | ||
{ | ||
public function testCloverForBankAccountTest(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be testCoberturaForBankAccountTest
instead of testCloverForBankAccountTest
.
A Cobertura report would allow integrating 'modern' code coverage Jenkins plugins (ex. https://jenkins.io/blog/2018/08/17/code-coverage-api-plugin-1/), for those using it as CI. A tool (or XSLT file) that would convert one of the already existing formats in phpunit into Cobertura, would also do the trick, but I couldn't find any. IMO, it's just a nice to have. |
…pelines The plugin is a source of issue [0] which is unlikely to get some attention soon [1]. It also sometimes crash when analyzing the reports which completely fail the pipeline even if all the tests have been passed successfully. The Javascript now uses the modern approach to do code coverage in Jenkins [2]. For the PHP code coverage the situation is less pretty. PHPUnit cannot output a report in a format understood by the Jenkins plugin which means we loose the high level overview. The HTML report is still browsable from the archive and it looks like there is currently some traction to add a format that would be supported by the Jenkins plugin [3] (we can also consider writting a small tool to do the format conversion). [0] jenkinsci/clover-plugin#21 [1] jenkinsci/clover-plugin#21 (comment) [2] https://jenkins.io/blog/2018/08/17/code-coverage-api-plugin-1/ [3] sebastianbergmann/php-code-coverage#734 Change-Id: Ic4915047616a7023515939dd305a23f12f2300cc
Yes, a nice to have from my part as well, as it seems many popular test frameworks and tools in popular languages have Cobertura support of some sort. Mostly my motivation is to be able to do merging and managing coverage reports a little easier between languages and test suites inside larger code bases. |
An idea for "non-OOP" coverage reporting with Cobertura: Treat each file that contains PHP code to be reported upon as a "class" with the class name being the file name instead. Maybe put this behavior behind a flag So a <coverage ...>
<sources>
...
</sources>
<packages>
<package ...>
<class name="functions.php" filename="/path/to/functions.php" line-rate="0.5" ...>
<methods>
<!-- the "method" below is actually just a bare function inside functions.php -->
<method name="hello_world" signature="hello_world(string $val) : string">
...
</method>
</methods>
<lines>
<!-- these in addition to the faux methods above will report non-function code in files -->
<line num="1" hit="1" />
<line num="2" hit="1" />
<line num="3" hit="1" />
<line num="4" hit="2" />
<line num="5" hit="2" />
<line num="6" hit="2" />
...
</lines>
</class>
</package>
</packages>
</coverage> Also from the looks of the example report I linked earlier, the |
@rask I would like to test out your updates for Cobertura reporting. How do I get your changes integrated and usable with PHPUnit? |
999abec
to
67341a5
Compare
I'm also interested in this feature since GitLab chose Cobertura for their Test Coverage Visualization feature. The code in this pull request already works perfectly for this use case with only minor changes, and I'm using it in CI/CD. A few comments:
@johnwc I found it easiest to use a custom PHPUnit ResultPrinter to generate the report: Gist |
@xconstruct Until this update gets put in place and released, I have had to use this other tool in our CI pipeline to convert php unit's clover code coverage report into Cobertura. Prior to running the conversion, I have to run a simple PS script that I wrote that moves all the flat file php files into a "Unstructured" package in the clover report. So that it can be better organized in the report between code that is in real class vs waterfall php code. |
I need cobertura report to publish code coverage results to Azure Pipelines. it's only support cobertura and JaCoCo formats. |
@aufalm look at my previous comment, there is a task in Azure DevOps that will convert it for you. |
@johnwc Thank you |
Thank you for your contribution. I appreciate the time you invested in preparing this pull request. However, I have decided not to merge it. |
@rask Are you by any chance making this into a package or something? :) |
@martindilling please review the entire thread, there is already a working solution to get it working in Azure. |
I'd like to second the request from @martindilling for a package. I'd like to use it for coverage in Gitlab pipelines. |
If you can run bash in gitlab, you can use the same utility that I pointed out to convert the report to cobertura. |
I have, and I have gotten it to work. But I would prefer to have it handled by phpunit. |
Just in case I can save someone else some time, here's what I ended up with after a lot of searching, trial and error: I did as @johnwc suggested and installed ReportGenerator (specifically dotnet-reportgenerator-globaltool) on my test docker container. Used it to convert the clover xml to
The tricky part was then matching the So for my Laravel app the repository directory structure is:
And my pipeline runner would output something like: <coverage>
<sources>
<source>./app</source>
</sources>
<packages>
<package name="App" line-rate="1" branch-rate="1" complexity="NaN">
<classes>
<class name="/builds/my-group/my-project/app/User.php" filename="/builds/my-group/my-project/app/User.php" line-rate="1" branch-rate="1" complexity="NaN">
...
</class>
</classes>
</package>
<package name="App\Http\Controllers" line-rate="1" branch-rate="1" complexity="NaN">
<classes>
<class name="/builds/my-group/my-project/app/Http/Controllers/HomeController.php" filename="/builds/my-group/my-project/app/Http/Controllers/HomeController.php" line-rate="1" branch-rate="1" complexity="NaN">
...
</class>
</classes>
</package>
...
</package>
</coverage> I found a couple of commands that would help to translate the output to what GitLab expects:
Examples after the above with updates: |
Shawn McCabe wrote on Twitter:
I was not aware that more people than @rask, who created this pull request, would be interested in this. Furthermore, the There seems to be enough interest to warrant the inclusion of this report into this library as well as into PHPUnit (CLI option, XML configuration).
Since version 9 (used by PHPUnit 9.3), this library has support for line, branch, and path coverage. The data you need is available.
This is a limitation I can live with. We just need to make sure to explain it, in the documentation, for instance. TL;DR: I will accept this when it is based on current |
Hello. Been super busy and have not had time to improve upon this. Sorry! Someone else can freely go ahead and fork my work on this in case they are in a rush, otherwise I might pick this up later on. Thanks! (Also GitHub notifications are not working for me for some reason, so I did not see the discussion here.) |
We should create another pull request in sebastianbergmann/phpunit to add CLI option, XML configuration. This could be something like:
phpunit --coverage-cobertura cobertura.xml
<report>
<cobertura outputFile="cobertura.xml"/>
</report> We need to change I have another question to we generate a generic |
I will gladly implement the configuration on the PHPUnit side of things once the Cobertura report generator has been implemented. |
@Daniel-Mendes What do you mean, with your last statement about "versions" of cobertura? Each of those hosting companies do not have their own version, they merely read the standard formatted cobertura xml doc. |
@johnwc I was talking about this comment, I know all platforms use the same standardized format but with specificities:
|
I've got a PR ready already for phpunit and I'm working on an updated version of this PR, I'll post both later today, not quite ready yet. Just wanted to let people know so no one ended up duplicating work. |
New PR here: #812 |
@Daniel-Mendes that's not the issue with the sites, it's an issue with cobertura. As cobertura was designed to be used with java, that had only classes and namespaces. We just have to come up with a way to mimic that for php files that are more script based than class based. And this gist you linked is a dead link. |
Superseded by #812. |
Attempting to add Cobertura support, addressing #6, in case this is something we want to have.
The report should be valid against http://cobertura.sourceforge.net/xml/coverage-04.dtd, and I need to know how to represent the
branch-rate
and related data. Is that something the coverage collector can provide?line-rate
and related are simple enough.The tests are work in progress as well, need to add the example files for the other types of coverage reports as well.
Other notes:
Cobertura, as originating in the Java world, has no concept of code outside classes. This means we probably can't display "raw" PHP file code coverage (e.g. stuff like
functions.php
,index.php
and so on) in these reports unless we mangle the data to fit report structure in some way.