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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42847732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:27:46  来源:igfitidea点击:

How to run testng.xml from Maven command line

javamavenseleniumcmdtestng

提问by jeffsia

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? 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.

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

 mvn clean test -DsuiteXmlFile=testng.xml

回答by S-Kerrigan

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. And, then, calling it from cmd.

其次,您可以在 pom.xml 中使用变量创建部分。然后,从 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. And, if you will use variable in cmd, you will rewrite values.

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

回答by niharika_neo

If you have a fixed testng xml, then you just need to do 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.

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

回答by Prakash Palnati

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