我有一些慢速测试依赖于我不希望每次使用Maven构建项目时都运行的数据库。我已将excludedGroups元素添加到我的pom文件中,如http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#excludedGroups所述,但我似乎无法使其正常工作。
我创建了一个最小的项目。这是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);
}
}
然而,两项测试仍然有效。我无法弄清楚我做错了什么。
答案 0 :(得分: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>
并指定在个人资料中运行的内容:
<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>
答案 1 :(得分: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。作为旁注,我总是使用外部配置文件来测试,而不是pom中的嵌入式配置,因此参数groups
可能不正确。