maven - 使用 Maven 命令行以编程方式运行 TestNG

标签 maven webdriver testng

我需要并行运行多个测试套件。
其中一种方法是创建一个套件文件,如下所示 -

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="AllTests" verbose="8">
    <suite-files>
    <suite-file path="./Suite1.xml"></suite-file>
    <suite-file path="./Suite2.xml"></suite-file>
</suite-files>
</suite>

创建一个类如下 -
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.testng.xml.Parser;
import org.testng.xml.XmlSuite;
import org.testng.TestNG;
import org.xml.sax.SAXException;

public class RunSuitesInParallel{

    public static void main(String[] args) throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
        TestNG testng = new TestNG(); 
        testng.setXmlSuites((List <XmlSuite>)(new Parser("src"+File.separator+"test"+File.separator+"resources"+File.separator+"xml_Suites"+File.separator+"AllTests.xml").parse()));       
        testng.setSuiteThreadPoolSize(2);
        testng.run();
    }
}

当我从 Eclipse IDE 运行它时,我能够实现上述目标。
如何从 Maven 命令行运行它?

POM.xml 的片段 -
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.14.1</version>
    <configuration>
        <include>com/shn/test/*Tests.class</include>
        <suiteXmlFiles>
            <!-- <suiteXmlFile>src/test/resources/TestNG.xml</suiteXmlFile> -->
            <suiteXmlFile>${tests}</suiteXmlFile>
        </suiteXmlFiles>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>

目前执行我使用的任何给定的 XML -
mvn -Dtests=AllTests.xml test

最佳答案

并行运行测试的最简单解决方案是使用 maven-surefire-plugin 的配置。像这样:

</plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.15</version>
        <configuration>
          <parallel>methods</parallel>
          <threadCount>10</threadCount>
        </configuration>
      </plugin>
    [...]
</plugins>

通常你不需要单独的 testng.xml 文件来运行测试,因为它们将基于 naming conventions for tests 默认运行。 .
此外,除了您给定的定义是错误的之外,不需要制定单独的包含规则。

您可以通过 group 参数控制哪些测试将与 TestNG 相关联地运行,如下所示:
mvn -Dgroups=Group1 test

此外,可以通过 test property 控制将运行哪些测试。像这样:
mvn -Dtest=MyTest test

或者
mvn -Dtest=MyTest,FirstTest,SecondTest test

从命令行指定测试的更细粒度的方法是这样的:
mvn -Dtest=MyTest#myMethod test

运行方法 myMethod在 MyTest 类中。

关于maven - 使用 Maven 命令行以编程方式运行 TestNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17449190/

相关文章:

java - Slf4j 库排除 log4j 的 Sonatype 安全漏洞

maven - 即使我没有在任何地方配置它,Android Gradle Build 也会尝试访问 JCenter

java - 无法运行使用 Java 10 从 Spring Initializer 创建的基于 Maven 的 Spring 项目

javascript - Node Selenium WebdriverJS - 如何检查元素是否真正可点击?

java - Selenium Web 驱动程序无法定位元素

java - 如果 @BeforeMethod 失败,TestNG 至 6.10 会忽略套件中的后续类

java - MAVEN:如何传递 -D 系统变量进行测试

java - WebDriver - 捕获的屏幕截图似乎具有红色或橙色色调

java - 对大量网站运行相同的测试

java - 通过命令行使用 TestNG xml 运行已编译的 Java Selenium 测试(带有依赖项)