一、引言
1、java单元测试框架
junit
testNG
Spock
2、可以用来生成覆盖率报告的插件
Jacoco
Cobertura
3、sonarqube上的单元测试覆盖率
SonarQube 不会运行测试或生成报告。要在分析中包含覆盖结果,需要设置第三方覆盖工具来生成报告并配置 SonarQube 以导入这些报告。
生成单元测试覆盖率需要按照以下步骤操作:
SonarQube 使用导入的覆盖率报告中的覆盖行和可执行行(或要覆盖的行)来计算其覆盖率指标。 SonarQube 计算覆盖率如下:
文章来源:https://www.toymoban.com/news/detail-430830.html
二、Jacoco
官方参考 https://community.sonarsource.com/t/coverage-test-data-importing-jacoco-coverage-report-in-xml-format/12151文章来源地址https://www.toymoban.com/news/detail-430830.html
1、 junit 框架
- 代码结构
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>java-maven-junit-helloworld</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- This configures the compiler to produce Java 8 class files. -->
<!-- The minimum JDK version installed is 8 of course, but newer JDK releases should work too. -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<junit.jupiter.version>5.2.0</junit.jupiter.version>
<junit.platform.version>1.2.0</junit.platform.version>
<hamcrest.version>1.3</hamcrest.version>
<mockito.version>2.21.0</mockito.version>
<!-- This configures the jacoco and sonarqube. -->
<jacoco.plugin.version>0.8.1</jacoco.plugin.version>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.language>java</sonar.language>
</properties>
<dependencies>
<!-- Testing dependencies. -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
</dependency>
<!-- Jacoco dependencies. -->
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Configures the compiler. -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>-Xlint</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<!-- Unit tests are run by surefire. -->
<!-- Classes under src/test/java called *Test are included automatically. --&g
到了这里,关于Sonarqube-8.9版本测试单元测试覆盖率的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!