11

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

不起作用的事情:

mvn -Dtest=ClassName test

我在 pom.xml 中定义了组,而这个类不在其中一个组中。所以它基于这些理由被排除在外。

mvn -Dgroups=skipped-group test
mvn -Dsurefire.groups=skipped-group test

当配置是

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.7.1</version>
  <configuration>
    <groups>functest</groups>
  </configuration>
</plugin>

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

同样,当surefire配置为

<configuration>
  <includes>
    <include>**/*UnitTest.java</include>
  </includes>
</configuration> 

我可以添加另一个带有-Dtest参数的测试,但不能添加组。在任何组合中,我都可以缩小要与组一起执行的测试,但不能扩展它们。

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

在 Ubuntu 10.04 上试用 Maven 2.2.1、TestNG 5.14.6 和 Surefire 2.7.1

4

4 回答 4

11

我没有使用 TestNG 5.12.1 进行测试,但我可以说使用参数运行单个测试并使用参数test从组中进行测试适用于 TestNG 5.14.2(和 surefire 2.6)(在 TestNG 5.14 中不起作用)groupsgroups

这是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>

简单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");
 }

}

两个都

$ mvn test -Dtest=AppTest

$ mvn test -Dgroups=slow

产生预期的结果。

于 2010-11-12T08:35:44.953 回答
4

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

<profiles>
    <profile>
        <id>test-slow</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <groups>slow</groups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

然后运行测试

mvn -P test-slow test
于 2011-01-18T14:18:40.083 回答
0

我建议尝试类似的东西

mvn test -Dincludes=rs/magrathea/TestClassName

虽然我自己没有测试过。

于 2011-01-17T07:51:19.263 回答
0

为了运行单个测试,您需要官方文档中的以下内容

mvn -Dtest=MyFirstTest 测试

或者

mvn -Dtest=MyFirstTest,MySecondTest 测试

这是在 maven 3 上测试(和工作)的。

然后,您可以避免使用配置文件。我遇到了同样的问题,因为我需要单独运行负载测试并并行使用分析器来获取真实数据。

注意:不知道为什么,但要确保开关出现在阶段之前,即“-Dtest=MyFirstTest”在“测试”之前,否则它不起作用(Mac OSX)

于 2013-08-07T06:58:17.140 回答