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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4159948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:11:12  来源:igfitidea点击:

Running single test class or group with Surefire and TestNG

javaunit-testingmaven-2testngsurefire

提问by Slartibartfast

I want to run single test class from command line using Maven and TestNG

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

Things that doesn't work:

不起作用的事情:

mvn -Dtest=ClassName test

I have defined groups in pom.xml, and this class isn't in one of those groups. So it got excluded on those grounds.

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

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

when config is

当配置是

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

Parameters work fine in there are no groups defined in pom.xml.

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

Similarly, when surefire is configured with

同样,当surefire配置为

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

I can add another test with -Dtestparameter, but cannot add group. In any combination, I can narrow down tests to be executed with groups, but not expand them.

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

What's wrong with my configuration? Is there a way to run a single test or group outside of those defined in pom.xml?

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

Tried on Ubuntu 10.04 with Maven 2.2.1, TestNG 5.14.6 and Surefire 2.7.1

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

采纳答案by Slartibartfast

As I've explained in question, any mention of groups either in pom.xml or on command line resulted in reduction of executed tests count. Only way I've managed to avoid this is by using mavens profiles like this:

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

and then running tests with

然后运行测试

mvn -P test-slow test

回答by Pascal Thivent

I didn't test with TestNG 5.12.1 but I can say that running a single test using the testparameter andtests from groups using the groupsparameter works with TestNG 5.14.2 (and surefire 2.6) (groupsdoesn't work in TestNG 5.14)

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

Here is the pom.xmlI'm using:

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

With a simple AppTestas follow:

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

}

Both

两个都

$ mvn test -Dtest=AppTest

and

$ mvn test -Dgroups=slow

produce the expected result.

产生预期的结果。

回答by Zac Thompson

I would suggest to try something like

我建议尝试类似的东西

mvn test -Dincludes=rs/magrathea/TestClassName

although I haven't tested this myself.

虽然我自己没有测试过。

回答by Denis

In order to run a single test you need the following from official documentation

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

mvn -Dtest=MyFirstTest test

mvn -Dtest=MyFirstTest 测试

or

或者

mvn -Dtest=MyFirstTest,MySecondTest test

mvn -Dtest=MyFirstTest,MySecondTest 测试

This is tested (and working) on maven 3.

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

Then you can avoid using the profiles. I had the same problem as I needed to run load test in isolation and using profiler in parallel to get the real figures.

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

Note: Not sure why but make sure that the switches come before the phase i.e. "-Dtest=MyFirstTest" before "test" otherwise it is not working (Mac OSX)

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