使用JUnit 5并行执行测试用例

3htmauhk  于 2023-01-30  发布在  其他
关注(0)|答案(3)|浏览(273)

是否可以与JUnit 5并行执行测试用例?
我在TestNG中查找类似threadPoolSizeinvocationCount的内容:

@Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
enxuqcxy

enxuqcxy1#

您可以使用JUnit 5.3编写并行测试。https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution

@Execution(ExecutionMode.CONCURRENT)
class MyTest {

    @Test
    void test1() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("Test1 " + Thread.currentThread().getName());
    }

    @Test
    void test2() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("Test 2! " + Thread.currentThread().getName());
    }
}

// should run simultaneously

请记住将junit.jupiter.execution.parallel.enabled=true添加到JUnit配置中
https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params
如果您需要fixed thread pool,请将其添加到JUnit配置中:

junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=4
vhipe2zx

vhipe2zx2#

在编写本文时,并行测试执行是Junit 5的一个实验特性:
https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution
更新junit-platform.properties文件:
并行执行顶级类但在同一线程中执行方法的配置参数:

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread
junit.jupiter.execution.parallel.mode.classes.default = concurrent

按顺序执行顶级类但并行执行其方法的配置参数:

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = same_thread

默认执行模式应用于测试树的所有节点,但有几个值得注意的例外,即使用Lifecycle.PER_CLASS模式或MethodOrderer(Random除外)的测试类。在前一种情况下,测试作者必须确保测试类是线程安全的;在后一种情况下,并发执行可能与配置的执行顺序冲突。2因此,在这两种情况下,只有当@Execution(CONCURRENT)注解出现在测试类或方法上时,这些测试类中的测试方法才能并发执行。

emeijp43

emeijp433#

对于Maven:

// JUnit 5
@Execution(ExecutionMode.CONCURRENT)
abstract class BaseTest {
}

// pom.xml
<properties>
    <!-- CLI parameters -->
    <ignore-failure />
    <include-tags />
    <exclude-tags />
    <parallel-enabled />
    <thread-count />
</properties>

<build>
    <plugins>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M8</version>
            <configuration>
                <testFailureIgnore>${ignore-failure}</testFailureIgnore>
                <groups>${include-tags}</groups>
                <excludedGroups>${exclude-tags}</excludedGroups>
                <properties>
                    <configurationParameters>
                        junit.jupiter.execution.parallel.enabled=${parallel-enabled}
                        junit.jupiter.execution.parallel.config.strategy=fixed
                        junit.jupiter.execution.parallel.config.fixed.parallelism=${thread-count}
                        junit.jupiter.execution.parallel.config.fixed.max-pool-size=${thread-count}
                    </configurationParameters>
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>

相关问题