使用Surefire和TestNG运行单个测试类或组 [英] Running single test class or group with Surefire and TestNG

查看:216
本文介绍了使用Surefire和TestNG运行单个测试类或组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

不起作用的东西:

mvn -Dtest=ClassName test

我在pom.xml中定义了组,而此类不在这些组之一中.因此,基于这些理由将其排除在外.

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

当配置为

<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中没有定义任何组的情况下,参数可以正常工作.

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

类似地,当将surefire配置为

Similarly, when surefire is configured with

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

我可以使用-Dtest参数添加另一个测试,但是不能添加组.无论如何,我都可以缩小要与组一起执行的测试的范围,但不能扩展它们.

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.

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

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

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

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

推荐答案

正如我所解释的那样,在pom.xml或命令行中对组的任何提及都会减少已执行测试的数量.我设法避免这种情况的唯一方法是使用像这样的Maven配置文件:

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>

然后使用

mvn -P test-slow test

这篇关于使用Surefire和TestNG运行单个测试类或组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆