user944849
user944849

Reputation: 14981

Missing SonarQube metrics for test files with 6.3

Using SonarQube 6.3, sonar-maven-plugin 3.3.0.660, Java 8, on a multi-module Maven project.

On the source files, code coverage (with JaCoCo) is working well, as are the analysis plugins, duplication detection, test executions and duration etc. But, we have no metrics for test files. The files are being loaded into SonarQube, but Lines of Code and Duplications are blank (as is Coverage but that one makes sense), while Bugs, Vulnerabilities, and Code Smells are all 0. We may have something configured incorrectly but I can't figure out what.

Example: one module contains Java and XML source and test files. The sonar properties in the POM are:

<sonar.sources>.</sonar.sources>
<sonar.inclusions>src/main/**/*,src/app/**/*,**/*.xml,**/*.properties,**/*.json</sonar.inclusions>
<sonar.tests>.</sonar.tests>
<sonar.test.inclusions>src/test/**/*,e2e/**/*</sonar.test.inclusions>

In SonarQube --> Administration --> Analysis Scope, we have Import Unknown Files enabled. We also have the property sonar.global.exclusions set to target/**/*

When running an analysis with the Maven plugin, we see messages like this in logs:

[INFO] 13:01:35.726 Base dir: /JENKINS_WS/project/module
[INFO] 13:01:35.726 Working dir: /JENKINS_WS/project/module/target/sonar
[INFO] 13:01:35.726 Source paths: .
[INFO] 13:01:35.726 Test paths: .
[INFO] 13:01:35.726 Source encoding: UTF-8, default locale: en_US
[INFO] 13:01:35.726 Index files
[INFO] 13:01:35.726 Included sources: 
[INFO] 13:01:35.726   src/main/**/*
[INFO] 13:01:35.726   src/app/**/*
[INFO] 13:01:35.726   **/*.xml
[INFO] 13:01:35.726   **/*.properties
[INFO] 13:01:35.727   **/*.json
[INFO] 13:01:35.727 Excluded sources: 
[INFO] 13:01:35.727   target/**/*
[INFO] 13:01:35.727   src/test/**/*
[INFO] 13:01:35.727   e2e/**/*
[INFO] 13:01:35.727 Included tests: 
[INFO] 13:01:35.727   src/test/**/*
[INFO] 13:01:35.727   e2e/**/*
...

It appears that files in the test directories are being indexed properly:

[DEBUG] 13:01:35.778 'src/test/resources/log4j.xml' indexed as test with language 'xml'
[DEBUG] 13:01:35.779 'src/test/resources/appContext-test.xml' indexed as test with language 'xml'
[DEBUG] 13:01:35.779 'src/test/java/com/company/module/EnvEchoTest.java' indexed as test with language 'java'

And the Java scanner reports that the single test file was analyzed too:

[INFO] 13:01:38.520 Java Test Files AST scan
[INFO] 13:01:38.520 1 source files to be analyzed
[DEBUG] 13:01:38.521 'src/test/java/com/company/module/EnvEchoTest.java' generated metadata as test  with charset 'UTF-8'
[DEBUG] 13:01:38.529 ----- Classpath analyzed by Squid:
[DEBUG] 13:01:38.529 /JENKINS_WS/project/module/target/test-classes
[DEBUG] 13:01:38.529 /JENKINS_WS/project/module/target/classes
... snip printing all the jar file debug msgs ....
[INFO] 13:01:38.567 1/1 source files have been analyzed
[INFO] 13:01:38.567 Java Test Files AST scan (done) | time=47ms

But the metrics are not populated in SonarQube. Can anyone provide pointers?

Upvotes: 0

Views: 1654

Answers (1)

G. Ann - SonarSource Team
G. Ann - SonarSource Team

Reputation: 22854

All is well. Or at least working as designed.

SonarQube is designed to report on the quality of your code, and tests aren't really considered to be code. They're... supporting files.

That said, there is a set of rules written specifically to examine the quality of your tests as tests. If included in your profile, they might raise issues on your tests. But so-called normal rules won't be run against tests, and metrics will not be calculated.

If you really want those things, you'll need to set up a separate, second analysis of your project, where the sonar.sources property points to the tests directory.

Upvotes: 2

Related Questions