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

发布于 2024-10-02 04:44:20 字数 1016 浏览 7 评论 0原文

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

I want to run single test class from command line using Maven and 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.

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.

Similarly, when surefire is configured with

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星軌x 2024-10-09 04:44:20

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

产生预期的结果。

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

Here is the pom.xml I'm using:

<?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 AppTest as follow:

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.

梦亿 2024-10-09 04:44:20

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

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:

<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
悟红尘 2024-10-09 04:44:20

我建议尝试类似的方法,

mvn test -Dincludes=rs/magrathea/TestClassName

尽管我自己还没有测试过。

I would suggest to try something like

mvn test -Dincludes=rs/magrathea/TestClassName

although I haven't tested this myself.

将军与妓 2024-10-09 04:44:20

为了运行单个测试,您需要以下来自官方的 文档

mvn -Dtest=MyFirstTest test

mvn -Dtest=MyFirstTest,MySecondTest test

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

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

注意:不知道为什么,但请确保开关位于阶段之前,即“test”之前的“-Dtest=MyFirstTest”,否则它将无法工作(Mac OSX)

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

mvn -Dtest=MyFirstTest test

or

mvn -Dtest=MyFirstTest,MySecondTest test

This is tested (and working) on 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文