一、引言
1、java單元測(cè)試框架
junit
testNG
Spock
2、可以用來(lái)生成覆蓋率報(bào)告的插件
Jacoco
Cobertura
3、sonarqube上的單元測(cè)試覆蓋率
SonarQube 不會(huì)運(yùn)行測(cè)試或生成報(bào)告。要在分析中包含覆蓋結(jié)果,需要設(shè)置第三方覆蓋工具來(lái)生成報(bào)告并配置 SonarQube 以導(dǎo)入這些報(bào)告。
生成單元測(cè)試覆蓋率需要按照以下步驟操作:
SonarQube 使用導(dǎo)入的覆蓋率報(bào)告中的覆蓋行和可執(zhí)行行(或要覆蓋的行)來(lái)計(jì)算其覆蓋率指標(biāo)。 SonarQube 計(jì)算覆蓋率如下:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-430830.html
二、Jacoco
官方參考 https://community.sonarsource.com/t/coverage-test-data-importing-jacoco-coverage-report-in-xml-format/12151文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-430830.html
1、 junit 框架
- 代碼結(jié)構(gòu)
- 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
到了這里,關(guān)于Sonarqube-8.9版本測(cè)試單元測(cè)試覆蓋率的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!