簡體   English   中英

使用 JUnit 5 並行執行測試用例

[英]Parallel test case execution with JUnit 5

是否可以與 JUnit 5 並行執行測試用例?

我正在從TestNG 中尋找類似threadPoolSizeinvocationCount東西:

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

您可以使用 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

跟進 - 現在可以作為 JUnit 5.3 的實驗性功能使用: https : //junit.org/junit5/docs/snapshot/user-guide/index.html#writing-tests-parallel-execution

在撰寫本文時,並行測試執行是 Junit5 中的一項實驗性功能:

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 除外)的測試類。 在前一種情況下,測試作者必須確保測試類是線程安全的; 在后者中,並發執行可能與配置的執行順序沖突。 因此,在這兩種情況下,只有在測試類或方法上存在 @Execution(CONCURRENT) 注釋時,此類測試類中的測試方法才會並發執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM