2012-05-21 21 views
3

我目前正在開發一個Maven支持的項目。我選擇了TestNg來實現我的單元測試。爲了在每個Maven構建運行我的幺測試,我已經添加了Maven的萬無一失,插件我的pom.xml:帶有TestNg的Maven-surefire-plugin:如何指定存儲測試套件xml文件的目錄?

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.12</version> 
      <configuration> 
      <!-- Configuring the test suites to execute --> 
       <suiteXmlFiles> 
        <suiteXmlFile>testsuite-persistence-layer.xml</suiteXmlFile> 
       </suiteXmlFiles> 
      </configuration> 
     </plugin> 

此外,我要指定使用TestNG的TestSuiteXmlFile執行的測試。例如,在我的pom.xml中,我配置了surefire插件,以便它將執行名爲「testsuite-persistence-layer.xml」的xml文件中定義的測試。

問題是,默認情況下,surefire插件似乎是在我的項目的根目錄下查找這個xml文件。 如何指定surefire插件應該查找TestSuite xml文件的目錄?

根據TestNg文檔,這可以通過「maven.testng.suitexml.dir」屬性指定,但Surefire插件似乎沒有考慮到它。

回答

6

我不確定我是否瞭解您的問題。您可以輕鬆地指定xml文件的確切位置,既可以指定相對路徑,也可以指定完全限定路徑。

<suiteXmlFile>c:/some/dir/testsuite-persistence-layer.xml</suiteXmlFile> 

<suiteXmlFile>src/test/java/com/something/project/testsuite-persistence-layer.xml</suiteXmlFile> 

但是,這是太容易了,所以我猜你正在尋找一種方式來參數化,其中個XML所在的目錄。這使我腦海中快速的解決辦法是

<suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile> 

現在你可以運行

mvn test -DxmlPath=c:/some/path 

當然xmlpath中只是虛構的名字,你可以使用任何你希望的其他變量名。

如果您不想將路徑作爲參數從命令行傳遞,則可以在POM的屬性部分中指定xmlPath變量的值。屬性是位於<項目>分支下的主要部分之一。

<project ... > 
    ... 

    <properties> 
     <xmlPath>c:/some/path</xmlPath> 
    </properties> 
     ... 

    <build> 
     ... 
     <plugins> 
     ... 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <suiteXmlFiles> 
         <suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile> 
        </suiteXmlFiles>           
        ... 
       </configuration> 
      </plugin> 
     ... 
     </plugins> 
     ... 
    </build> 
    ... 

</project> 
+0

非常感謝您的幫助!不知道,也沒有嘗試在標籤內指定一個相對於項目根的路徑 – kyiu

+0

其他方式是在一組接下來的命令集之後運行mvn -D測試$ {path}/test1 test,mvn -D測試$ {路徑}/TEST2測試和依賴關係是 org.apache.maven.plugins 行家-萬無一失-插件 2.12 <結構> <! - 套件從命令行輸入文件注入參數 - > $ {tests} .xml Badrinath

相關問題