【问题标题】:SonarQube - Java - Sonar Scanner not analyzing src/test/java foldersSonarQube - Java - 声纳扫描仪不分析 src/test/java 文件夹
【发布时间】:2018-08-30 15:36:22
【问题描述】:

我下载了 SonarQube 7.0,并设置了 Maven 项目以使用声纳扫描仪。 pom.xml 如下所示:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.jitpack</groupId>
<artifactId>maven-simple</artifactId>
<version>0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Simple Maven example</name>
<url>https://jitpack.io/#jitpack/maven-simple/0.1</url>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <mavenSurefireVersion>2.20</mavenSurefireVersion>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <src.dir>src/main/java</src.dir>
    <sonar.sources>pom.xml,src/main/java</sonar.sources>
    <mavenSurefireVersion>2.20</mavenSurefireVersion>
</properties>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<profiles>
    <profile>
        <id>sonarLocal</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <sonar.host.url>http://localhost:9000</sonar.host.url>
        </properties>
    </profile>
</profiles>

<build>
    <sourceDirectory>${src.dir}</sourceDirectory>
    <testResources>
        <testResource>
            <directory>${project.basedir}</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.source}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>validate</id>
                    <phase>validate</phase>
                    <configuration>
                        <includeTestSourceDirectory>true</includeTestSourceDirectory>
                        <configLocation>google_checks.xml</configLocation>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                        <failOnViolation>true</failOnViolation>
                        <violationSeverity>warning</violationSeverity>
                    </configuration>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${mavenSurefireVersion}</version>
        </plugin>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.4.0.905</version>
        </plugin>
    </plugins>
</build>

我的项目结构如下:

src/main/java --> 包含应用代码。

src/test/java --> 包含所有测试代码。

我使用命令“mvn clean install sonar:sonar -PsonarLocal”来执行Sonar。

现在我观察到 Sonar 从不分析 src/test/java 文件夹中的所有 .java 文件。

为了证明这一点,我在其中一个测试类中添加了一个未使用的变量,而 Sonar 没有捕捉到它。如果我在 src/main/java 文件夹中的任何文件中执行相同的操作,Sonar 会捕获它。

我有什么遗漏吗?

【问题讨论】:

    标签: java maven sonarqube sonarqube-scan


    【解决方案1】:

    您期望将“正常”规则应用于测试。今天情况并非如此。相反,仅应用特定于测试的规则的子集。尝试将测试标记为@Ignored。你应该会看到一个问题。

    在不进行其他分析的情况下进行仔细检查的一种方法是查看项目的代码页面。您应该会看到那里列出的测试文件,如果您深入其中,应该能够从文件查看器右上角的“汉堡菜单”图标访问特定于文件的指标。

    目前,要对源文件运行正常规则,您需要进行第二次单独的分析(请务必使用 -Dsonar.projectKey 输入不同的项目密钥,否则您将覆盖正常分析)并指定您的测试目录作为源 (sonar.sources=relative/path/to/tests)。

    【讨论】:

    • 你好 G. Ann,我在我的 Maven 项目中使用了 TestNG。当我尝试添加@Ignore 注解时,它显示需要导入import org.junit.Ignore;。这是正确的方法吗?如果没有,如何为 TestNG 测试添加忽略?
    • 请忽略我上面的评论。当我添加 @Ignore 时,我看到 Sonar 中出现错误错误。我收到错误消息“修复或删除此跳过的单元测试”,而不是显示未使用的变量。有什么方法可以对测试进行声纳分析?
    • 查看我的扩展@JohnSmith。
    • 好的,我也尝试实现它。我输入了命令mvn clean install sonar:sonar -PsonarLocal -Dsonar.projectKey=com.github.jitpack:maven-simple -Dsonar,sources=src/test/java/com/github/jitpack,它仍然没有扫描测试文件夹。我在这里做错了吗?
    • -Dsonar,sources-Dsonar.sources?如果是后者,可能是时候开始讨论Google Group@JohnSmith了。
    【解决方案2】:

    使用以下命令,您可以在同一个 Sonar 项目中分析src/main/java src/test/java 中的代码:

    1. 首先使用 JaCoCo 构建您的应用程序并存储其代码覆盖率:

    mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false

    1. 然后将结果上传到 Sonar:

    mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=&lt;a valid token&gt; -Dsonar.sources=src -Dsonar.test.inclusions=src/test/java/*

    请注意,-Dsonar.login=&lt;a valid token&gt; 属性仅在 http://localhost:9000(或任何主机 url)需要身份验证时才是必需的。替换为有效的令牌。

    然而,我建议在单独 Sonar 项目中扫描您的src/test/java 代码,因为在扫描应用程序代码旁边的测试代码时,代码覆盖率等指标会被丢弃(如Sonar 将报告所有测试类本身的 0% 覆盖率)。单独扫描:

    对于应用程序代码 (src/main/java),运行:

    mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=&lt;a valid token&gt;

    对于测试代码(src/test/java),运行:

    mvn sonar:sonar -Dsonar.projectKey=&lt;my project&gt;-tests -Dsonar.host.url=http://localhost:9000 -Dsonar.login=&lt;a valid token&gt; -Dsonar.sources=src/test -Dsonar.test.inclusions=src/test/java/*

    使用您的项目名称更新-Dsonar.projectKey=&lt;my project&gt;-tests,以便您拥有用于测试扫描的唯一项目密钥。

    对于这种方法,不需要对 pom.xml 进行任何特定于 Sonar/JaCoCo 的添加。

    请注意,每当您对代码进行更新时,必须在 mvn sonar:sonar... 之前执行带有 JaCoCo 的 mvn clean 以使这些更新反映在 Sonar 中。

    我使用 SonarQube 6.6 和 Sonar Scanner 3.0.3.778 对此进行了测试。

    【讨论】:

    • metrics like code coverage get thrown off when scanning test code alongside application code 感谢您的提示!
    猜你喜欢
    • 2017-08-19
    • 2019-07-19
    • 2017-04-27
    • 2018-02-05
    • 2019-11-05
    • 2020-01-10
    • 2021-01-14
    • 2019-03-09
    • 2018-03-14
    相关资源
    最近更新 更多