17

我正在用MaCo與JaCoCo插件生成故障安全和surefire報告,但我只能設法將它們放入單獨的報告中。我希望有一個全面的覆蓋視圖(在單元測試和集成測試之間合併)。與JaCoCo合併集成和單元測試報告

之後我認爲是一個徹底的谷歌搜索,我只能找到一種方法來做到這一點與聲納。有沒有更簡單的方法來做到這一點?

相關問題:Maven separate Unit Test and Integration Tests

回答

8

你應該看看到JaCoCo Maven插件包含一個merge目標的文檔。

+0

很好的答案!隨意看下面我的代碼示例。 –

13

不是你要找的答案,但仍...

在大多數情況下,你不應該從單元測試和集成測試合併範圍。

單元測試的價值在於它們改進了應用程序的設計並確保代碼的角落案例正常工作。你應該試着對你的單元測試進行高分支覆蓋。

您的集成測試的價值在於它們確保您的應用程序的主要使用情況正常運行,並且整個堆棧已正確集成。您應該嘗試爲您的集成測試提供較高的功能覆蓋率。 (但用一種工具來衡量功能覆蓋是相當困難的)。

如果您需要集成測試來改善您的分支機構覆蓋範圍,那麼這就表明您應該檢查代碼的設計。如果您的分支機構覆蓋率已經很高,但沒有進行集成測試,那麼添加它們不應該明顯改變您的指標。

隨意上下投票這個答案,因爲它是一個有點話題和相當自以爲是......

+0

雖然仍然有趣的一點! – Jpnh

11

最近我實現了這個:有些頭疼和大量的測試之後,我有一個精美的作品的配置。

    <plugin> 
        <groupId>org.jacoco</groupId> 
        <artifactId>jacoco-maven-plugin</artifactId> 
        <version>${jacoco.version}</version> 
        <executions> 
         <execution> 
          <id>pre-unit-test</id> 
          <goals> 
           <goal>prepare-agent</goal> 
          </goals> 
          <configuration> 
           <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile> 
           <propertyName>surefireArgLine</propertyName> 
          </configuration> 
         </execution> 
         <execution> 
          <id>pre-integration-test</id> 
          <goals> 
           <goal>prepare-agent-integration</goal> 
          </goals> 
          <configuration> 
           <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile> 
           <propertyName>testArgLine</propertyName> 
          </configuration> 
         </execution> 

         <execution> 
          <id>post-integration-test</id> 
          <phase>post-integration-test</phase> 
          <goals> 
           <goal>report</goal> 
          </goals> 
          <configuration> 
           <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile> 
           <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory> 
          </configuration> 
         </execution> 
         <execution> 
          <id>post-unit-test</id> 
          <phase>prepare-package</phase> 
          <goals> 
           <goal>report</goal> 
          </goals> 
          <configuration> 
           <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile> 
           <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory> 
          </configuration> 
         </execution> 
         <execution> 
          <id>merge-results</id> 
          <phase>verify</phase> 
          <goals> 
           <goal>merge</goal> 
          </goals> 
          <configuration> 
           <fileSets> 
            <fileSet> 
             <directory>${project.build.directory}/coverage-reports</directory> 
             <includes> 
              <include>*.exec</include> 
             </includes> 
            </fileSet> 
           </fileSets> 
           <destFile>${project.build.directory}/coverage-reports/aggregate.exec</destFile> 
          </configuration> 
         </execution> 
         <execution> 
          <id>post-merge-report</id> 
          <phase>verify</phase> 
          <goals> 
           <goal>report</goal> 
          </goals> 
          <configuration> 
           <dataFile>${project.build.directory}/coverage-reports/aggregate.exec</dataFile> 
           <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory> 
          </configuration> 
         </execution> 
        </executions> 
       </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.18.1</version> 
      <configuration> 
       <argLine>${surefireArgLine}</argLine> 
       <skipTests>${skip.unit.tests}</skipTests> 
       <includes> 
        <include>**/*UT.java</include> 
        <include>**/*MT.java</include> 
        <include>**/*Test.java</include> 
       </includes> 
       <skipTests>${skipUTMTs}</skipTests> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-failsafe-plugin</artifactId> 
      <version>2.12.4</version> 
      <configuration> 
       <skipTests>${skipTests}</skipTests> 
       <skipITs>${skipITs}</skipITs> 
       <argLine>${testArgLine}</argLine> 
       <excludes> 
        <exclude>**/*UT*.java</exclude> 
       </excludes> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>integration-test</goal> 
         <goal>verify</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

正如您所看到的,有6個不同的Jacoco執行來運行測試,合併文件並創建彙總報告。在Jacoco配置之上,還需要配置Surefire和Failsafe以從Jacoco中獲取參數(Surefire運行單元測試,Failsafe運行集成測試)。

我使用的所有配置都應該在那裏,你用它來做什麼是你的設計架構,以使其符合你的要求。就個人而言,如果您遇到文件未被讀取的問題,我建議您查看我排除的內容並將其包含在surefire和failsafe中。

+0

我建議不要更改maven-surefire-plugin和maven-failsafe-plugin的默認命名約定,因爲這不是必需的。保持約定......我強烈建議使用相同版本的maven-surefire和maven-failsafe以及最新版本...... – khmarbaise

+0

可能是值得推薦的。我只是想給用戶一個代碼示例,而不是「嘿,去檢查'這個'出'。 –

+1

@BobVanDeHey非常感謝此片段。這對我設置JaCoCo配置非常有用。 – gvdm