Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar Java: check the quality of the test classes source code?

Is it possible to check in Sonar the quality of the *Test.java source code, e.g. Methods maximum size 100 lines?

The problem is, that the Java Junit tests are growing with the productive code, also the complexity.

We have unit test classes with more than 1000 lines and 2 methods.

We want to check in Sonar some rules for these *Test.java classes.

like image 691
pan40 Avatar asked Jan 17 '13 10:01

pan40


People also ask

How does SonarQube detect code coverage?

SonarQube uses path-sensitive dataflow engines in combination with static code analyzers to detect such bugs. Besides bugs, it helps you to find code smells.

Does SonarQube only analysis Java code?

SonarQube is an open source quality management platform, designed to analyze and measure your code's technical quality. It is used to test code written in the main programming languages such as C/C++, JavaScript, Java, C#, PHP, and Python, and even a combination of several languages simultaneously.

Does SonarQube run tests?

SonarQube doesn't run your tests or generate reports. It only imports pre-generated reports. Below you'll find language- and tool-specific analysis parameters for importing coverage and execution reports.

How can I get better sonar coverage?

to increase your code coverage, the suggestion is to write tests. It can be unit tests (easiest way) or other tests (Integration test, System tests) which may contribute to coverage when a tool can report coverage for these.


2 Answers

Since Sonar 3.1, it includes a plugin that has specific PMD rules to be executed against the unit tests (a JIRA was created for that). You can see them in the Configuration > Quality Profiles > Coding Rules.

However, it seems that you want to run a full analysis on the test source code, like you do on the production source code, and get additional metrics (for ex. a % rules compliance and also a % rules compliance for unit tests). I don't think that Sonar provides such feature natively. What you can do is to run 2 Sonar analysis:

  1. Your first analysis is the current one;
  2. The second analysis will consider the src/test/java as the "production" source code. Thus, this second analysis will give you the quality of your code. For this analysis, you can specify a specific Maven profile (or an alternative pom.xml) that will change the project information (for ex. it will indicate that src/test/java is the default sourceDirectory).
like image 146
Romain Linsolas Avatar answered Sep 30 '22 20:09

Romain Linsolas


I also noticed that SonarQube will by default ignore the test resources for quality analysis. Using schnatterers answer, i found a simple way to create a separate project only including the test classes as sources in SonarQube, therefore triggering the quality anlysis on them. In the POM of the project i want to analyze i add a profile, which changes the sonar properties accordingly:

    <profiles>
            <profile>
                    <id>analyze-test-classes</id>
                    <properties>
                            <sonar.sources>src/test/java</sonar.sources>
                            <sonar.tests></sonar.tests>
                            <sonar.projectName>${project.name}-tests</sonar.projectName>
                            <sonar.projectKey>${project.groupId}:${project.artifactId}-tests</sonar.projectKey>
                    </properties>
            </profile>
    </profiles>

Running Maven with

mvn sonar:sonar -Panalyze-test-classes

will then activate this profile and create an additional project in SonarQube with the suffix -tests, which only contains the analysis of the test classes.

like image 25
red_hood Avatar answered Sep 30 '22 21:09

red_hood