2015-09-07 56 views
1

我有一個包含單元和集成測試的項目。 這些測試已經分解爲單元測試和集成測試,同時使用不同的套件作爲套件。用於將測試分離爲單元和集成測試的Maven surefire配置失敗

如何配置surefire在階段「集成測試」的階段「測試」和集成測試期間執行單元測試。

這裏是我當前的配置:

<plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.18.1</version> 
       <configuration> 
        <excludes> 
         <exclude>**/Test*.java</exclude> 
         <exclude>**/*Test.java</exclude> 
         <exclude>**/*Test*.java</exclude> 
        </excludes> 
       </configuration> 
       <executions> 
        <execution> 
         <id>unit-test</id> 
         <phase>test</phase> 
         <goals> 
          <goal>test</goal> 
         </goals> 
         <configuration> 
          <includes> 
           <include>**/CoreUnitTests.java</include> 
          </includes> 
         </configuration> 
        </execution> 
        <execution> 
         <id>integration-test</id> 
         <phase>integration-test</phase> 
         <goals> 
          <goal>test</goal> 
         </goals> 
         <configuration> 
          <includes> 
           <include>**/CoreIntegrationTests.java</include> 
          </includes> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 

利用這種結構沒有測試將被執行,但如果我刪除了「不包括」所有測試階段中的「測試」,而不僅是單元測試執行。


更新 - 解決方案

與亞當·米哈利克的解釋,我是能夠解決的問題。 而是覆蓋默認測試我跳過這些測試,因爲ID「單元測試」比「默認測試」我的單元測試 這裏更好的是POM的最終配置:

<plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.18.1</version> 
       <executions> 
        <execution> 
         <id>default-test</id> 
         <configuration> 
          <skip>true</skip> 
         </configuration> 
        </execution> 
        <execution> 
         <id>unit-test</id> 
         <phase>test</phase> 
         <goals> 
          <goal>test</goal> 
         </goals> 
         <configuration> 
          <includes> 
           <include>**/CoreUnitTests.java</include> 
          </includes> 
         </configuration> 
        </execution> 
        <execution> 
         <id>integration-test</id> 
         <phase>integration-test</phase> 
         <goals> 
          <goal>test</goal> 
         </goals> 
         <configuration> 
          <includes> 
           <include>**/CoreIntegrationTests.java</include> 
          </includes> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 

回答

2

經驗法則:excludes覆蓋includes。因此,服用unit-test執行作爲一個例子,你的配置告訴萬無一失到:

  • 首先,只有包括**/CoreUnitTests.java
  • 但是從,排除**/Test*.java**/Test.java**/*Test*.java - 即排除清單涵蓋CoreUnitTests.java,所以什麼都沒有執行

同樣爲integration-test執行。

因此,在integration-test階段只有運行CoreUnitTeststest相和CoreIntegrationTests,是應該足以配置includes像你一樣,不定義任何excludes任何地方。然而,這還不夠好,因爲正如你所觀察到的,所有的測試都是由默認的Surefire程序運行的,這個程序被稱爲default-test,在默認配置下,它會選取所有後綴爲Test的類。要解決這個問題,你可以通過重新配置它

<execution> 
    <id>default-test</id> 
    <configuration> 
     <skip>true</skip> 
    </configuration> 
</execution> 

關閉在默認執行或您的unit-test執行重命名爲default-test(這將節省您的構建時間爲幾毫秒,因爲最終你只會調用Surefire兩次,而不是三次只是爲了跳過默認執行)。

+0

謝謝你的好解釋和你的解決方案! –