Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get unit coverage percentage (eg from jacoco) on only new code?

My hypothethical scenario is this:

  • I have run my junit tests and generated a jacoco file
  • my git diff shows I have changed 10 lines of code

Now the intersection of my git diff and the jacoco information is that 7 of the 10 lines of code changed are covered. ie - I have 70% coverage on new code.

But I had to work that out manually.

I'd like an automated way to work out the percentage how many new lines of code are covered.

My question is: Is there a way to get unit coverage percentage (eg from jacoco) on only new code?

(Note I know sonarqube can do this if you run the scanner with analysis.mode=publish and get interrogate the task result with the JSON API - I'm looking for something lightweight that a developer can run locally.)

like image 660
hawkeye Avatar asked Jan 25 '17 12:01

hawkeye


People also ask

How to get code coverage report in JaCoCo?

Step 4: To get you code coverage report navigate to the target > site > jacoco > index.html > right-click > Open In > Browser > And your preferred browser. So basically index.html is your code code coverage report file. So you can see your report will be shown like this and the percentage completely depends on how you have written the test cases.

What is JaCoCo and how to use it?

This tutorial describes the usage of the Jacoco, which can be used to check the code coverage of Java projects. 1. Jacoco Jacoco is an open source project, which can be used to check production code for test code coverage. It creates reports and integrates well with IDEs like the Eclipse IDE.

What are the 38 instructions shown by JaCoCo in the report?

The 38 instructions shown by JaCoCo in the report refer to the bytecode instructions instead of Java code instructions. The JaCoCo reports help you visually analyze code coverage by using diamonds with colors for branches and background highlight colors for lines. A brief explanation of the diamonds seen in the code coverage report is below:

How to check if the rules set in JaCoCo have been met?

You can run maven clean verify to check whether the rules set in jacoco:check goal are met or not. The log shows “All coverage checks have been met.” as our code coverage score is 94% which is greater than our minimum 50%.


2 Answers

There are two plugins:

  • Gradle: diff coverage gradle plugin
  • Maven: diff coverage maven plugin

Both are built on top of JaCoCo and able to generate JaCoCo like reports. enter image description here

Also, they able to fail the build if your coverage ratio is less than expected(the ratio colud be configured by the plugins)

like image 123
SergiiGnatiuk Avatar answered Oct 01 '22 20:10

SergiiGnatiuk


Jacoco + diff-cover

It is work for me. CI Shell script:

export PATH="${jobPath}"/buildbox/Python-3.8.2/bin:$PATH
python --version
python -m pip install -i http://xx.xx.xx.com/artifactory/api/pypi/xx-pypi-public/simple/ --trusted-host xx.xx.xx.com diff_cover==4.0.1

mvn clean test -Dmaven.test.failure.ignore=true

diff-cover "${WORKSPACE}"/xxx-test/target/site/jacoco/jacoco.xml --compare-branch origin/"${target_branch}" --src-roots $(find . -name java -type d | grep 'src/main/java$' | egrep -v "target|test|testkit") --html-report ${WORKSPACE}/xxx-test/target/site/jacoco/index.html
like image 23
Languoguang Avatar answered Oct 01 '22 21:10

Languoguang