2017-03-17 129 views
0

我在配置中定義的套件文件在我的pom.xml中有以下條目。如何從Maven命令行運行testng.xml

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.19.1</version> 
    <configuration> 
     <suiteXmlFiles> 
      <suiteXmlFile>testng.xml</suiteXmlFile> 
     </suiteXmlFiles> 
    </configuration> 
</plugin> 

什麼是使用maven在cmd中運行此命令的正確方法?我能夠使用下面的命令運行測試,但在命令中已經定義了pom時再次指明testng.xml是沒有意義的。

mvn clean test -DsuiteXmlFile=testng.xml 

回答

0

在第一,請加改進的pom.xml ...需要加入執行部分:

<executions> 
<execution> 
    <phase>test</phase> 
    <goals> 
     <goal>test</goal> 
    </goals> 
</execution> 
</executions> 

第二,你可以創建在pom.xml中與變量部分。然後,從cmd中調用它。

<properties> 
<defaultSuiteFiles> 
     ./Sets/Suits/CMS_Administration_SiteBackup_029.xml, 
    </defaultSuiteFiles> 
    <suiteFile>${defaultSuiteFiles}</suiteFile> 
</defaultSuiteFiles> 
</properties> 
... 
... 
<suiteXmlFiles> 
    <suiteXmlFile>${suiteFile}</suiteXmlFile> 
</suiteXmlFiles> 

因此,樣機的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"> 
..... 
..... 


<properties>   
    <defaultSuiteFiles> 
     ./Sets/Suits/CMS_Administration_SiteBackup_029.xml, 
    </defaultSuiteFiles> 
    <suiteFile>${defaultSuiteFiles}</suiteFile> 


    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 

<profiles> 
    <profile> 
     <id>Base configuration</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <build> 
      <defaultGoal>install</defaultGoal> 
      <plugins>      
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-surefire-plugin</artifactId> 
        <version>2.19.1</version> 
        <inherited>true</inherited> 
        <executions> 
         <execution> 
          <phase>test</phase> 
          <goals> 
           <goal>test</goal> 
          </goals> 
         </execution> 
        </executions> 
        <configuration> 
         <suiteXmlFiles> 
          <suiteXmlFile>${suiteFile}</suiteXmlFile> 
         </suiteXmlFiles>        
        </configuration> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles> 
<dependencies> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-java</artifactId> 
     <version>3.3.1</version> 
    </dependency> 
</dependencies> 
</project> 

,並使用此,如何爲CMD一個例子:mvn test -DdefaultSuiteFiles="./YOUR_DIRECTORY/YOUR_SUITE.xml,"

說明:在pom.xml中,你設置個XML默認並放置到變量。而且,如果你將在cmd中使用變量,你將重寫值。

2

如果你有一個固定的testng xml,那麼你只需要做mvn clean test。 默認會在maven的測試階段執行surefire插件,並且xml硬編碼會被拾取。

+0

嘗試這個。有用。似乎是觸發testng xml的最簡單方法。謝謝! – jeffsia

0

最簡單的解決方案是,在surefire插件中添加下面的行。

<suiteXmlFiles> 
    <!-- pass testng.xml files as argument from command line --> 
    <suiteXmlFile>${suiteXmlFile}</suiteXmlFile> 
</suiteXmlFiles> 

並運行如下的xml文件,

mvn clean test -Dsurefire.suiteXmlFiles=/path/to/testng.xml