2011-04-19 138 views
3

我使用TestNg和Maven和surefire插件來運行我的測試。我有幾個不同的組件,我希望能夠在不同的時間使用相同的pom運行。目前要做到這一點,我有幾個不同的XML文件定義了一個測試套件,並且我設置了pom,所以我可以執行mvn test -Dtestfile =/path並使用該套件。使用testng和maven運行不同的測試套件

我想知道是否有辦法將XML文件合併到一個文件中,並選擇基本測試名稱或其他系統?

編輯:我已經用Smoke,Sanity,Regression定義了所有測試,並且我希望能夠爲給定組件運行所有迴歸。如果我運行TestNG CLI,我可以給出-testnames comp1,comp2,comp3等。每個組件在一個包含多個測試(x)的xml套件中定義。我想知道是否有任何方法可以在maven中使用exec:java插件。

回答

6

你可以做的是定義不同的配置文件

<profiles> 
    <profile> 
     <id>t1</id> 
     <build> 
     <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.8.1</version> 
      <configuration> 
      <suiteXmlFiles> 
       <suiteXmlFile>testng.xml</suiteXmlFile> 
      </suiteXmlFiles> 
      </configuration> 
     </plugin> 
     </plugins> 
     </build> 
    </profile> 
    </profiles> 

,並通過MVN -Pt1 ... 從命令行調用或定義配置文件中的屬性,並使用該屬性的configuraiton。

+0

我試過這樣做。那麼,你爲不同的配置文件設置了不同的testng.xml文件嗎? – 12rad 2012-07-24 17:57:28

+0

這樣做,我不斷收到錯誤。 – 12rad 2012-07-24 17:57:40

6

TestNG通過在測試用例本身或者suite.xml文件中指定測試類/方法上的組來支持測試組。通過使用組,您可以將所有測試放在一個xml文件中。請參閱TestNG用戶指南中的Groups

Surefire插件允許測試基於組中包含或排除:

 <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.8.1</version> 
     <configuration> 
      <groups>${testng.groups}</groups> 
     </configuration> 
     </plugin> 

你可以把所有的測試在一個XML文件,然後選擇通過一個或多個組設置爲運行哪些包含在$ {testng.groups}屬性中,該屬性應該是以逗號分隔的組名列表。

您可以使用配置文件或在命令行-Dtestng.groups=[groups to run]上爲POM中的$ {testng.groups}屬性定義值。

0

另請注意,TestNG允許您將多個套件組合成一個套件,例如,如果你想你的API和UI冒煙測試結合成一個套件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="uber-smoke-suite" verbose="1" parallel="methods" thread-count="1" configfailurepolicy="continue"> 
    <suite-files> 
    <suite-file path="smoke_api.xml" /> 
    <suite-file path="smoke_ui.xml" /> 
    </suite-files> 
</suite> 

這樣,你也可以創建一個超級套件也結合了所有的測試爲一體,但仍允許您根據需要來跑單套房,例如:

-Dtestfile=smoke 
-Dtestfile=smoke_api 
-Dtestfile=smoke_ui 
相關問題