使用 Surefire 和 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 中定义的测试或组之外运行单个测试或组?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我没有使用 TestNG 5.12.1 进行测试,但我可以说使用
test
参数运行单个测试,并使用groups
参数适用于 TestNG 5.14.2(和 Surefire 2.6)(groups
在 TestNG 5.14 中不起作用)pom.xml
:这是我正在使用的 简单的
AppTest
如下:都
和
产生预期的结果。
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 thegroups
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:With a simple
AppTest
as follow:Both
and
produce the expected result.
正如我所解释的那样,在 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:
and then running tests with
我建议尝试类似的方法,
尽管我自己还没有测试过。
I would suggest to try something like
although I haven't tested this myself.
为了运行单个测试,您需要以下来自官方的 文档
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)