使用Surefire和TestNG运行单个测试类或组

Sla*_*ast 10 java testng maven-2 unit-testing surefire

我想使用Maven和TestNG从命令行运行单个测试类

不起作用的事情:

mvn -Dtest=ClassName test
Run Code Online (Sandbox Code Playgroud)

我在pom.xml中定义了组,并且该类不在其中一个组中.所以它被排除在这些理由之外.

mvn -Dgroups=skipped-group test
mvn -Dsurefire.groups=skipped-group test
Run Code Online (Sandbox Code Playgroud)

什么时候配置

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.7.1</version>
  <configuration>
    <groups>functest</groups>
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

参数工作正常,没有在pom.xml中定义的组.

同样,配置surefire时

<configuration>
  <includes>
    <include>**/*UnitTest.java</include>
  </includes>
</configuration> 
Run Code Online (Sandbox Code Playgroud)

我可以使用-Dtest参数添加另一个测试,但不能添加组.在任何组合中,我可以缩小要使用组执行的测试,但不能扩展它们.

我的配置有什么问题?有没有办法在pom.xml中定义的那些之外运行单个测试或组?

使用Maven 2.2.1,TestNG 5.14.6和Surefire 2.7.1在Ubuntu 10.04上尝试

Pas*_*ent 10

我没有使用TestNG测试5.12.1,但我可以说,通过运行一个测试test参数,并使用该组测试groups参数使用TestNG 5.14.2(和神火2.6)的作品(groups不TestNG的5.14工作)

这是pom.xml我正在使用的:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.stackoverflow</groupId>
  <artifactId>Q4159948</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Q4159948</name>
  <url>http://maven.apache.org</url>
  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>5.14.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.6</version>
        <configuration/>
      </plugin>
    </plugins>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

简单AppTest如下:

import org.testng.annotations.*;

public class AppTest {

 @BeforeClass
 public void setUp() {
   // code that will be invoked when this test is instantiated
 }

 @Test(groups = { "fast" })
 public void aFastTest() {
   System.out.println("Fast test");
 }

 @Test(groups = { "slow" })
 public void aSlowTest() {
    System.out.println("Slow test");
 }

}
Run Code Online (Sandbox Code Playgroud)

$ mvn test -Dtest=AppTest
Run Code Online (Sandbox Code Playgroud)

$ mvn test -Dgroups=slow
Run Code Online (Sandbox Code Playgroud)

产生预期的结果.


Sla*_*ast 4

正如我所解释的那样,在 pom.xml 中或在命令行中提及组都会导致执行的测试计数减少。我设法避免这种情况的唯一方法是使用这样的 Maven 配置文件:

<profiles>
    <profile>
        <id>test-slow</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <groups>slow</groups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

然后运行测试

mvn -P test-slow test
Run Code Online (Sandbox Code Playgroud)