jck28-lucio-junit5动态测试的生命周期

动态测试的生命周期方法

  • 在动态测试中,@BeforeEach@AfterEach 生命周期方法会针对每个 @TestFactory 方法执行
  • 不会针对每个动态测试执行。

静态测试用例

  • static Test的每一个参数用例都会执行一次BeforeEachAfterEach
package com.junit5.dynamictest_lifecycle;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

public class StaticTest {

    static int m=0;

    @BeforeAll
    static void beforeAll(){
        System.out.println("BeforeAll");
    }

    @BeforeEach
    void beforeEach(){
        System.out.println("BeforeEach");
        m=8;
    }


    @ParameterizedTest
    @MethodSource
    void test01(int arg){

        System.out.println("m:"+m);//8,8,8
        m+=arg;
        System.out.println("m:"+m);// 14,15,16
        assertThat(arg, is(greaterThan(2)));
    }

    static Stream test01(){
        return Stream.of(6,7,8);
    }

    @Test
    void test02(){
        System.out.println("test02");
    }

    @AfterEach
    void afterEach(){
        System.out.println("AfterEach");
    }



    @AfterAll
    static void afterAll(){
        System.out.println("AfterAll");
    }
}

动态测试用例

  • Dynamic Test的方法只会执行一次BeforeEachAfterEach
    • 默认把BeforeEachAfterEach的生命周期提升为了BeforeAllAfterAll
package com.junit5.dynamictest_lifecycle;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;

public class DynamicTest01 {

    static int m = 0;

    @BeforeAll
    static void beforeAll() {
        System.out.println("BeforeAll");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("BeforeEach");
        m = 8;
    }

    /**
     * BeforeEach和AfterEach是针对@TestFactory运行的,不是针对每个测试用例运行的
     *
     * @return
     */
    @TestFactory
    Stream<DynamicTest> dynamicTestStream() {

        return Stream.of(6, 7, 8)
                .map(arg ->
                        DynamicTest.dynamicTest("test" + arg, () -> {

                            System.out.println("m:" + m);//8,8,8 //8,14,21
                            m += arg;
                            System.out.println("m:" + m);// 14,15,16 //14,21,29
                            assertThat(arg, is(greaterThan(2)));
                        })
                );
    }

    @TestFactory
    Stream<DynamicContainer> dynamicTestStream2() {

        return Stream.of(11, 12, 13)
                .map(arg -> DynamicContainer.dynamicContainer("dynamicContainer",

                        Stream.of(
                                DynamicTest.dynamicTest("test1" + arg, () -> {
                                    System.out.println("1m:" + m);//8,8,8 //8,14,21
                                    m += arg;
                                    System.out.println("1m:" + m);// 14,15,16 //14,21,29
                                    assertThat(arg, is(greaterThan(2)));
                                }),
                                DynamicTest.dynamicTest("test2" + arg, () -> {
                                    System.out.println("2m:" + m);//8,8,8 //8,14,21
                                    m += arg;
                                    System.out.println("2m:" + m);// 14,15,16 //14,21,29
                                    assertThat(arg, is(greaterThan(2)));
                                })
                        ))

                );
    }

    @AfterEach
    void afterEach() {
        System.out.println("AfterEach");
    }


    @AfterAll
    static void afterAll() {
        System.out.println("AfterAll");
    }
}