redis注解,JUnit5 @RepeatedTest注解示例

 2023-10-06 阅读 29 评论 0

摘要:通過 JUnit5 @RepeatedTest注解,可以編寫可以多次運行的可重復測試模板。 頻率可以配置為@RepeatedTest注解的參數。 1. @RepeatedTest注解用法 redis注解。要創建可重復的測試模板方法,請使用@RepeatedTest注解該方法。 @DisplayName("

通過 JUnit5 @RepeatedTest注解,可以編寫可以多次運行的可重復測試模板。 頻率可以配置為@RepeatedTest注解的參數。

1. @RepeatedTest注解用法

redis注解。要創建可重復的測試模板方法,請使用@RepeatedTest注解該方法。

@DisplayName("Add operation test")
@RepeatedTest(5)
void addNumber(TestInfo testInfo) {Calculator calculator = new Calculator();Assertions.assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
}

在上面的代碼中,addNumber()測試將重復 5 次。

請注意,每次重復測試的行為都類似于常規@Test方法的執行,并且完全支持相同的生命周期回調和擴展。 這意味著對于每個單獨的調用,將在適合它們的測試生命周期的地方調用@BeforeEach和@AfterEach帶注解的方法。

package com.howtodoinjava.junit5.examples;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
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.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;@RunWith(JUnitPlatform.class)
public class RepeatedTestExample {@BeforeAllpublic static void init(){System.out.println("Before All init() method called");}@BeforeEachpublic void initEach(){System.out.println("Before Each initEach() method called");}@DisplayName("Add operation test")@RepeatedTest(5)void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) {System.out.println("Running addNumber test -> " + repetitionInfo.getCurrentRepetition());Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");}@AfterEachpublic void cleanUpEach(){System.out.println("After Each cleanUpEach() method called");}@AfterAllpublic static void cleanUp(){System.out.println("After All cleanUp() method called");}
}

Spring注解。上面程序的輸出:

Before All init() method calledBefore Each initEach() method called
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 1
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 2
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 3
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 4
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 5
After Each cleanUpEach() method calledAfter All cleanUp() method called

2. 自定義顯示測試名稱

除了指定重復次數之外,您還可以為每個重復指定自定義顯示名稱。此自定義顯示名稱可以是靜態文本+動態占位符的組合。 當前,支持 3 個占位符:

  • {displayName}:@RepeatedTest方法的顯示名稱。
  • {currentRepetition}:當前重復計數。
  • {totalRepetitions}:重復總數。
@RunWith(JUnitPlatform.class)
public class JUnit5AnnotationsExample 
{@DisplayName("Add operation test")@RepeatedTest(value = 5, name = "{displayName} - repetition {currentRepetition} of {totalRepetitions}")void addNumber(TestInfo testInfo) {Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");}
}

junit怎么用,運行以上測試將在下面輸出:

在這里插入圖片描述
JUnit5 重復測試的顯示名稱

您可以使用兩種預定義格式之一,即RepeatedTest.LONG_DISPLAY_NAMERepeatedTest.SHORT_DISPLAY_NAME。 如果未指定,則SHORT_DISPLAY_NAME是默認格式。

  • RepeatedTest.LONG_DISPLAY_NAME – {displayName} :: repetition {currentRepetition} of {totalRepetitions}
  • RepeatedTest.SHORT_DISPLAY_NAME – repetition {currentRepetition} of {totalRepetitions}
@DisplayName("Add operation test")
@RepeatedTest(value = 5, name = RepeatedTest.LONG_DISPLAY_NAME)
void addNumber(TestInfo testInfo) {Assertions.assertEquals(2, Calculator .add(1, 1), "1 + 1 should equal 2");
}

3. RepetitionInfo接口

RepetitionInfo用于將有關重復測試的當前重復的信息注入@RepeatedTest,@BeforeEach和@AfterEach方法中。

@RunWith(JUnitPlatform.class)
public class JUnit5AnnotationsExample {@BeforeEachpublic void initEach(RepetitionInfo info){int currentRepetition = info.getCurrentRepetition();int totalRepetitions = info.getTotalRepetitions();//Use information as needed}@DisplayName("Add operation test")@RepeatedTest(value = 5, name="{displayName} :: repetition {currentRepetition} of {totalRepetitions}")void addNumber(TestInfo testInfo) {Calculator calculator = new Calculator();Assertions.assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");}@AfterEachpublic void cleanUpEach(RepetitionInfo info){int currentRepetition = info.getCurrentRepetition();int totalRepetitions = info.getTotalRepetitions();//Use information as needed}
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/120975.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息