簡體   English   中英

使用Surefire和TestNG運行單個測試類或組

[英]Running single test class or group with Surefire and 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中定義的那些之外運行單個測試或組?

使用Maven 2.2.1,TestNG 5.14.6和Surefire 2.7.1在Ubuntu 10.04上嘗試

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

產生預期的結果。

正如我在解釋中所解釋的那樣,在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>

然后運行測試

mvn -P test-slow test

要運行單個測試,您需要從官方文檔中獲得以下內容

mvn -Dtest = MyFirstTest測試

要么

mvn -Dtest = MyFirstTest,MySecondTest測試

這是在maven 3上測試(和工作)。

然后,您可以避免使用配置文件。 我有同樣的問題,因為我需要單獨運行負載測試並且並行使用分析器來獲得真實數據。

注意:不確定原因,但要確保開關在“test”之前出現在“-Dtest = MyFirstTest”之前,否則它不起作用(Mac OSX)

我建議嘗試類似的東西

mvn test -Dincludes=rs/magrathea/TestClassName

雖然我自己沒有測試過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM