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

jef*_*sia 5 java testng selenium cmd maven

我在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>
Run Code Online (Sandbox Code Playgroud)

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

 mvn clean test -DsuiteXmlFile=testng.xml
Run Code Online (Sandbox Code Playgroud)

Pra*_*ati 6

最简单的解决方案是在surefire插件中添加以下几行。

<suiteXmlFiles>
    <!-- pass testng.xml files as argument from command line -->
    <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
Run Code Online (Sandbox Code Playgroud)

然后如下运行您的xml文件,

mvn clean test -Dsurefire.suiteXmlFiles=/path/to/testng.xml
Run Code Online (Sandbox Code Playgroud)


S-K*_*gan 5

首先,请在 pom.xml 中添加改进...需要添加执行部分:

<executions>
<execution>
    <phase>test</phase>
    <goals>
        <goal>test</goal>
    </goals>
</execution>
</executions>
Run Code Online (Sandbox Code Playgroud)

其次,您可以在 pom.xml 中使用变量创建部分。然后,从 cmd 调用它。

<properties>
<defaultSuiteFiles>
        ./Sets/Suits/CMS_Administration_SiteBackup_029.xml,
    </defaultSuiteFiles>
    <suiteFile>${defaultSuiteFiles}</suiteFile>
</defaultSuiteFiles>
</properties>
...
...
<suiteXmlFiles>
    <suiteXmlFile>${suiteFile}</suiteXmlFile>
</suiteXmlFiles>
Run Code Online (Sandbox Code Playgroud)

所以,原型 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>
Run Code Online (Sandbox Code Playgroud)

并使用这个,如何使用 cmd 的示例:mvn test -DdefaultSuiteFiles="./YOUR_DIRECTORY/YOUR_SUITE.xml,"

说明:在 pom.xml 中,您将 xml 设置为默认值并放置到变量中。而且,如果您将在 cmd 中使用变量,您将重写值。


nih*_*neo 5

如果您有固定的 testng xml,那么您只需要执行mvn clean test. Surefire插件将在maven的测试阶段默认执行,并且硬编码的xml将被拾取。