![](/img/trans.png)
[英]How to run tests from the command line ? (IntelliJ Idea + Java +TestNG)
[英]TestNG annotations are not respected when run group based tests through command line
我想基礎上,通過與Maven命令行的組來運行我的測試。(意思是如果我說mvn test -Dgroups=sanity
,僅測試標有sanity
組將運行)
為了實現上述目的,我遵循了pom.xml中的代碼。 由於以下原因,如果命令行上沒有為group即mvn test
提供任何內容,它將使用regression
組運行測試
<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>org.maven.test</groupId>
<artifactId>MavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<groups>regression</groups>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<groups>${groups}</groups>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我的測試類具有BeforeClass
和AfterClass
批注:
public class SanityTest {
@Test(groups={"sanity"})
public void f() {
System.out.println("This is sanity test");
}
@BeforeClass
public void beforeClass() {
System.out.println("This is before class.");
}
@AfterClass
public void afterClass() {
System.out.println("This is after class.");
}
}
為了運行健全性測試,我點擊了mvn test -Dgroups=sanity
。 健全性測試可以運行,但是AfterClass
和BeforeClass
方法沒有執行。
其輸出為:
This is sanity test
實際上,我期望在這里:
This is before class.
This is sanity test
This is after class.
有人可以幫我做什么,以便也尊重TestNG注釋嗎?
您需要將alwaysRun屬性添加到@BeforeClass和@AfterClass注釋中:
從testNG文檔中: alwaysRun對於before方法(beforeSuite,beforeTest,beforeTestClass和beforeTestMethod,但not beforeGroups除外):如果設置為true,則將運行此配置方法,無論它屬於哪個組。 對於after方法(afterSuite,afterClass,...):如果設置為true,則即使先前調用的一個或多個方法失敗或被跳過,此配置方法也將運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.