<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # JUnit5 `@RepeatedTest`注解示例 > 原文: [https://howtodoinjava.com/junit5/repeated-test-annotation-example/](https://howtodoinjava.com/junit5/repeated-test-annotation-example/) 通過 JUnit5 [`@RepeatedTest`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/RepeatedTest.html)注解,可以編寫可以多次運行的**可重復測試模板**。 頻率可以配置為`@RepeatedTest`注解的參數。 ## 1\. `@RepeatedTest`注解用法 要創建可重復的測試模板方法,請使用`@RepeatedTest`注解該方法。 ```java @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`](//howtodoinjava.com/junit-5/before-each-annotation-example/)和`@AfterEach`帶注解的方法。 ```java 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 { @BeforeAll public static void init(){ System.out.println("Before All init() method called"); } @BeforeEach public 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"); } @AfterEach public void cleanUpEach(){ System.out.println("After Each cleanUpEach() method called"); } @AfterAll public static void cleanUp(){ System.out.println("After All cleanUp() method called"); } } ``` 上面程序的輸出: ```java Before All init() method called Before Each initEach() method called After Each cleanUpEach() method called Before Each initEach() method called Running addNumber test -> 1 After Each cleanUpEach() method called Before Each initEach() method called Running addNumber test -> 2 After Each cleanUpEach() method called Before Each initEach() method called Running addNumber test -> 3 After Each cleanUpEach() method called Before Each initEach() method called Running addNumber test -> 4 After Each cleanUpEach() method called Before Each initEach() method called Running addNumber test -> 5 After Each cleanUpEach() method called After All cleanUp() method called ``` ## 2\. 自定義顯示測試名稱 除了指定重復次數之外,您還可以**為每個重復**指定自定義顯示名稱。 此自定義顯示名稱可以是*靜態文本+動態占位符*的組合。 當前,支持 3 個占位符: * `{displayName}`:`@RepeatedTest`方法的顯示名稱。 * `{currentRepetition}`:當前重復計數。 * `{totalRepetitions}`:重復總數。 ```java @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"); } } ``` 運行以上測試將在下面輸出: ![JUnit5 Repeated Test Display Names](https://img.kancloud.cn/95/12/9512231a272438bcf3b6b4add90e50da_545x189.png) JUnit5 重復測試的顯示名稱 您可以使用兩種預定義格式之一,即`RepeatedTest.LONG_DISPLAY_NAME`和`RepeatedTest.SHORT_DISPLAY_NAME`。 如果未指定,則`SHORT_DISPLAY_NAME`是默認格式。 * `RepeatedTest.LONG_DISPLAY_NAME` – `{displayName} :: repetition {currentRepetition} of {totalRepetitions}` * `RepeatedTest.SHORT_DISPLAY_NAME` – `repetition {currentRepetition} of {totalRepetitions}` ```java @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`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/RepetitionInfo.html)用于將有關重復測試的當前重復的信息注入`@RepeatedTest`,`@BeforeEach`和`@AfterEach`方法中。 ```java @RunWith(JUnitPlatform.class) public class JUnit5AnnotationsExample { @BeforeEach public 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"); } @AfterEach public void cleanUpEach(RepetitionInfo info){ int currentRepetition = info.getCurrentRepetition(); int totalRepetitions = info.getTotalRepetitions(); //Use information as needed } } ``` 將我的問題放在評論部分。 學習愉快! [源碼下載](https://github.com/lokeshgupta1981/Junit5Examples/tree/master/JUnit5Examples)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看