如何从Maven命令行运行testng.xml

如何从Maven命令行运行testng.xml

问题描述:

我在配置中定义的套件文件在我的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 

在第一,请加改进的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中使用变量,你将重写值。

如果你有一个固定的testng xml,那么你只需要做mvn clean test。 默认会在maven的测试阶段执行surefire插件,并且xml硬编码会被拾取。

+0

尝试这个。有用。似乎是触发testng xml的最简单方法。谢谢! – jeffsia

最简单的解决方案是,在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