有什么方法可以并行运行JUnit5测试吗? [英] Any way to run JUnit5 tests in parallel?
问题描述
以前,我在测试中使用的是Maven + Selenide + JUnit4,这很好,并行运行效果很好.示例:
Previously I was using Maven+Selenide+JUnit4 for my tests and it was fine, parallel running worked perfectly. Example:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin}</version>
<configuration>
<parallel>all</parallel>
<perCoreThreadCount>true</perCoreThreadCount>
<threadCount>4</threadCount>
<perCoreThreadCount>false</perCoreThreadCount>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
在Jenkins的工作中,我能够运行测试(下面的示例)
And in Jenkins job I was able to run tests (example below)
mvn -Dtest=TestClassName test
我的测试在4种浏览器中运行.
My tests were running in 4 browsers.
在切换到JUnit5之前,因为我想使用按标签运行的测试,例如
Before I switched to JUnit5, because I would like to use running tests by tags, for example
@Test
@Tag("smoke")
public void test1() {}
并运行所有由下一个命令标记为烟雾"的测试:
And run all tests which marked as 'smoke' by next command:
mvn -Dtag=smoke test
但是我遇到了下一个问题:并行执行不起作用,而且我仍然没有找到解决方案. 我发现此错误 https://github.com/junit-team/junit5/issues/1424
But I got next problem: parallel execution does not work and I still did not find solution. I found this bug https://github.com/junit-team/junit5/issues/1424
如何与JUnit5并行运行测试?
How can I run tests in parallel with JUnit5?
我尝试在 pom.xml
<forkCount>2</forkCount>
<reuseForks>true</reuseForks>
<parallel>all</parallel>
这没有帮助,我创建了一个文件 junit-platform.properties 并在其中插入
It did not help, I have created a file junit-platform.properties and insert there
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.config.strategy = fixed
但是无论如何我都无法解决这个问题.
But anyway I was not able to solve this problem.
推荐答案
最后我找到了解决方法.
Finally I found solution.
在Maven + JUnit5上,并行执行仅适用于类(不适用于我在JUnit4中常用的方法)
On Maven+JUnit5 parallel execution will work only by classes (not by methods as I get used to have in JUnit4)
如何实施:
只需将这两个字符串放入您的pom.xml
:
How it can be implemented:
Just put these 2 strings in your pom.xml
:
<forkCount>4</forkCount>
<reuseForks>false</reuseForks>
示例:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<forkCount>4</forkCount>
<reuseForks>false</reuseForks>
<properties>
<includeTags>${tag}</includeTags>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-logger-api</artifactId>
<version>${surefire-logger-api}</version>
</dependency>
</dependencies>
</plugin>
例如,您有3个带有测试的类,因此从控制台运行后,将创建3个浏览器实例(每个类一个),并且在每个类内部,测试将由一致地执行,但类是并行执行的.
For instance you have 3 classes with tests, so after running from console current tests will be created 3 instances of your browser (one for each class) and inside each class tests will be executed by consistently, but classes are executed parallel.
这篇关于有什么方法可以并行运行JUnit5测试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!