maven jacoco_使用JaCoCo Maven插件为单元和集成测试创建代码覆盖率报告
maven jacoco
當我開始使用Java 7時,我立即注意到Cobertura Maven插件不支持它 。 這對我來說是個大問題,因為我每天都使用代碼覆蓋率報告。 我做了一些研究,發現了JaCoCo代碼覆蓋庫 。 看起來很有趣,我決定試一試。
問題在于配置它確實很困難,并且花費了大量時間。 我閱讀了許多教程,只是發現其中給出的說明對我不起作用。 然后我遇到了這個博客文章 ,一切都準備就緒。
盡管該博客文章對我來說非常有價值,但還是有點含糊。 我覺得對JaCoCo Maven插件的用法進行更詳細的解釋很有價值。
這篇博客文章描述了我們如何使用JaCoCo Maven插件為單元測試和集成測試創建代碼覆蓋率報告。
我們的構建要求如下:
- 運行測試時,我們的構建必須為單元測試和集成測試創建代碼覆蓋率報告。
- 代碼覆蓋率報告必須在單獨的目錄中創建。 換句話說,必須將用于單元測試的代碼覆蓋率報告創建到與用于集成測試的代碼覆蓋率報告不同的目錄中。
讓我們開始吧。
注意 :這篇博客文章的示例應用程序基于我的博客文章“ Maven集成測試”的示例應用程序。 如果尚未閱讀,建議您在閱讀本博客文章之前先閱讀它。
配置JaCoCo Maven插件
我們使用JaCoCo Maven插件有兩個目的:
我們可以按照以下步驟配置JaCoCo Maven插件:
下面將更詳細地描述這些步驟。
將JaCoCo Maven插件添加到POM文件
通過將以下插件聲明添加到其“ 插件”部分,我們可以將JaCoCo Maven插件添加到我們的POM文件中:
<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.6.3.201306030806</version> </plugin>讓我們繼續研究如何為單元測試配置代碼覆蓋率報告。
配置單元測試的代碼覆蓋率報告
我們可以通過將兩個執行添加到插件聲明中來為單元測試配置代碼覆蓋率報告。 這些執行方式描述如下:
我們的插件配置的相關部分如下所示:
<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>讓我們找出如何為集成測試配置代碼覆蓋率報告。
配置集成測試的代碼覆蓋率報告
我們可以通過在插件聲明中添加兩個執行來為集成測試配置代碼覆蓋率報告。 這些執行方式描述如下:
我們的插件配置的相關部分如下所示:
<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>而已。 現在,我們已經配置了JaCoCo Maven插件。 下一步是配置Maven Surefire插件。 讓我們找出如何做到這一點。
配置Maven Surefire插件
我們使用Maven Surefire插件運行示例應用程序的單元測試。 因為我們要為單元測試創??建代碼覆蓋率報告,所以我們必須確保在運行單元測試時JaCoCo代理正在運行。 我們可以通過添加surefireArgLine屬性作為argLine配置參數的值的值確保這一點。
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插件。 讓我們找出如何做到這一點。
配置Maven故障安全插件
我們的示例應用程序的集成測試由Maven Failsafe插件運行。 因為我們要為集成測試創建代碼覆蓋率報告,所以我們必須確保在運行集成測試時JaCoCo代理正在運行。 我們可以通過將failsafeArgLine屬性的值添加為argLine配置參數的值來實現。
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>創建代碼覆蓋率報告
現在,我們已成功完成所需的配置。 讓我們看看如何為單元測試和集成測試創建代碼覆蓋率報告。
該博客文章的示例應用程序具有三個構建配置文件,下面對此進行了描述:
- 開發配置文件在開發過程中使用,它是我們構建的默認配置文件。 當此配置文件處于活動狀態時,僅運行單元測試。
- 集成測試概要文件用于運行集成測試。
- all-tests配置文件用于運行單元測試和集成測試。
我們可以通過在命令提示符處運行以下命令來創建不同的代碼覆蓋率報告:
- 命令mvn clean test運行單元測試,并為目錄target / site / jacoco-ut創建單元測試的代碼覆蓋率報告。
- 命令mvn clean verify -P integration-test運行集成測試,并創建用于集成測試的代碼覆蓋率報告到目錄target / site / jacoco-it 。
- 命令mvn clean verify -P all-tests運行單元測試和集成測試,并為單元測試和集成測試創建代碼覆蓋率報告。
今天就這些。 與往常一樣,此博客文章的示例應用程序可在Github上獲得 。
翻譯自: https://www.javacodegeeks.com/2013/08/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin.html
maven jacoco
總結
以上是生活随笔為你收集整理的maven jacoco_使用JaCoCo Maven插件为单元和集成测试创建代码覆盖率报告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑抓包工具(电脑抓包工具中文wires
- 下一篇: 无服务器:SLAppForge Sigm