java - 尝试使用 Surefire 从 Maven 启动并行 TestNG 测试

标签 java maven parallel-processing testng maven-surefire-plugin

我正在尝试使用 Surefire 从 Maven 启动并行 TestNG 测试,但到目前为止收效甚微。

我的套件有 4 个测试,但 Maven 到目前为止只是连续运行一个测试。只有当一项测试完成后,下一项测试才会开始。

如果您能查看我正在使用的配置,如果您发现任何问题或有任何一般性建议,请告诉我,我将非常感激。

这是在 pom.xml 中配置 Surefire 的方式:-

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
            <parallel>classes</parallel>
            <threadCount>3</threadCount>
            <testFailureIgnore>true</testFailureIgnore>
            <suiteXmlFiles>
                <suiteXmlFile>src/main/MyTestSuite.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin>

这是我的套件文件 (MyTestSuite.xml):-

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

    <suite name="My Custom suite" parallel="classes">
        <parameter name="driver.type" value="CHROME"/>
        <parameter name="environment" value="DEV"/>
        <parameter name="timeout" value="5000"/>
        <parameter name="maxAttempts" value="20"/>
        <parameter name="logLevel" value="INFO"/>
        <parameter name="driver.quit" value="true"/>
        <suite-files>
            <suite-file path="./Test1.xml"/>
            <suite-file path="./Test2.xml"/>
            <suite-file path="./Test3.xml"/>
            <suite-file path="./Test4.xml"/>
        </suite-files>
    </suite>

这里是单个套件文件之一的示例 (Test1.xml):-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test Suite 1" parallel="classes">
    <test verbose="10" name="Test1" annotations="JDK">
        <classes>
            <class name="com.me.test.Test1" />
        </classes>
    </test>
</suite>

测试成功运行,但是是串行的,而不是并行的。任何建议都将受到欢迎。我可以毫无问题地确保并行运行我的 Cucumber 测试,但到目前为止 TestNG 还没有运气。感谢您的阅读。

最佳答案

您正在使用一套套件。默认情况下,当 TestNG 看到一组套件时,除非另有说明,否则它会按顺序运行它们。

在您的情况下,您可以通过配置 Surefire 插件来触发并行套件执行,如下所示:

首先添加如下属性

<properties>
    <threads>2</threads>
    <file>src/test/resources/MyTestSuite.xml</file>
</properties>

然后定义surefire插件如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
    <configuration>
        <suiteXmlFiles>${file}</suiteXmlFiles>
        <skipTests>false</skipTests>
        <properties>
            <property>
                <name>suitethreadpoolsize</name>
                <value>${threads}</value>
            </property>
        </properties>
    </configuration>
</plugin>

您现在可以使用命令mvn clean test -Dthreads=1来运行测试

有关并行执行和在不使用套件的情况下并行运行套件的更多详细信息,您可以引用我的博文 here .

关于java - 尝试使用 Surefire 从 Maven 启动并行 TestNG 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46448014/

相关文章:

google-app-engine - 使用 google appengine maven 插件部署 Jax-RS war

vb.net - 使用 VB.Net Parallel.ForEach 和 ConcurrentDictionary 的正确语法是什么?

r - 并行计算时函数参数中的变量不会传递给集群

Java:URLConnection 的合理超时时间

java - 使用 IText 在 pdf 文件中进行多重签名

c - C/C++ 单元测试与 Maven 的集成

matlab - 在外部 for 中使用 parfor 的两个循环错误

java - 使用 addAll 在哈希集中添加列表

java - 在 Java 中,通过 getter 引用字段与通过变量引用字段之间是否存在性能差异?

maven - 多模块构建中的 maven-release-plugin 忽略了事件配置文件