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

标签 java unit-testing maven-2 testng surefire

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

最佳答案

我没有使用 TestNG 5.12.1 进行测试,但我可以说使用 test 参数运行单个测试 使用 groups 的组测试 参数适用于 TestNG 5.14.2(和 surefire 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

产生预期的结果。

关于java - 使用 Surefire 和 TestNG 运行单个测试类或组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4159948/

相关文章:

java - BoneCP & Derby - 如何正确关闭

java - Hibernate Left Outer Join问题: path expected on join

java - 从颜色选择器中删除文本

java - 用户类别信息

java - 完全模拟和部分模拟有什么区别?

javascript - 用于单元测试的模拟 Angular bootstrap-ui 指令

c++ - Boost单元测试链接错误

maven-2 - 如何在 Maven 中更改用户代理?

java - spring - 从类路径资源 hibernate 加载 *.hbm.xml

java - 没有使用扁平的 pom?