Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to get clover coverage percentage for one test case on a source file?

kvallish April 14, 2020

Hi All,

I was able to generate clover HTML report for 5 test cases run on an instrumented source. Now, the HTML report shows 80% covered for a source file. The report also gives a way for me to select the tests hitting this source file to show the coverage for a particular test method.

 

1. How can I get this functionality in the JSONReporter? In short, what am trying to do is to build a heuristic to consider a test case for execution if the test hits a source file with 80% coverage. As of today, the JSONReporter gets overall coverage as below:

 

"stats": {
"Complexity": 56,
"CoveredElements": 153,
"ErroneousTests": 0,
"FailingTests": 0,
"PassingTests": 0,
"PcErroneousTests": -1,
"PcFailingTests": -1,
"PcPassingTests": -1,
"TestExecutionTime": 0,
"Tests": 0,
"TotalPercentageCovered": 62.19512,
"UncoveredElements": 93
}
 

2. Why does "Tests", "PassingTests", "FailingTests" field show 0? I have checked multiple source files being hit by this test case and all of them show the fields as 0.

 

Note: Am using distributedCoverage with per-test mapping to get the report for end-to-end tests.

2 answers

1 accepted

0 votes
Answer accepted
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 15, 2020

Hi! Unfortunately, as far as I remember, JSON reporter does not provide per-test coverage statistics, you can find it only in the HTML report.

In order to get the data you want, you can read data directly from the clover.db database. Please have a look at examples provided here:

http://openclover.org/doc/manual/latest/developer-guide--database-structure.html

chapter "Reading from a Clover database". Once you load the database with coverage, you can iterate over packages, source files, classes etc. 

Entities like FileInfo extend the InstrumentationInfo interface which returns an index to retrieve coverage hit count.

The CoverageData class has methods like mapTestsAndCoverageForFile, getTestsCovering or getHitsFor which will let you run different queries. You can get this class from CloverDatabase#getCoverageData.

kvallish April 17, 2020

I tried to get the coverage data from CloverDatabase but coverage is in BitSet. Am not sure how to extract information from it. Are there any short examples of extracting this data that can give me some indication of how it can be done?

I also see that the data present in coverage database is not a direct percentage for a per test coverage. Is it possible to extract coverage percentage for a particular test case, given that this is an end-to-end test and the database is a merged one?

Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 17, 2020

BitSet is a map (covered/uncovered) to code elements (method entries, statements, branches). Index from InstrumentationInfo tells at which bit from a BitSet you have to look.

The com.atlassian.clover.CoverageData#mapTestsAndCoverageForFile returns a map of: test case, bitset with a bitmask applied for a given file.

The FileInfo#getDataIndex returns first index to look at in the bitset above.

The FileInfo#getDataLength returns size - so you look [index .. index + length).

If you count bits set to 1 in this range and compare with getDataLength you will get a pretty good value of per-test code coverage for this file.

kvallish April 19, 2020

Yes, that worked flawlessly. Thank you!

Soneya binta Hossain January 18, 2022
CloverDatabase db = CloverDatabase.loadWithCoverage("joda_time/target/clover/clover.db", new CoverageDataSpec());
ProjectInfo projectInfo = db.getRegistry().getProject();
// print some project details
printProject(projectInfo, System.out);
CoverageData cd= db.getCoverageData();
Map<TestCaseInfo, BitSet> map = cd.mapTestsAndCoverageForFile((FullFileInfo) fileInfo);


I want to get which lines are executed by each test case, but I dont know how to create FullFileInfo

Also can you help out how to get the line numbers of the executed statements for each testcase?

 
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 19, 2022
CloverDatabase#getModel()
FullProjectInfo#getPackageRoots() or #findPackage()
PackageInfo#getFiles()
Soneya binta Hossain January 19, 2022

getFiles returns FileInfo and I can cast this to FullFileInfo.  I was looking for alternatives  I don't have to do casting.

Also, any idea on how to have all tests that cover a particular statement from a file? Is it even possible?

0 votes
kvallish April 14, 2020

I wanted to create this question under Clover but for some reason, I cannot find that product under the dropdown anymore.

Kat Warner
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
April 16, 2020

I have moved the question for you. :)

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events