问题
I'm new to sonar and jacoco and I wasn't able to find an ansewer to following question
We are going to use sonar and jacoco to analyze our test coverage.
We are going to have three kind of test: unit test, integration test (use spring boot test) and acceptance test that we will run on real application instance. We want to merge test results. To generating jacoco files for unit and integration tests is not a problem as they have access to source code. And we can merge this reports in sonar.
My question is it possible to generate jacoco file for acceptance test which interacts with real application? And maybe you will have a link to how to do it?
My best idea for now is to run acceptance tests twice on both real application and embedded one, and get report from embedded. But maybe there is better solution. Thanks,
回答1:
JaCoCo is able to record any kind of execution of Java application - you just need to start this application with agent, for example:
java -javaagent:jacoco-0.8.0/lib/jacocoagent.jar -cp classes Main
By default at termination of application this will produce file jacoco.exec
with execution data. There are also ways to get execution data from running application.
And there are plenty of examples all over the internet of using agent in various cases - with Spring Boot, Tomcat, Weblogic, etc.
After that report can be generated using this execution data jacoco.exec
and exactly the same class files that were used at runtime for generation of execution data. For example using JaCoCo command line interface (there are also Ant tasks, Gradle plugin, Maven plugin):
java -jar jacoco-0.8.0/lib/jacococli.jar \
report \
jacoco.exec \
--classfiles classes \
--html report
Report can be generated without source files, but in this case you won't be able to drill down lower than method level granularity:
Report with source files:
jacoco-0.8.0/lib/jacococli.jar \
report \
jacoco.exec \
--classfiles classes \
--html report \
--sourcefiles src
Now about SonarQube: it performs analysis of exec
and class files, and so also requires that class files are the same as where used at runtime for generation of exec
file. So you need to guarantee that SonarQube analysis is performed for exactly the same class files as under testing.
来源:https://stackoverflow.com/questions/48300162/get-jacoco-report-from-running-acceptance-test-on-real-application