我必須從不同的代碼庫執行業務測試時獲取應用程序的代碼覆蓋範圍。Jacoco測試覆蓋率報告顯示0%
我使用:Maven作爲我的構建 Jbehave作爲我的測試框架。 測試用java編寫。
我的應用程序是一組部署在tomcat上的war文件。
應用程序代碼庫與測試代碼庫分開。
在獲取覆蓋範圍之後,我按照以下步驟操作。
1使用maven編譯測試代碼。
2複印應用從它的構建($ {app.code.dir} /目標/班)爲$ {test.code.dir} /目標/類
[3]運行的地方類測試和jacoco基於Maven報告
的MVN編譯:我一直
<profile>
<id>coverage</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<phase>process-resources</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<skip>false</skip>
<destFile>${basedir}/target/jacoco-coverage.exec</destFile>
</configuration>
</execution>
<execution>
<id>default-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- <skip>true</skip> -->
<excludes>
<exclude>com/mytest/bdt/**</exclude><!-- test classes -->
<exclude>com/mytest/bdd/**</exclude><!-- test classes -->
</excludes>
<dataFile>${basedir}/target/jacoco-coverage.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
的jbehave測試使用執行:
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>${jbehave.core.version}</version>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>unpack-view-resources</id>
<phase>process-resources</phase>
<goals>
<goal>unpack-view-resources</goal>
</goals>
</execution>
<execution>
<id>embeddable-stories</id>
<phase>test</phase>
<configuration>
<includes>
<include>${embeddables5}</include><!-- TestSuite.java -->
</includes>
<excludes />
<ignoreFailureInStories>true</ignoreFailureInStories>
<ignoreFailureInView>true</ignoreFailureInView>
<threads>1</threads>
<metaFilters>
<metaFilter>${meta.filter}</metaFilter>
<metaFilter>-skip *</metaFilter>
<metaFilter>+run</metaFilter>
</metaFilters>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
</executions>
</plugin>
當我執行mvn mvn install -Pcoverage
執行過程如下。
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ test-bdd-testsuite ---
[INFO] --- jbehave-maven-plugin:3.7.5:unpack-view-resources (unpack-view-resources) @ test-bdd-testsuite ---
[INFO] --- jacoco-maven-plugin:0.6.3.201306030806:prepare-agent (default-prepare-agent) @ test-bdd-testsuite ---
[INFO] argLine set to -javaagent:/home/testUser/.m2/repository/org/jacoco/org.jacoco.agent/0.6.3.201306030806/org.jacoco.agent-0.6.3.201306030806-runtime.jar=destfile=/home/testUser/testProj/trunk/target/jacoco-coverage.exec
[INFO]
[INFO] --- maven-compiler-plugin:2.1:compile (default-compile) @ test-bdd-testsuite ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ test-bdd-testsuite ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/testUser/testProj/trunk/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.1:testCompile (default-testCompile) @ test-bdd-testsuite ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ test-bdd-testsuite ---
[INFO] No tests to run.
[INFO] Surefire report directory: /home/testUser/testProj/trunk/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- jbehave-maven-plugin:3.7.5:run-stories-as-embeddables (embeddable-stories) @ test-bdd-testsuite ---
[INFO] Running stories as embeddables using embedder Embedder[ .....
.....
.....
Test execution log comes here .......
.....
.....
[INFO] Reports view generated with 1 stories (of which 0 pending) containing 25 scenarios (of which 0 pending)
[INFO] Meta filters excluded 0 stories and 24 scenarios
[WARNING] Failures in reports view: 0 scenarios failed
[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ test-bdd-testsuite ---
[INFO] Building jar: /home/testUser/testProj/trunk/target/test-bdd-testsuite-1.0.jar
[INFO]
[INFO] --- jacoco-maven-plugin:0.6.3.201306030806:report (default-report) @ test-bdd-testsuite ---
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ test-bdd-testsuite ---
[INFO] Installing /home/testUser/testProj/trunk/target/test-bdd-testsuite-1.0.jar to /home/testUser/.m2/repository/com/testCode/bdd/test-bdd-testsuite/1.0/test-bdd-testsuite-1.0.jar
[INFO] Installing /home/testUser/testProj/trunk/pom.xml to /home/testUser/.m2/repository/com/testCode/bdd/test-bdd-testsuite/1.0/test-bdd-testsuite-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.134s
[INFO] Finished at: Tue Nov 26 19:18:28 IST 2013
[INFO] Final Memory: 14M/309M
[INFO] ------------------------------------------------------------------------
用這個,我得到一個覆蓋報告,與應用程序包一起生成。但覆蓋率顯示爲0%
在會話鏈接中,應用程序類未加載。 屏幕截圖:
有人可以幫我嗎?
我能夠解決這個問題如下。 – Asanke
[1]將應用程序複製到instrmentation floder中。 [2]使用java參數「-javaagent:$ WORKSPACE/target/lib/jacoco-agent-0.6.3.2.jar = includes = *,destfile = $ TOMCAT_HOME/jacoco-coverage」啓動應用服務器(mycat中的tomcat)。 exec,append = false「(我把jacoco-agent jar複製到我的項目中) [3]執行測試(這可以是自動的或手動的) [4]停止tomcat servere(jacoco-coverage .exec在此處更新) [5]執行ant報告目標。點更新的jacoco-coverage.exec和複製的應用程序類文件夾。 – Asanke
參考: http://car-online.fr/en/blog/fabien_duchene/2013-05-03-Java%20Code%20Coverage%20in%20Tomcat%20JSP%20applications,%20e.g.,%20WebGoat% 20with%20Jacoco/ – Asanke