简体   繁体   English

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

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

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. 我在pom.xml中定义了组,并且此类不在其中一个组中。 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. 参数工作正常,没有在pom.xml中定义的组。

Similarly, when surefire is configured with 同样,配置surefire时

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

I can add another test with -Dtest parameter, but cannot add group. 我可以使用-Dtest参数添加另一个测试,但不能添加组。 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? 有没有办法在pom.xml中定义的那些之外运行单个测试或组?

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

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) 我没有使用TestNG 5.12.1进行测试,但我可以说使用test参数运行单个测试使用groups参数从组中进行测试可以使用TestNG 5.14.2(和surefire 2.6)( groups在TestNG中不起作用) 5.14)

Here is the pom.xml I'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 AppTest as 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. 产生预期的结果。

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. 正如我在解释中所解释的那样,在pom.xml或命令行中提及组都会导致执行测试计数减少。 Only way I've managed to avoid this is by using mavens profiles like this: 我设法避免这种情况的唯一方法是使用像这样的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

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 ie "-Dtest=MyFirstTest" before "test" otherwise it is not working (Mac OSX) 注意:不确定原因,但要确保开关在“test”之前出现在“-Dtest = MyFirstTest”之前,否则它不起作用(Mac OSX)

I would suggest to try something like 我建议尝试类似的东西

mvn test -Dincludes=rs/magrathea/TestClassName

although I haven't tested this myself. 虽然我自己没有测试过。

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

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