如何避免“@RepeatedTest 注解在 JUnit 中运行 BeforeEach 和 AfterEach”?

How to avoid that '@RepeatedTest Annotation runs BeforeEach and AfterEach in JUnit'?

我在使用 JUnit 5 时遇到以下问题。 我想 运行 测试 15 次,所以我使用了注释 @RepeatedTest(15) 并且它起作用了。但问题是,在每个 运行 中它调用 @BeforeEach 方法和 @AfterEach 方法。
它对所有 15 个循环都这样做,但它应该只在第一个 运行 之前调用 @BeforeEach,在最后一个 运行 之后调用 @AfterEach。我想我不能使用 @BeforeAll@AfterAll 因为我有多个测试,所以这只会在测试 1 和测试 50 之前被调用。

目前运行如何:

@BeforeAll Method
Test 1:
    - @BeforeEach Method
    - Run1
    - @AfterEach Method
    - @BeforeEach Method
    - Run2
    - @AfterEach Method
Test 2:
    - @BeforeEach Method
    - Run1
    - @AfterEach Method
    - @BeforeEach Method
    - Run2
    - @AfterEach Method
@AfterAll Method


应该如何 运行:

@BeforeAll Method
Test 1:
    - @BeforeEach Method
    - Run1
    - Run2
    - @AfterEach Method
Test 2:
    - @BeforeEach Method
    - Run1
    - Run2
    - @AfterEach Method
@AfterAll Method

好的,我找到了解决方案! 我不知道为什么,但是对于每个测试 Class @BeforeAll@AfterAll 都会被调用。 所以我删除了 @BeforeEach@AfterEach 方法并将它们的代码移动到 @BeforeAll@AfterAll.

现在他们 运行 是这样的:

@BeforeAll Method
Test 1:
    - Run1
    - Run2
@AfterAll Method
@BeforeAll Method
Test 2:
    - Run1
    - Run2
@AfterAll Method

我得到了一个 .jtl 文件的预期输出,其中包含每个测试用例 15 次运行的数据。

我在这里看到四个选项:

  1. 为每个场景创建一个新测试 class,每个场景使用一种 @RepeatedTest 方法,并使用 @BeforeAll@AfterAll 进行测量。

  2. 不要使用 @RepeatedTest,但在正常的 @Test 内进行重复和测量。

  3. 让 JUnit 在需要决定执行的地方注入 RepetitionInfo

    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.RepeatedTest;
    import org.junit.jupiter.api.RepetitionInfo;
    import org.junit.jupiter.api.TestInfo;
    
    class RepeatTest {
    
        @BeforeEach
        void setUp(RepetitionInfo repetitionInfo, TestInfo testInfo) {
            if (repetitionInfo.getCurrentRepetition() == 1) {
                System.out.println("Before repetition of " + testInfo.getDisplayName());
            }
        }
    
        @RepeatedTest(value = 3, name = "{displayName}_{currentRepetition}of{totalRepetitions}") @DisplayName("test1")
        void test1(TestInfo testInfo) {
            System.out.println(testInfo.getDisplayName());
        }
    
        @RepeatedTest(value = 5, name = "{displayName}_{currentRepetition}of{totalRepetitions}") @DisplayName("test2")
        void test2(TestInfo testInfo) {
            System.out.println(testInfo.getDisplayName());
        }
    
        @AfterEach
        void tearDown(RepetitionInfo repetitionInfo, TestInfo testInfo) {
            if (repetitionInfo.getCurrentRepetition() == repetitionInfo.getTotalRepetitions()) {
                System.out.println("After repetition of " + testInfo.getDisplayName());
            }
        }
    }
    

    输出:

    Before repetition of test1_1of3
    test1_1of3
    test1_2of3
    test1_3of3
    After repetition of test1_3of3
    Before repetition of test2_1of5
    test2_1of5
    test2_2of5
    test2_3of5
    test2_4of5
    test2_5of5
    After repetition of test2_5of5
    
  4. 如果您有很多这样的测试,请考虑实现一个小型 JUnit 扩展。也许引入一些新的方法注释。