Code coverage for scala with separate test project in java - java

I have two separate projects, Product and CompoTestProduct. Product is written in Scala and uses sbt while CompoTestProduct is written in Java and uses maven. Product will be deployed in a server and CompoTestProduct's tests classes will be ran locally. I tried to use scoverage as it says that it supports Multi project reports but I still have no idea how to do this and I'm still struggling to make the samples work. I am getting this error on the sample projects:
[error] Not a valid command: coverage
[error] Not a valid project ID: coverage
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: coverage (similar: homepage, package, compilerCache)
[error] coverage
[error] ^
I am new to this code coverage testing thing. Do you have other tutorials or maybe know other framework/toolkit that can generate test code coverage report for do this setup?

The multi project support refers to being able to aggregate the results of multiple reports.
Scoverage instruments your code when you activate coverage on the sbt command line. So sbt clean coverage compile would be enough in your Product project to get the classfiles instrumented.
Then you could run your unit tests as normal with Maven. At this point Maven needs to be configured to also use Scoverage, as it will need to write out the coverage data once it is completed.
Then you would need to run the report step after.
So, summary, it is possible, with a LOT of hassle, but why are you going through these hoops to have an awkward project setup? Just move your tests into the main project, do a combined java/scala compile and it should be much easier as you could run the entire build via sbt or maven, and not both mixed.

Related

I am using Gauge framework with Java and maven and wanted to implement Test coverage using Jacoco. At spec level,

For example if 10 specs are run. then code coverage report should be generated for the same. I have my classes in src/test/Java folder.
Is there any way to achieve this?
I have tried using antrun plugin. It's getting executed but code coverage for all the classes is displayed as zeros. however execution of specs are successful and it's pass

Aggregated Sonar report for mutiModule maven Project

I have a setup for multi Module module project something like this
Module1
|
submodule1
|
submodule2
I have written a Junit test in submodule 1 and it's covering the code of submodule 2 also but when I try to see coverage in sonar it's showing 0% for submodule 2 is there any way to show coverage of submodule2 ? Also I can generate aggregated xml report by running mvn clean install jacoco:report-aggregate but how can I feed this aggregated report into sonar ? How to setup pom of module as well as submodules ?
I'm not sure if this is what you are doing, but I'll say it anyway. If you are creating submodule1 as 'code' and submodule2 as 'tests', that's a horrible way to organize it. Tests (specifically, unit tests) should be stored with the code that it is testing. Thus, each submodule has src/main/java and src/test/java.
Now, that said, it is plausible that you have a 'common-lib' module, and then additional modules that depend on that lib (e.g. application tier). It is highly likely that you run a test at the application-tier and it invokes code in the lib module. But you get no code-coverage credit for that. Jacoco coverage of the lib will be solely based on the tests in the lib. The 'app' tests only give you coverage of the app. This has to do with the instrumentation that happens when jacoco runs - it's only going to instrument the things local to the module it is running in.
Yes, there is a jacoco-aggregate report, but this will merge the module reports together - single report of each module. It does NOT give you unified coverage of an app-tier test calling lib methods, etc.
Lastly, sonar. I believe as long as you have jacoco files hanging around, the sonar-scanner will make use of them. I think it operates on the raw jacoco.exec file, but may be able to interpret a jacoco-result.xml or jacoco-aggregate.xml. If it is giving you grief, include your pom.xml

Java code coverage for other source code repository

I've maven project1, which has src/main/java -- application source code
I do have another maven project2, which has all tests src/main/test -- all api tests which are all part of above project1
Now, Is there any way to configure Java Jacoco code coverage agent in project2 in such away that It should calculate code coverage of project1 when execute tests from project2 ?
Thanks in advance, I know it's kind of theoretical question.
Speaking in general - yes it is possible:
By default agent collects information about execution of all the
classes, no matter from where they come.
Report generation requires
collected information about execution, plus original class files and
sources for which you want to generate report.
If both projects are part of the same Maven reactor, then have a look at jacoco simple integration test solution . If not, then JaCoCo Ant Tasks that provide flexibility in specification of paths - http://www.jacoco.org/jacoco/trunk/doc/ant.html

Adding jacoco integration tests coverage for Sonar

Background:
I have 2 separate java projects, call them A and B.
Project A is the actual product (war application), with unit-tests. Gradle builds the project and then runs sonar analysis, and I can see the unit-tests coverage in Sonar.
Project B is an integration test for the first project. It is run by Jenkins in a pipeline after building project A and deploying it on an integration-environment. The deployment also involves instrumenting the code so that the jacoco-it report will correlate to Project A's classes.
My question:
How can I add to project A's sonar page, which currently has only unit-tests coverage, the integration tests coverage - as a second step?
The flow I need is:
build project A
run sonar analysis on project A (now it'll show unit-tests coverage on its sonar page)
build project B
run sonar analysis on project B - which will simply add integration-coverage to project A on sonar
It's currently not working. when I run sonar analysis on project B it messes up project A's sonar page, removing the unit-test coverage of A.
The flow that I have now, which is working but I want to change is:
build project A
run sonar analysis on project A
build project B
run jacoco report on project B. this outputs the jacoco-it.exec file into a specific location on my disk
run sonar analysis on project A (it has a setting to take the jacoco-it.exec file from the specified location
now project A's sonar page will show both unit-tests and integration-tests coverage, but step 5 is completely redundant and I want to avoid it.
Any suggestions?
what i will suggest is a bit different:
employ some build automation tool
create a sonar 'checker' job, which doesnt do the full analysis; just the incremental, this should break when add quality gate breaker issues
create a nightly job for your A project with full analysis: -Dsonar.analysis.mode=analysis (note: my experience shows that in this case you must run an incremental prior to this analysis, because in analysis mode sonar accepts new quality gate breaker issues
i don't think integration tests should count into project test coverage, so that might be just left out.
You cannot amend a previous analysis, which is why your 'B' job appears to replace the unit test numbers. What you need to do is generate your integration test coverage report before analysis, and make that report available so that both unit test and integration test numbers can be read in the same analysis.
From a build pipeline standpoint, that could get tricky depending on how you need to structure the jobs, but off-hand it sounds like you need 3 jobs:
build project A
build project B & generate integration test report
job 3 pulls integration test report from job 2, and either rebuilds project A or pulls code, classes, and unit test report from job 1 and performs analysis

How can I get both Scala and Java Cobertura coverage?

I have a Scala project built with SBT. I use Jenkins to do my build, and include the following as a build target:
scoverage:test
This gets me very nice coverage reports for my Scala sources using the Cobertura plugin for Jenkins.
My project has Java files as well, however, and I get no coverage for them. If this were Maven, I'd know what to do. But being SBT, I'm a bit unclear on how I can get coverage for the Java sources and then ensure that Jenkins sees the output and displays it as well.
I do know that the Java tests run using "sbt test" so that's not an issue.
Is this even possible?

Categories

Resources