【问题标题】:Running single test class or group with Surefire and TestNG使用 Surefire 和 TestNG 运行单个测试类或组
【发布时间】:2010-11-11 22:34:57
【问题描述】:

我想使用 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

【问题讨论】:

  • 以前从未遇到过这个问题。检查您使用的surefire 版本是最新的。除此之外,我通常会运行我的单个测试,例如: mvn test -Dtest=ClassName (交换参数)。但我不认为这应该有所作为

标签: java unit-testing maven-2 testng surefire


【解决方案1】:

我没有使用 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>

用一个简单的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

产生预期的结果。

【讨论】:

    【解决方案2】:

    正如我在问题中所解释的,在 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>
    

    然后用

    运行测试
    mvn -P test-slow test
    

    【讨论】:

      【解决方案3】:

      我会建议尝试类似的东西

      mvn test -Dincludes=rs/magrathea/TestClassName
      

      虽然我自己没有测试过。

      【讨论】:

        【解决方案4】:

        为了运行单个测试,您需要以下 from official documentation

        mvn -Dtest=MyFirstTest 测试

        mvn -Dtest=MyFirstTest,MySecondTest 测试

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多