繁体   English   中英

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

[英]Running single test class or group with Surefire and TestNG

我想使用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中定义的那些之外运行单个测试或组?

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

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

产生预期的结果。

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

要运行单个测试,您需要从官方文档中获得以下内容

mvn -Dtest = MyFirstTest测试

要么

mvn -Dtest = MyFirstTest,MySecondTest测试

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

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

注意:不确定原因,但要确保开关在“test”之前出现在“-Dtest = MyFirstTest”之前,否则它不起作用(Mac OSX)

我建议尝试类似的东西

mvn test -Dincludes=rs/magrathea/TestClassName

虽然我自己没有测试过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM