簡體   English   中英

Junit5 參數化測試生命周期。 為什么 @BeforeAll 和 static 塊在所有測試之后運行

[英]Junit5 Parameterized Test LifeCycle. why @BeforeAll and static block run after all the tests

我在下面發布了一個簡單的代碼,為什么 @BeforeAll 注釋方法和靜態塊在參數化測試之后運行? 在這種情況下,如何在 Junit5 中加載參數化測試或利用 @BeforeAll 或靜態塊的功能之前注冊公共對象或數據庫連接。 (僅供參考:Junit4 中參數化測試的等效代碼在所有測試之前運行靜態塊。但不是 @BeforeClass 注釋方法。)

package com.something;

import static org.junit.jupiter.api.Assertions.*;

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

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;


public class Example {
    MathUtils utils;

    static{
        System.out.println("Static Block------");
    }

    @BeforeAll
    static void beforeAllInit(){
        System.out.println("This will run Before all tests");
    }

    @BeforeEach //fresh instance before each test method
    void Init(){
        utils = new MathUtils();
        System.out.println("utils init()");
    }

    @AfterEach
    void cleanup(){
        System.out.println("Cleaning Up...");
    }

    //@Test
    @ParameterizedTest
    @CsvSource(value={"1,2,3","10,10,20","5,9,14"})
    void testAdd(int num1, int num2, int exp){
        //MathUtils utils = new MathUtils();
        //int exp = 3;
        int actual = utils.add(num1,num2);
        assertEquals(exp,actual,"Adding two numbers");
    }

    @ParameterizedTest
    @MethodSource("createDataCollection")
    void testMethod(ReadJson rj) {
        assertNotNull(rj);
    }

    public Stream<ReadJson> createDataCollection() {
        //beforeAllInit();

        final List<ReadJson> testInputs = new ArrayList<>();

        testInputs.add(new ReadJson("one","something","Miami","ABC"));
        testInputs.add(new ReadJson("two","something","New York","ABC"));
        testInputs.add(new ReadJson("three","something","Redlands","ABC"));

        return testInputs.stream();
    }

}

輸出

實用程序初始化()

打掃干凈...

實用程序初始化()

打掃干凈...

實用程序初始化()

打掃干凈...

靜態塊-----

這將在所有測試之前運行

進程以退出代碼 0 結束

無法重現。 以下測試文件:

package com.example;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

class JUnitTests {

  static {
    System.out.println("Static initializer...");
  }

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

  @BeforeEach
  void beforeEach() {
    System.out.println("Before each...");
  }

  @ParameterizedTest
  @CsvSource(value = {"5,5,10", "2,3,5"})
  void parameterizedTest(int x, int y, int r) {
    System.out.printf("Parameterized test (%d, %d, %d)...%n", x, y, r);
    assertEquals(r, x + y, () -> String.format("%d + %d expected to equal %d", x, y, r));
  }

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

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

使用以下 Gradle 構建腳本編譯並執行:

plugins {
    java
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
}

tasks.test {
    useJUnitPlatform()
    testLogging {
        showStandardStreams = true
    }
}

給出以下輸出:

> Task :test

JUnitTests STANDARD_OUT
    Static initializer...
    Before all...

JUnitTests > parameterizedTest(int, int, int) > com.example.JUnitTests.parameterizedTest(int, int, int)[1] STANDARD_OUT
    Before each...
    Parameterized test (5, 5, 10)...
    After each...

JUnitTests > parameterizedTest(int, int, int) > com.example.JUnitTests.parameterizedTest(int, int, int)[2] STANDARD_OUT
    Before each...
    Parameterized test (2, 3, 5)...
    After each...

JUnitTests STANDARD_OUT
    After all...

BUILD SUCCESSFUL in 3s
3 actionable tasks: 3 executed

如您所見,一切都按預期的順序執行。

暫無
暫無

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

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