maven jacoco_使用JaCoCo Maven插件为单元和集成测试创建代码覆盖率报告
maven jacoco
當(dāng)我開始使用Java 7時(shí),我立即注意到Cobertura Maven插件不支持它 。 這對(duì)我來說是個(gè)大問題,因?yàn)槲颐刻於际褂么a覆蓋率報(bào)告。 我做了一些研究,發(fā)現(xiàn)了JaCoCo代碼覆蓋庫 。 看起來很有趣,我決定試一試。
問題在于配置它確實(shí)很困難,并且花費(fèi)了大量時(shí)間。 我閱讀了許多教程,只是發(fā)現(xiàn)其中給出的說明對(duì)我不起作用。 然后我遇到了這個(gè)博客文章 ,一切都準(zhǔn)備就緒。
盡管該博客文章對(duì)我來說非常有價(jià)值,但還是有點(diǎn)含糊。 我覺得對(duì)JaCoCo Maven插件的用法進(jìn)行更詳細(xì)的解釋很有價(jià)值。
這篇博客文章描述了我們?nèi)绾问褂肑aCoCo Maven插件為單元測(cè)試和集成測(cè)試創(chuàng)建代碼覆蓋率報(bào)告。
我們的構(gòu)建要求如下:
- 運(yùn)行測(cè)試時(shí),我們的構(gòu)建必須為單元測(cè)試和集成測(cè)試創(chuàng)建代碼覆蓋率報(bào)告。
- 代碼覆蓋率報(bào)告必須在單獨(dú)的目錄中創(chuàng)建。 換句話說,必須將用于單元測(cè)試的代碼覆蓋率報(bào)告創(chuàng)建到與用于集成測(cè)試的代碼覆蓋率報(bào)告不同的目錄中。
讓我們開始吧。
注意 :這篇博客文章的示例應(yīng)用程序基于我的博客文章“ Maven集成測(cè)試”的示例應(yīng)用程序。 如果尚未閱讀,建議您在閱讀本博客文章之前先閱讀它。
配置JaCoCo Maven插件
我們使用JaCoCo Maven插件有兩個(gè)目的:
我們可以按照以下步驟配置JaCoCo Maven插件:
下面將更詳細(xì)地描述這些步驟。
將JaCoCo Maven插件添加到POM文件
通過將以下插件聲明添加到其“ 插件”部分,我們可以將JaCoCo Maven插件添加到我們的POM文件中:
<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.6.3.201306030806</version> </plugin>讓我們繼續(xù)研究如何為單元測(cè)試配置代碼覆蓋率報(bào)告。
配置單元測(cè)試的代碼覆蓋率報(bào)告
我們可以通過將兩個(gè)執(zhí)行添加到插件聲明中來為單元測(cè)試配置代碼覆蓋率報(bào)告。 這些執(zhí)行方式描述如下:
我們的插件配置的相關(guān)部分如下所示:
<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.6.3.201306030806</version><executions><!--Prepares the property pointing to the JaCoCo runtime agent whichis passed as VM argument when Maven the Surefire plugin is executed.--><execution><id>pre-unit-test</id><goals><goal>prepare-agent</goal></goals><configuration><!-- Sets the path to the file which contains the execution data. --><destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile><!--Sets the name of the property containing the settingsfor JaCoCo runtime agent.--><propertyName>surefireArgLine</propertyName></configuration></execution><!--Ensures that the code coverage report for unit tests is created afterunit tests have been run.--><execution><id>post-unit-test</id><phase>test</phase><goals><goal>report</goal></goals><configuration><!-- Sets the path to the file which contains the execution data. --><dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile><!-- Sets the output directory for the code coverage report. --><outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory></configuration></execution></executions> </plugin>讓我們找出如何為集成測(cè)試配置代碼覆蓋率報(bào)告。
配置集成測(cè)試的代碼覆蓋率報(bào)告
我們可以通過在插件聲明中添加兩個(gè)執(zhí)行來為集成測(cè)試配置代碼覆蓋率報(bào)告。 這些執(zhí)行方式描述如下:
我們的插件配置的相關(guān)部分如下所示:
<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.6.3.201306030806</version><executions><!-- The Executions required by unit tests are omitted. --><!--Prepares the property pointing to the JaCoCo runtime agent whichis passed as VM argument when Maven the Failsafe plugin is executed.--><execution><id>pre-integration-test</id><phase>pre-integration-test</phase><goals><goal>prepare-agent</goal></goals><configuration><!-- Sets the path to the file which contains the execution data. --><destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile><!--Sets the name of the property containing the settingsfor JaCoCo runtime agent.--><propertyName>failsafeArgLine</propertyName></configuration></execution><!--Ensures that the code coverage report for integration tests afterintegration tests have been run.--><execution><id>post-integration-test</id><phase>post-integration-test</phase><goals><goal>report</goal></goals><configuration><!-- Sets the path to the file which contains the execution data. --><dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile><!-- Sets the output directory for the code coverage report. --><outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory></configuration></execution></executions> </plugin>而已。 現(xiàn)在,我們已經(jīng)配置了JaCoCo Maven插件。 下一步是配置Maven Surefire插件。 讓我們找出如何做到這一點(diǎn)。
配置Maven Surefire插件
我們使用Maven Surefire插件運(yùn)行示例應(yīng)用程序的單元測(cè)試。 因?yàn)槲覀円獮閱卧獪y(cè)試創(chuàng)??建代碼覆蓋率報(bào)告,所以我們必須確保在運(yùn)行單元測(cè)試時(shí)JaCoCo代理正在運(yùn)行。 我們可以通過添加surefireArgLine屬性作為argLine配置參數(shù)的值的值確保這一點(diǎn)。
Maven Surefire插件的配置如下所示(突出顯示了所需的更改):
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.15</version><configuration><!-- Sets the VM argument line used when unit tests are run. --><argLine>${surefireArgLine}</argLine><!-- Skips unit tests if the value of skip.unit.tests property is true --><skipTests>${skip.unit.tests}</skipTests><!-- Excludes integration tests when unit tests are run. --><excludes><exclude>**/IT*.java</exclude></excludes></configuration> </plugin>我們快完成了。 剩下要做的就是配置Maven Failsafe插件。 讓我們找出如何做到這一點(diǎn)。
配置Maven故障安全插件
我們的示例應(yīng)用程序的集成測(cè)試由Maven Failsafe插件運(yùn)行。 因?yàn)槲覀円獮榧蓽y(cè)試創(chuàng)建代碼覆蓋率報(bào)告,所以我們必須確保在運(yùn)行集成測(cè)試時(shí)JaCoCo代理正在運(yùn)行。 我們可以通過將failsafeArgLine屬性的值添加為argLine配置參數(shù)的值來實(shí)現(xiàn)。
Maven Failsafe插件的配置如下所示(突出顯示了所需的更改):
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-failsafe-plugin</artifactId><version>2.15</version><executions><!--Ensures that both integration-test and verify goals of the Failsafe Mavenplugin are executed.--><execution><id>integration-tests</id><goals><goal>integration-test</goal><goal>verify</goal></goals><configuration><!-- Sets the VM argument line used when integration tests are run. --><argLine>${failsafeArgLine}</argLine><!--Skips integration tests if the value of skip.integration.tests propertyis true--><skipTests>${skip.integration.tests}</skipTests></configuration></execution></executions> </plugin>創(chuàng)建代碼覆蓋率報(bào)告
現(xiàn)在,我們已成功完成所需的配置。 讓我們看看如何為單元測(cè)試和集成測(cè)試創(chuàng)建代碼覆蓋率報(bào)告。
該博客文章的示例應(yīng)用程序具有三個(gè)構(gòu)建配置文件,下面對(duì)此進(jìn)行了描述:
- 開發(fā)配置文件在開發(fā)過程中使用,它是我們構(gòu)建的默認(rèn)配置文件。 當(dāng)此配置文件處于活動(dòng)狀態(tài)時(shí),僅運(yùn)行單元測(cè)試。
- 集成測(cè)試概要文件用于運(yùn)行集成測(cè)試。
- all-tests配置文件用于運(yùn)行單元測(cè)試和集成測(cè)試。
我們可以通過在命令提示符處運(yùn)行以下命令來創(chuàng)建不同的代碼覆蓋率報(bào)告:
- 命令mvn clean test運(yùn)行單元測(cè)試,并為目錄target / site / jacoco-ut創(chuàng)建單元測(cè)試的代碼覆蓋率報(bào)告。
- 命令mvn clean verify -P integration-test運(yùn)行集成測(cè)試,并創(chuàng)建用于集成測(cè)試的代碼覆蓋率報(bào)告到目錄target / site / jacoco-it 。
- 命令mvn clean verify -P all-tests運(yùn)行單元測(cè)試和集成測(cè)試,并為單元測(cè)試和集成測(cè)試創(chuàng)建代碼覆蓋率報(bào)告。
今天就這些。 與往常一樣,此博客文章的示例應(yīng)用程序可在Github上獲得 。
翻譯自: https://www.javacodegeeks.com/2013/08/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin.html
maven jacoco
總結(jié)
以上是生活随笔為你收集整理的maven jacoco_使用JaCoCo Maven插件为单元和集成测试创建代码覆盖率报告的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑抓包工具(电脑抓包工具中文wires
- 下一篇: phpnow服务器安装步骤图解