2011-08-22 51 views
3

我有一些慢速測試依賴於我不希望每次使用Maven構建項目時運行的數據庫。我已將excludedGroups元素添加到我的pom文件中,如http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#excludedGroups所解釋的,但我似乎無法使其工作。從Maven中排除TestNG組

我創建了一個最小的項目。下面是pom.xml中:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>test</groupId> 
    <artifactId>exclude</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.4.2</version> 
       <configuration> 
        <excludedGroups>db</excludedGroups> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

    <dependencies> 
     <dependency> 
      <groupId>org.testng</groupId> 
      <artifactId>testng</artifactId> 
      <version>5.14</version> 
     </dependency> 
    </dependencies> 

</project> 

而且這是兩個測試類:

public class NormalTest { 

    @Test 
    public void fastTest() { 
     Assert.assertTrue(true); 
    } 
} 

public class DatabaseTest { 

    @Test(groups={"db"}) 
    public void slowTest() { 
     Assert.assertTrue(false); 
    } 
} 

但是這兩個測試仍然可以運行。我無法弄清楚我做錯了什麼。

回答

5

我結束了創建外部測試服:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > 
<suite name="tests"> 
     <test name="standard"> 
     <groups> 
      <run> 
       <exclude name="slow" /> 
       <exclude name="external" /> 
       <exclude name="db" /> 
      </run> 
     </groups> 
     <packages> 
      <package name="com.test.*" /> 
     </packages> 
    </test> 
</suite> 

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > 
<suite name="tests"> 
    <test name="full"> 
     <packages> 
      <package name="com.test.*" /> 
     </packages> 
    </test> 
</suite> 

和specifyied其在配置文件中運行:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.11</version> 
    <configuration> 
     <suiteXmlFiles> 
      <suiteXmlFile>src/test/resources/suites/standard.xml</suiteXmlFile> 
     </suiteXmlFiles> 
    </configuration> 
</plugin> 

...

<profile> 
    <id>fulltest</id> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <configuration> 
        <suiteXmlFiles> 
         <suiteXmlFile>src/test/resources/suites/full.xml</suiteXmlFile> 
        </suiteXmlFiles> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</profile> 
3

根據我的經驗,排除的羣組功能僅在您擁有一組包含的羣組時纔有效。所以爲了做你想做的事情,你需要把所有的測試添加到至少一個組中(你可以通過註釋類而不是方法來「輕鬆」做到這一點)。

例如(只是改變了NormalTest)

@Test(groups = "fast") 
public class NormalTest { 

    @Test 
    public void slowTest() { 
     Assert.assertTrue(true); 
    } 
} 

,並在配置

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.4.2</version> 
      <configuration> 
       <groups>fast</groups> 
       <excludedGroups>db</excludedGroups> 
      </configuration> 
     </plugin> 

我知道,這是不是明顯,但它是TestNG的工作方式:S。作爲一個方面說明,我總是使用testng的外部配置文件,而不是pom中的嵌入式配置,因此參數groups可能不正確。

+2

這似乎是Maven(實際上是Surefire)的限制,而不是TestNG。單獨使用TestNG,您可以排除一個組而不包含任何組件。 –

+0

我試過在所有測試中加入組並在pom中指定這些,但這似乎沒有任何區別。 – samblake

+1

謝謝,這對我很有用。 「組」和「excludedGroups」按預期工作 - 它們可以一起使用或獨立使用,並按照您的預期行事。測試:maven 2.2.1,maven-surefire-plugin 2.4.3,testng 5.14.6。 – pestrella