gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:47:52 31 4
gpt4 key购买 nike

我在我的本地机器上运行 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

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

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

浏览器中的 JaCoCo HTML 报告:
enter image description here

之后我正在运行 sonar-scanner.bat ,在一些重要的(我是这样认为的)输出之下:
enter image description here
enter image description here

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

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

最佳答案

嗯...我想,这可能是因为 sonar.sources=src/main/ ... 如果您将其设置为 sonar.sources=src ,它会再次显示。

而且,我只是发现有一个 Sonar Parater :sonar.tests=src/test将在 Sonar 中显示junit报告。

这是我的 sonar-project.properties:

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

和 build.gradle:
...
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

关于maven - 即使生成了代码覆盖率, Sonar 也不会获取单元测试结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117714/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com