Sonar (JaCoCo) gives 0% coverage if my tests are not in a JUnit4 TestSuite - junit4

Hi I am using sonar and I love it...
But I have a somewhat strange behavior with my latest proj.
If I put all my tests in a JUnit 4 #TestSuite JaCoCo gives me a coverage of 86,2% ( Groove baby !!! ) when I run mvn sonar:sonar
If I let the tests by themselves I get 0% coverage even with the Unit test success
100,0%
I don't need a TestSuite for this proj any clue about why JaCoCo is doing this?
BTW: I am using Maven 2.2.1 + Sonar 3.4.1 + sonar-maven-plugin 1.0.

You do not need to define a test suite if your tests respect the "Test*.java" & "*Test.java" naming convention.
You can have a look at the following sample application for which tests are analysed by JaCoCo: https://github.com/SonarSource/sonar-examples/tree/master/projects/code-coverage/ut/maven/ut-maven-jacoco-runTests

Related

Scala 3, missing lines in coverage report

I'm facing a weird issue while generating coverage report for a Scala 3 project.
Following is an example of how report looks like a method:
because of some reason it is marking statements as uncovered (especially when they are in for comprehension )
NOTE: s3.list returns Try[Vector[S3ObjectSummary]] and my test-case generates both failure and success case.
I'm using sbt-scoverage plugin (v 2.0.0). My scala version is 3.2.2.
Report is generated via sbt clean coverage test coverageReport command.

How to chain SBT task with multi module project

I have configured one SBT multi-module project with scoverage plugin, which is working fine.
To generate test coverage, I am using > SBT clean coverage test coverageReport but is there any way to create a new task which chains internally coverage test coverageReport.
I have tried
Run custom task automatically before/after standard task to create a custom task, but it seems not working with multimodule project.
And one more - http://eed3si9n.com/sequencing-tasks-with-sbt-sequential
Try addCommandAlias like so
addCommandAlias("coverageAll", ";clean;coverage;test;coverageReport")
Now executing sbt coverageAll should generate coverage report for all the sub-projects.

Maven test coverage for scala

I want to do test coverage on my Scala project, for which I use Maven as a build tool.
So I found this:
https://github.com/scoverage/scoverage-maven-plugin
And I looked here:
http://scoverage.github.io/scoverage-maven-plugin/1.3.0/check-mojo.html
So now to check test coverage, I run this:
mvn test
And then:
mvn scoverage:check
However, this only makes the tests to be run. I get no information about coverage.
Also, I tried:
mvn scoverage:report
But the result is the same.
So how can I use this tool or another to get test coverage info in a Scala/Maven project?
I have only used Scoverage with SBT, but chances are the usage is the same.
mvn scoverage:check will only generate some metadata - XML - and compare the generated coverage values against any coverage minimum you might have set up.
With mvn scoverage:report you will get some formatted reports. More docs here.

Can one impose a coverage minimum for combined unit and integration tests?

When I run unit tests for my sbt project (with sbt clean coverage test), I get code coverage of ~77%.
When I run integration tests (sbt clean coverage it:test), I get code coverage of ~10%.
When I run both (sbt clean coverage test it:test), I get code coverage of ~84%.
I'd like to set an aggressive code coverage minimum and fail the build if it's not met, but if I add these build settings:
coverageMinimum := 83
coverageFailOnMinimum := true
...and then run sbt clean coverage test it:test, the coverage minimum is checked after the unit tests, before the integration tests can run, and the build fails:
[error] Coverage is below minimum [77.0% < 83.0%]
If I put it:test before test, it's even worse ([10.0% < 83.0%]).
Is there any way to stipulate that the 83% minimum should apply only after both unit and integration tests have run? Or am I doomed to setting the coverage minimum meetable by my unit tests alone, and always remembering to put test before it:test on the command line?
Automatic post-test coverage minimum check was removed in version 1.3.4 (see issue https://github.com/scoverage/sbt-scoverage/issues/132).
Upgrade plugin version to latest 1.3.5 and call coverageReport after all tests, e.g.:
sbt clean coverage test it:test coverageReport

How to run a single test in scalatest from maven

I have not found any documentation on how to do this. For JUnit the equivalent would be:
mvn -Dtest=org.apache.spark.streaming.InputStreamSuite test
tl;dr mvn test -Dsuites="some.package.SpecsClass"
I found an answer from here and it works:(https://groups.google.com/forum/#!topic/scalatest-users/Rr0gy61dg-0)
run test 'a pending test' in HelloSuite, and all tests in HelloWordSpec:
mvn test -Dsuites='org.example.
HelloSuite #a pending test, org.example.HelloWordSpec'
run all tests in HelloSuite containing 'hello':
mvn test -Dsuites='org.example.HelloSuite hello'
for more details: http://scalatest.org/user_guide/using_the_scalatest_maven_plugin
Found the answer: it is
-DwildcardSuites
So here is the example command line:
mvn -pl streaming -DwildcardSuites=org.apache.spark.streaming.InputStreamSuite test
Update Newer versions of scalatest use
-Dsuites
So the syntax would be:
mvn -pl streaming -Dsuites=org.apache.spark.streaming.InputStreamSuite test
Note that if you have some Java tests in the same module, as much of spark does, you need to turn them off -which you can do by telling surefire to run a test that isn't there
Here is the test that I've just been running
mvn test -Dtest=moo -DwildcardSuites=org.apache.spark.deploy.yarn.ClientSuite
That skips the java test and only runs the scala one.
One thing which scalatest doesn't seem to do is let you run a single test within a suite, the way maven surefire does. That's not ideal if you have one failing test in a big suite.
[Correction 2016-08-22: looks like you can ask for a specific suite by name; look at the other answers below. Happy to be wrong].