简体   繁体   English

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

[英]How to run testng.xml from Maven command line

I have the following entry in my pom.xml with the suite file defined in the configuration.我的 pom.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>

What is a the correct way to run this in cmd using maven?使用 maven 在 cmd 中运行它的正确方法是什么? I am able to run the test with the command below but it doesn't make sense to indicate the testng.xml again in the command when it's already defined in the pom.我可以使用下面的命令运行测试,但是当 testng.xml 已经在 pom 中定义时,在命令中再次指示它没有意义。

 mvn clean test -DsuiteXmlFile=testng.xml

Easiest solution would be, add the below lines in surefire plugin.最简单的解决方案是,在surefire插件中添加以下几行。

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

And run your xml file as below,并按如下方式运行您的 xml 文件,

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

If you have a fixed testng xml, then you just need to do mvn clean test .如果你有一个固定的 testng xml,那么你只需要做mvn clean test The surefire plugin would be executed by default in the test phase of maven and the xml hardcoded would be picked up. surefire 插件会在 maven 的测试阶段默认执行,并且硬编码的 xml 会被拾取。

in first, please add improvement in pom.xml... need adding execution section:首先,请在 pom.xml 中添加改进...需要添加执行部分:

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

In second, you can create section in pom.xml with variables.其次,您可以在 pom.xml 中使用变量创建部分。 And, then, calling it from cmd.然后,从 cmd 调用它。

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

So, result of prototype pom.xml所以,原型 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>

And use this, how a example for cmd: mvn test -DdefaultSuiteFiles="./YOUR_DIRECTORY/YOUR_SUITE.xml,"并使用这个,如何作为 cmd 的示例: mvn test -DdefaultSuiteFiles="./YOUR_DIRECTORY/YOUR_SUITE.xml,"

Explanations : in pom.xml you setup xmls for default and place to variable.说明:在 pom.xml 中,您将 xmls 设置为默认值并放置到变量。 And, if you will use variable in cmd, you will rewrite values.而且,如果您将在 cmd 中使用变量,您将重写值。

Need to pass path from the content root of testng.xml file between suiteXmlFiles tag需要在suiteXmlFiles标签之间从testng.xml文件的内容根传递路径

for eg for my project in place of testng.xml i am using file name as AllTests.xml, which is located under folder TestSuites例如,对于我的项目代替 testng.xml,我使用文件名作为 AllTests.xml,它位于文件夹 TestSuites 下

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/TestSuites/AllTests.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
 </plugin>

and now run mvn clean test , it will pick the test suite file from pom.xml and run all the test classes mentioned in this suite.现在运行mvn clean test ,它将从 pom.xml 中选择测试套件文件并运行该套件中提到的所有测试类。

Note:笔记:

  1. Reason we need to mention complete path of testng.xml file is, because by default maven will search this file directly under the project level之所以需要提到testng.xml文件的完整路径,是因为maven默认会在项目级别直接搜索这个文件

  2. If you using IntelliJ you can find Path from content root for your testng.xml file, by doing right click on the file-> Copy Path -> Path from content root如果您使用 IntelliJ,则可以通过右键单击文件 -> 复制路径 -> 来自内容根目录的路径,从内容根目录中找到 testng.xml 文件的路径

Followed exactly same as solution provided by @S-Kerrigan.But it is behaving differently in below scenario.遵循与@S-Kerrigan 提供的解决方案完全相同。但在以下情况下它的行为有所不同。

In my folder, there are 3 testng xml files but i am passing only one testng xml file during run time but it is executing all three xml files..在我的文件夹中,有 3 个 testng xml 文件,但我在运行时只传递了一个 testng xml 文件,但它正在执行所有三个 xml 文件。

In properties in pom file defined as below:在 pom 文件的属性中定义如下:

<defaultSuiteFiles>sampletestng</defaultSuiteFiles>
<suiteFile>${defaultSuiteFiles}</suiteFile> 

In plugin as below:在插件中如下:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
     <configuration>
      <testFailureIgnore>true</testFailureIgnore>
        <suiteXmlFiles>
         <suiteXmlFile>${suiteFile}</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

In CMD mvn clean test -DdefaultSuiteFiles=src/test/resources/TestSuites/precondiiton.xml在 CMD mvn clean test -DdefaultSuiteFiles=src/test/resources/TestSuites/precondiiton.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM