<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Junit 參數化測試 – `@Theory`和`@DataPoints` > 原文: [https://howtodoinjava.com/junit/junit-parameterized-testcases-with-theory-and-datapoints/](https://howtodoinjava.com/junit/junit-parameterized-testcases-with-theory-and-datapoints/) 在我之前關于該主題的文章中,介紹了如何編寫帶有`@Parameters`注解的**參數化測試用例**。 如果我選擇了正確的單詞,那么這種方法會很混亂并且不太可讀。 不必要地需要大量關注。 嗯,還有另一種方法,您可以借助`@Theory`和`@DataPoints`等注解在 Junit 中編寫參數化測試用例。 我將以以前的帖子為例,并將其轉換為新方法。 這是有道理的,因為在此之后我們將能夠比較哪些變化以及與以前的方法有何不同。 ## 1)使用`@DataPoints`饋送輸入數據 在此,僅注解已從`@Parameters`更改為`@DataPoints`。 其余的概念是相同的。 以前,提供輸入的方法是: ```java @Parameters(name = "Run #Square of : {0}^2={1}") public static Iterable<Object []> data() { return Arrays.asList(new Object[][] { { 1, 1 }, { 2, 4 }, { 3, 19 }, { 4, 16 }, { 5, 25 } }); } ``` 現在它是: ```java @DataPoints public static int[][] integers() { return new int[][]{{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {}}; } ``` 請注意,您可以使用`@DataPoint`注解分別編寫輸入。 ```java @DataPoint public static int[] input6 = new int[]{6, 36}; @DataPoint public static int[] input7 = new int[]{7, 49}; ``` 我將返回類型從“`Iterable<object[]>`”更改為“`int[][]`”,因為這些輸入饋送到測試用例的方式略有不同。 您將在下一部分中看到不同之處。 ## 2)用`@Theory`編寫測試用例 **從結構上講,基于理論的類比參數化測試類簡單**。 類聲明應使用`@RunWith(Theories.class)`進行注解,并且必須提供兩個實體: 1. 生成并返回測試數據的數據方法 2. 一個理論 數據方法必須使用`@DataPoints`進行注解,每個理論都必須使用`@Theory`進行注解。 與普通的單元測試一樣,每個理論都應至少包含一個斷言。 在以前的方法中,我們編寫了如下的測試用例: ```java @Test public void testUserMapping() { // You can use here assert also Assert.assertEquals(resultExpected, MathUtils.square(input)); } ``` 其中`input`和`resultExpected`被聲明為類成員,并使用參數化構造器進行填充。 如您所見,上面的`testUserMapping()`方法沒有任何參數。 在新方法中,測試使用`@Theory`注解進行注解。 例如: ```java @Theory public void testSquares(final int[] inputs) { Assume.assumeTrue(inputs[0] > 0 && inputs[1] > 0); Assert.assertEquals(inputs[1], MathUtils.square(inputs[0])); } ``` 您會看到參數現在已成為測試用例的一部分,這是概念的最佳組成部分。 假定`True()`確保參數為正數,并且`assertEquals()`檢查我們需要測試的函數邏輯。 要調整上述測試用例,請按以下方式用`@RunWith`注解類。 ```java @RunWith(Theories.class) public class JunitTestsWithParameters { //Testcases } ``` 如果您認為某些測試用例在執行操作時可能會引發異常,請使用`@Rule`注解和`ExpectedException`類對其進行處理。 下面給出一個更完整的工作示例: ```java package test.junit.theory; import org.junit.Assert; import org.junit.Assume; import org.junit.Rule; import org.junit.experimental.theories.DataPoint; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; @RunWith(Theories.class) public class JunitTestsWithParameters { @Rule public ExpectedException expectedException = ExpectedException.none(); @DataPoints public static int[][] integers() { return new int[][]{{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {}}; } @DataPoint public static int[] input6 = new int[]{6, 36}; @DataPoint public static int[] input7 = new int[]{7, 49}; @Theory public void testSquares(final int[] inputs) { Assume.assumeTrue(inputs.length == 2); Assume.assumeTrue(inputs[0] > 0 && inputs[1] > 0); Assert.assertEquals(inputs[1], MathUtils.square(inputs[0])); } @Theory public void testBlankArrays(final int[] inputs) { Assume.assumeTrue(inputs.length == 0); expectedException.expect(ArrayIndexOutOfBoundsException.class); Assert.assertEquals(inputs[1], MathUtils.square(inputs[0])); } } ``` 運行上述測試用例,結果將如下所示: ![Junit Theory Example Output](https://img.kancloud.cn/e4/bd/e4bdd183eee7b3e1884e53ffc1f148f6_721x150.png) Junit 理論示例輸出 > 請注意,將測試數據從測試/理論實現中分離出來,除了簡潔以外,還可以帶來另一個積極影響:您可能會開始考慮獨立于要測試的實際內容的測試數據。 但是同時,您應該已經注意到,沒有辦法將特定結果與特定數據點配對。 當您可以以斷言的形式表達數據點與預期結果之間的一般關系時,以及當該關系對于所有數據都成立時,應該使用理論。 因此,**應在適當考慮的情況下**在理論和參數化測試用例之間謹慎選擇。 它們不是參數化測試用例的精確替代,而是它們的補充。 **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看