關(guān)于SpringBoot單元測(cè)試(cobertura生成覆蓋率報(bào)告)_第1頁(yè)
關(guān)于SpringBoot單元測(cè)試(cobertura生成覆蓋率報(bào)告)_第2頁(yè)
關(guān)于SpringBoot單元測(cè)試(cobertura生成覆蓋率報(bào)告)_第3頁(yè)
關(guān)于SpringBoot單元測(cè)試(cobertura生成覆蓋率報(bào)告)_第4頁(yè)
關(guān)于SpringBoot單元測(cè)試(cobertura生成覆蓋率報(bào)告)_第5頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第關(guān)于SpringBoot單元測(cè)試(cobertura生成覆蓋率報(bào)告)目錄demo(SpringBoot項(xiàng)目)覆蓋率測(cè)試報(bào)告生成(cobertura)cobertura原理1.instrument2.執(zhí)行測(cè)試3.生成報(bào)告SpringBootpom.xml配置命令介紹maven-surefire-plugin使用說(shuō)明1.跳過(guò)測(cè)試2.動(dòng)態(tài)指定要運(yùn)行的測(cè)試用例3.包含與排除測(cè)試用例

demo(SpringBoot項(xiàng)目)

被測(cè)試類:

importorg.springframework.stereotype.Service;

@Service

publicclassTestService{

publicStringsayHi(){

return"hi";

publicintdivide(inta,intb){

returna/b;

}

測(cè)試代碼:

importstaticorg.junit.Assert.*;

importorg.junit.Test;

importorg.junit.runner.RunWith;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.boot.test.context.SpringBootTest;

importorg.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTest

publicclassTestServiceTest{

@Autowired

TestServicetestService;

@Test

publicvoidtestSayHi(){

TestServicetestService=newTestService();

Stringresult=testService.sayHi();

assertEquals("hi",result);

@Test

publicvoidtestDivide(){

TestServicetestService=newTestService();

intresult=testService.divide(3,6);

assertTrue(result-1);

pom.xml配置文件

![cobertura](C:\Users\jiaflu\Desktop\cobertura.PNG)xmlversion="1.0"encoding="UTF-8"

projectxmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"

modelVersion4.0.0/modelVersion

parent

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-parent/artifactId

version2.1.5.RELEASE/version

relativePath/!--lookupparentfromrepository--

/parent

groupIdcom.jiaflu/groupId

artifactIdlearn_springoot/artifactId

version0.0.1-SNAPSHOT/version

namelearn_springoot/name

descriptionDemoprojectforSpringBoot/description

properties

java.version1.8/java.version

jackson.version2.9.8/jackson.version

/properties

dependencies

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter/artifactId

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-test/artifactId

scopetest/scope

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

/dependency

/dependencies

build

plugins

plugin

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-maven-plugin/artifactId

/plugin

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-compiler-plugin/artifactId

configuration

source1.8/source

target1.8/target

/configuration

/plugin

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-surefire-plugin/artifactId

version2.5/version

/plugin

plugin

groupIdorg.codehaus.mojo/groupId

artifactIdcobertura-maven-plugin/artifactId

version2.5.2/version

configuration

encodingUTF-8/encoding

formats

formathtml/format

formatxml/format

/formats

/configuration

/plugin

/plugins

/build

/project

運(yùn)行mvncobertura:cobertura查看截圖:

覆蓋率測(cè)試報(bào)告生成(cobertura)

cobertura原理

cobertura執(zhí)行過(guò)程大致如下:

使用instrument修改我們編譯后的class文件,位于target\generated-classes。

執(zhí)行測(cè)試,測(cè)試數(shù)據(jù)輸出到xxx.ser中,位于target\cobertura\cobertura.ser。

使用report生成覆蓋率報(bào)告。

1.instrument

instrument:cobertura使用instrument修改我們編譯后的class文件,在代碼里面加入cobertura的統(tǒng)計(jì)代碼。并生成一個(gè).ser文件(用于覆蓋率數(shù)據(jù)的輸出)。

使用instrument執(zhí)行的過(guò)程中,CoberturaInstrumenter會(huì)首先調(diào)用分析監(jiān)聽(tīng)器分析給定的編譯好的.class,獲得touchPoint(可以認(rèn)為對(duì)應(yīng)于源代碼中的待覆蓋行)以及需要的其他信息。然后調(diào)用注入監(jiān)聽(tīng)器將信息注入到新的.class中,保存到\target\generated-classes目錄下。

示例:

//Sourcecoderecreatedfroma.classfilebyIntelliJIDEA

//(poweredbyFernflowerdecompiler)

packagecom.cisco.webex.cmse.soa.soaservice.service;

importnet.sourceforge.cobertura.coveragedata.HasBeenInstrumented;

importnet.sourceforge.cobertura.coveragedata.TouchCollector;

importorg.slf4j.Logger;

importorg.slf4j.LoggerFactory;

importorg.springframework.beans.factory.annotation.Value;

importorg.springframework.context.annotation.PropertySource;

importorg.springframework.stereotype.Service;

@PropertySource({"classpath:perties"})

@Service

publicclassPropertyServiceimplementsHasBeenInstrumented{

privatestaticfinalLoggerlogger;

@Value("${cdp.instance.url}")

privateStringcdpInstanUrl;

@Value("${soa.instance.url}")

privateStringsoaInstanceUrl;

@Value("${github.api.token}")

publicStringgitApiToken;

@Value("${github.instance.url}")

privateStringgithubInstance;

@Value("${github.repo.template.owner}")

privateStringtplRepoOwner;

@Value("${github.repo.consul.owner}")

privateStringconsulRepoOwner;

@Value("${}")

privateStringslmListenQueue;

publicPropertyService(){

booleanvar1=false;

int__cobertura__branch__number__=true;

TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService",12);

super();

publicStringgetCdpInstanUrl(){

booleanvar1=false;

int__cobertura__branch__number__=true;

TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService",33);

returnthis.cdpInstanUrl;

publicvoidsetSlmListenQueue(String()V){

booleanvar2=false;

int__cobertura__branch__number__=true;

TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService",85);

this.slmListenQueue=slmListenQueue;

TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService",86);

static{

booleanvar0=false;

booleanvar1=true;

TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService",13);

logger=LoggerFactory.getLogger(PropertyService.class);

2.執(zhí)行測(cè)試

在執(zhí)行測(cè)試用例時(shí),引用cobertura修改過(guò)的.class,測(cè)試信息寫入到cobertura.ser檔案文件。

3.生成報(bào)告

從cobertura.ser獲取覆蓋率數(shù)據(jù),然后結(jié)合src中提供的源代碼,生成最終的覆蓋率報(bào)告,放到了target\site\cobertura路徑下。若配置了生成html格式的報(bào)告,可以通過(guò)index.html查看覆蓋率測(cè)試報(bào)告。

SpringBootpom.xml配置

添加如下依賴:

plugin

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-maven-plugin/artifactId

/plugin

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-compiler-plugin/artifactId

configuration

source1.8/source

target1.8/target

/configuration

/plugin

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-surefire-plugin/artifactId

version2.5/version

configuration

!--此參數(shù)用于解決一個(gè)坑,下面會(huì)說(shuō)明--

argLine-noverify/argLine

/configuration

/plugin

plugin

groupIdorg.codehaus.mojo/groupId

artifactIdcobertura-maven-plugin/artifactId

version2.5.2/version

configuration

formats

formatxml/format

formathtml/format

/formats

/configuration

/plugin

采坑:

在使用mvncobertura:cobertura命令生成測(cè)試覆蓋率報(bào)告時(shí),出現(xiàn)了如下問(wèn)題(截取部分,報(bào)錯(cuò)原因如下):

Reason:

Expectedstackmapframeatthislocation.

Bytecode:

0x0000000:2ab4001b2bb9002e0200c6002f2ab400

0x0000010:1b2bb9002e0200c00030b60034c6001c

解決方法:

本人使用的是jdk1.8,添加jvm參數(shù)-noverify,可以在pom文件中添加配置,配置見(jiàn)上述pom.xml

網(wǎng)上查資料jdk1.7添加jvm參數(shù)-XX:-UseSplitVerifier,(1.8沒(méi)有-XX:-UseSplitVerifier這參數(shù))

命令介紹

cobertura:check

根據(jù)最新的源碼標(biāo)記(生成的class文件)校驗(yàn)測(cè)試用例的覆蓋率,如果沒(méi)有達(dá)到要求,則執(zhí)行失敗.

cobertura:check-integration-test

這個(gè)命令和cobertura:check功能是一樣的,區(qū)別是二者綁定的maven生命周期不一樣.cobertura:check綁定了test,cobertura:check-integration-test綁定了verify.再說(shuō)的明白些,maven生命周期中有一個(gè)是test跑得單元測(cè)試,還有一個(gè)是integration-test跑的集成測(cè)試.而verify前就是integration-test.即cobertura:check-integration-test比cobertura:check涵蓋的測(cè)試用例更多.

cobertura:clean

這個(gè)好理解,就是清理掉目錄/target/cobertura/中得文件.目前發(fā)現(xiàn)里面就一個(gè)文件cobertura.ser.

cobertura:cobertura

這個(gè)插件的關(guān)鍵命令.標(biāo)記被編譯的文件,運(yùn)行單元測(cè)試,生成測(cè)試報(bào)告.

cobertura:cobertura-integration-test

和cobertura:cobertura做了一樣的事情,區(qū)別是包含了集成測(cè)試用例.

cobertura:dump-datafile

在命令行輸出覆蓋率數(shù)據(jù).數(shù)據(jù)依據(jù)是生成的class文件.這個(gè)命令我沒(méi)搞懂他的意義何在.在后面一個(gè)有趣的實(shí)驗(yàn)我們會(huì)用這個(gè)命令來(lái)更好的理解cobertura-maven-plugin.

cobertura:help

cobertura:instrument

標(biāo)記被編譯的class文件.執(zhí)行這個(gè)命令會(huì)在目錄/target/generated-classes/cobertura下生成一套class文件.

maven-surefire-plugin使用說(shuō)明

Maven本身并不是一個(gè)單元測(cè)試框架,它只是在構(gòu)建執(zhí)行到特定生命周期階段的時(shí)候,通過(guò)插件來(lái)執(zhí)行JUnit或者TestNG的測(cè)試用例。這個(gè)插件就是maven-surefire-plugin,也可以稱為測(cè)試運(yùn)行器(TestRunner),它能兼容JUnit3、JUnit4以及TestNG。

在默認(rèn)情況下,maven-surefire-plugin的test目標(biāo)會(huì)自動(dòng)執(zhí)行測(cè)試源碼路徑(默認(rèn)為src/test/java/)下所有符合一組命名模式的測(cè)試類。這組模式為:

*/Test.java:任何子目錄下所有命名以Test開(kāi)關(guān)的Java類。

*/Test.java:任何子目錄下所有命名以Test結(jié)尾的Java類。

*/TestCase.java:任何子目錄下所有命名以TestCase結(jié)尾的Java類。

maven-surefire-plugin插件應(yīng)用:

1.跳過(guò)測(cè)試

跳過(guò)測(cè)試運(yùn)行mvnpackage-DskipTests

或者通過(guò)pom提供該屬性:

plugin

groupIdorg.apache.maven.plugins/groupId

artifactIdmaven-surefire-plugin/artifactId

version2.5/version

configuration

skipTeststrue/skipTests

/configuration

/plugin

跳過(guò)測(cè)試代碼的編譯mvnpackage

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論