Cobertura和EMMA插件實際上都是設計用于單元測試的。 因此,我們必須解決該限制。
- 首先,我們需要對類進行檢測。
- 其次,我們需要打包檢測的類,并在以后的構建中使用它們。
- 需要告訴集成測試為其使用依賴的檢測類。
- 生成結果的XML報告。
我嘗試過這樣做,但又不依賴于ant,但是每次我嘗試使用maven-site-plugin并將其配置為生成報告時,都會抱怨cobertura:check沒有正確配置。 在我們的情況下,我不需要運行檢查,只需要生成的報告即可。 于是Ant和AntContrib得以營救。 以下是我想出的完整的Maven個人資料:
<profile><id>cobertura</id><dependencies><dependency><groupId>net.sourceforge.cobertura</groupId><artifactId>cobertura</artifactId><optional>true</optional><version>1.9.4.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>cobertura-maven-plugin</artifactId><configuration><instrumentation><excludes><exclude>org/ebayopensource/turmeric/test/**/*.class</exclude><exclude>org/ebayopensource/turmeric/common/v1/**/*.class</exclude></excludes></instrumentation></configuration><executions><execution><id>cobertura-instrument</id><phase>process-classes</phase><goals><goal>instrument</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><executions><execution><id>cobertura-jar</id><phase>post-integration-test</phase><goals><goal>jar</goal></goals><configuration><classifier>cobertura</classifier><classesDirectory>${basedir}/target/generated-classes/cobertura</classesDirectory></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-install-plugin</artifactId><version>2.3.1</version><executions><execution><id>cobertura-install</id><phase>install</phase><goals><goal>install</goal></goals><configuration><classifier>cobertura</classifier></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><executions><execution><phase>verify</phase><configuration><tasks><taskdef classpathref='maven.runtime.classpath'resource='tasks.properties' /><taskdef classpathref='maven.runtime.classpath'resource='net/sf/antcontrib/antcontrib.properties' /><availablefile='${project.build.directory}/cobertura/cobertura.ser'property='ser.file.exists' /><if><equals arg1='${ser.file.exists}'arg2='true' /><then><echo message='Executing cobertura report' /><mkdirdir='${project.build.directory}/site/cobertura' /><cobertura-reportformat='xml'destdir='${project.build.directory}/site/cobertura'datafile='${project.build.directory}/cobertura/cobertura.ser' /></then><else><echo message='No SER file found.' /></else></if></tasks></configuration><goals><goal>run</goal></goals></execution></executions><dependencies><dependency><groupId>ant-contrib</groupId><artifactId>ant-contrib</artifactId><version>20020829</version></dependency></dependencies></plugin></plugins></build></profile>
注意:請勿在此配置文件中使用cobertura:cobertura目標。 由于它將嘗試對類進行兩次檢測,因此構建將失敗。
必須使用Ant和AntContrib,因為沒有cobertura:report目標,因為它希望在網站生成階段運行。 但是,這會導致檢查目標也可以運行,而我們則不需要。 因此,也許,我將制作一個補丁以添加報告目標,以便僅運行報告而不必運行站點目標。 希望這對某些人有幫助,因為我為此花費了很多時間。
祝您編程愉快,別忘了分享!
參考: Intelligent Cramps博客上的JCG合作伙伴 David Carver 使用Cobertura和Maven為集成和單元測試啟用代碼覆蓋率 。
翻譯自: https://www.javacodegeeks.com/2012/09/cobertura-and-maven-code-coverage-for.html