我正在尝试使用 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/