【问题标题】:How to run testng.xml from Maven command line如何从 Maven 命令行运行 testng.xml
【发布时间】:2017-08-08 10:24:01
【问题描述】:

我的 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>

使用 maven 在 cmd 中运行它的正确方法是什么?我可以使用下面的命令运行测试,但是当它已经在 pom 中定义时,再次在命令中指示 testng.xml 是没有意义的。

 mvn clean test -DsuiteXmlFile=testng.xml

【问题讨论】:

    标签: java maven selenium cmd testng


    【解决方案1】:

    首先,请在 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 中,您将 xmls 设置为默认值并放置到变量中。而且,如果您将在 cmd 中使用变量,您将重写值。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 试过这个。有用。似乎是触发 testng xml 的最简单方法。谢谢!
      【解决方案3】:

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

      【讨论】:

      • 谢谢普拉卡什。这对我帮助很大。在大多数线程中,答案都是错误的,但您的帮助很大。
      • 经过大量浏览,终于成功了。谢谢普拉卡什。
      • 我收到此错误 [错误] 未知生命周期阶段“.suiteXmlFile=./testng.xml”。
      • 没关系,我之前使用的是 powershell。命令提示符很好。
      【解决方案4】:

      需要在 suiteXmlFiles 标签之间传递 testng.xml 文件内容根目录的路径

      例如对于我的项目代替 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>
      

      现在运行mvn clean test, 它将从 pom.xml 中选择测试套件文件并运行该套件中提到的所有测试类。

      注意:

      1. 我们需要提到 testng.xml 文件的完整路径的原因是, 因为默认情况下maven会直接在项目级别下搜索这个文件

      2. 如果您使用 IntelliJ,您可以通过右键单击文件 -> 复制路径 -> 内容根目录中的路径,从内容根目录中找到您的 testng.xml 文件的路径

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 2020-03-16
        • 1970-01-01
        • 2018-10-13
        • 2017-10-09
        • 1970-01-01
        • 2019-09-07
        相关资源
        最近更新 更多