即使生成代码覆盖率,Sonar也不会获取单元测试结果

问题描述 投票:3回答:2

我在我的本地机器上运行SonarQuebe 6.2,我有Spring Boot Java 8项目,带有书面单元测试,我想上传到Sonar进行静态分析以及代码覆盖。 生成代码覆盖率 - 我有我的JaCoCo HTML报告,生成了JUnit XML测试文件,但我的Sonar似乎错过了单元测试结果,甚至认为代码覆盖率已经显示: pom.xml中:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.6</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.6.6</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.facebook4j</groupId>
        <artifactId>facebook4j-core</artifactId>
        <version>2.4.8</version>
    </dependency>

    <dependency>
        <groupId>net.sf.dozer</groupId>
        <artifactId>dozer</artifactId>
        <version>5.5.1</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.4</version>
    </dependency>

</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    </execution>
                 <execution>
                    <id>generate-code-coverage-report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>  

我的sonar-project.properties:

sonar.projectKey=org.eventizer:EventizerServer
sonar.projectName=EventizerServer
sonar.projectVersion=1.0

sonar.log.level=DEBUG

sonar.sources=src/main/

sonar.language=java
sonar.java.source=1.8

sonar.sourceEncoding=UTF-8

sonar.java.binaries=target/classes/org/eventizer/eventizerserver/
sonar.java.test.binaries=target/test-classes/org/eventizer/eventizerserver/
sonar.tests=src/test/

sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPaths=target/jacoco.exec
sonar.junit.reportPaths=target/surefire-reports/  

我正在运行这个mvn命令: mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent test -Dmaven.test.failure.ignore = true 结果我得到了以下输出的目标目录: Target output directory

sonar.java.binaries设置的类目录: enter image description here

sonar.java.test.binaries设置的测试类目录: enter image description here

Surefire JUnit测试报告为sonar.junit.reportPaths设置的目录: enter image description here

JaCoCo报告输出目录: enter image description here

浏览器中的JaCoCo HTML报告: qazxsw poi

之后我运行qazxsw poi,低于一些重要的(我认为是)输出: qazxsw poi qazxsw poi

我的Sonar Web实例项目分析:enter image description here

而且我真的不知道为什么会发生这种情况,因为看起来所有内容都已正确生成。从昨天开始,我想我已经尝试了StackOverflow上的所有内容,所以请不要将其标记为重复。 这甚至更奇怪,因为当我访问此项目的Coverage指标时,我可以看到100%单元测试通过: sonar-scanner.bat

maven sonarqube jacoco jacoco-maven-plugin
2个回答
0
投票

好吧......我想,那可能是因为enter image description here ...如果你把它设置为enter image description here,它会再次出现。

并且,我发现有一个enter image description hereenter image description here将在声纳显示junit报告。

这是我的sonar-project.properties:

sonar.sources=src/main/

而build.gradle:

sonar.sources=src

建立:

Sonar Parater

0
投票

好的,所以我发现了可能是Sonar bug的东西。

基本上,这个项目已经被推向Sonar很长一段时间,基本的sonar.tests=src/test配置。所以,它甚至没有做测试结果。现在我想通过詹金斯运行,所以我在sonar.projectKey=com.test.marslo:marslo-test sonar.projectName=marslo-test sonar.projectVersion=1.1.0 onar.projectBaseDir=. sonar.sources=src/main sonar.tests=src/test sonar.java.binaries=build sonar.sourceEncoding=UTF-8 sonar.java.source=1.8 sonar.jacoco.reportPaths=./build/jacoco/test.exec sonar.junit.reportPaths=./build/test-results/test 填充了所有必要的字段,并通过... apply plugin: "jacoco" ... ... jacoco { toolVersion = "0.8.0" } jacocoTestReport { reports { xml.enabled true csv.enabled true html.enabled true } } test { jacoco { append = false destinationFile = file("$buildDir/jacoco/jacocoTest.exec") classDumpDir = file("$buildDir/jacoco/classpathdumps") } } 而不是gradle clean build test jacocoTestReport 推送。

在您这样做之后,单元测试质量门失败了,没有充分的理由。因为在我最新的屏幕截图中,您可以看到实际上单元测试通过了100.0%。

我决定通过将mvn sonar:sonar属性更改为其他内容来将该分析推送到单独的项目中,所有突然一切都顺利进行。

© www.soinside.com 2019 - 2024. All rights reserved.