<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # TestNG – 測試組,元組,默認組示例 > 原文: [https://howtodoinjava.com/testng/testng-test-groups-meta-group-default-group-examples/](https://howtodoinjava.com/testng/testng-test-groups-meta-group-default-group-examples/) **分組測試方法**是 TestNG 的最重要功能之一。 在 TestNG 中,用戶可以將多種測試方法分組為一個命名組。 您還可以執行屬于一個或多個組的一組特定的測試方法。 此功能允許將測試方法分為不同的部分或模塊。 例如,您可以擁有一組屬于健全性測試的測試,而其他測試則可能屬于回歸測試。 您還可以**根據測試方法驗證的功能/特征**分離測試。 這有助于在需要時僅執行一組特定的測試。 在本教程中,我們將在以下步驟/部分中了解有關 TestNG 中測試分組的信息。 ```java Table of Contents Grouping tests example Running a TestNG group through Eclipse Running a TestNG group through testng.xml Writing tests which belong to multiple groups Including and excluding groups Using regular expressions Default group Group of groups ``` ## 分組測試示例 讓我們創建一個測試類,其中包含屬于某個組的某些測試方法。 ```java package com.howtodoinjava.groupExamples; import org.testng.annotations.Test; public class TestGroupExample { @Test(groups = { "test-group" }) public void testMethodOne() { System.out.println("Test method one belonging to group."); } @Test public void testMethodTwo() { System.out.println("Test method two not belonging to group."); } @Test(groups = { "test-group" }) public void testMethodThree() { System.out.println("Test method three belonging to group."); } } ``` 如果您將在 eclipse 中正常運行上述測試,則??測試執行將不考慮要執行的組,因此將執行指定測試類中的所有測試。 如果只想在某個特定組下執行方法,則將以以下兩節中討論的任一種方式執行它們。 ## 通過 Eclipse 運行 TestNG 組 在前面的部分中,我們創建了一個測試類,其中包含屬于測試組的某些測試方法。 現在,讓我們使用 Eclipse 運行測試組。 1)**轉到“運行 | 運行配置”** 2)**從可用配置列表中選擇 TestNG,然后單擊“新的配置”圖標**。 3)在新的配置窗口中,**提供配置名稱**,例如`TestGroupExample`。 4)轉到項目部分,然后單擊“瀏覽”按鈕。 **選擇先前創建的項目**,即`TestNGExamples`。 ![Select group in eclipse](https://img.kancloud.cn/b7/86/b7864bc02326a67151b6479aabce4ca8_929x586.png) 5)轉到“分組”部分,然后單擊“瀏覽”按鈕。 **從列表中選擇要執行的組**,在這種情況下為測試組。 ![Browse Group Name](https://img.kancloud.cn/2d/68/2d687d3aa158b79fa5946d1ed388d568_605x274.png) 6)**單擊“應用”按鈕,然后單擊“運行”**。 以下結果將顯示在 Eclipse 的 TestNG 的“結果”窗口中: ```java Test method one belonging to group. Test method three belonging to group. PASSED: testMethodOne PASSED: testMethodThree =============================================== GRP-test-group Tests run: 2, Failures: 0, Skips: 0 =============================================== ``` 恭喜,您已經使用 Eclipse 中的 TestNG 運行器配置成功執行了屬于特定組的測試方法。 您還可以通過在“瀏覽”部分中選擇相應的組來使用該工具執行多個組。 通常,最好使用基于 TestNG-XML 的執行來執行屬于特定組的測試方法。 ## 通過`testng.xml`運行 TestNG 組 現在讓我們學習如何創建一個 testng XML 文件來執行屬于特定組的測試方法。 **此方法是執行組的首選且簡便的方法。 而且,這些 testng XML 文件然后可以與構建工具一起使用以執行 TestNG 測試套件。** 1)打開 Eclipse 并在先前創建的項目中創建一個名稱為`testng.xml`的新文件。 2)在上述文件中添加以下代碼: ```java <suite name="Time test Suite" verbose="1"> <test name="Group Test"> <groups> <run> <include name="test-group" /> </run> </groups> <classes> <class name="com.howtodoinjava.groupExamples.TestGroupExample" /> </classes> </test> </suite> ``` 該 xml 文件在套件中僅包含一個測試。 它包含通過使用`groups`標記定義的`groups`部分,如代碼所示。 運行標簽表示需要運行的組。 `include`標記代表需要執行的組的名稱。 3)選擇先前創建的 testng XML 文件并將其作為 TestNG 套件運行。 您將看到以下測試結果: ```java Test method one belonging to group. Test method three belonging to group. =============================================== Time test Suite Total tests run: 2, Failures: 0, Skips: 0 =============================================== ``` 很棒。 我們成功創建了一個 testng XML 文件,該文件中包含一個組,從而在上述套件中創建了一個測試。 這是通過將上述組包含在運行部分中來完成的。 運行部分又是測試內部`groups`標簽部分的一部分。 TestNG 將在測試的類部分中提到的類下查找屬于該組的測試方法。 用戶還可以提供測試包。 TestNG 將搜索添加到測試中的所有類,以包括或排除屬于特定組的特定測試方法。 一旦找到,這些測試方法將由 TestNG 作為測試套件執行。 ## 編寫屬于多個組的測試 之前我們了解了創建屬于單個組的測試的方法,但是 TestNG 允許**測試方法也屬于多個組**。 這可以通過在`@Test`注解的`groups`屬性中以數組形式提供組名稱來完成。 讓我們創建一個包含多個小組的示例程序,以了解其操作方法。 ```java package com.howtodoinjava.groupExamples; import org.testng.annotations.Test; public class MultiGroupExample { @Test(groups = { "group-one" }) public void testMethodOne() { System.out.println("Test method one belonging to group."); } @Test(groups = { "group-one", "group-two" }) public void testMethodTwo() { System.out.println("Test method two belonging to both group."); } @Test(groups = { "group-two" }) public void testMethodThree() { System.out.println("Test method three belonging to group."); } } ``` 上一類包含三種測試方法。 其中兩種測試方法分別屬于一組,其中一種方法屬于兩組,分別是第一組和第二組。 現在,編輯`testng.xml`文件,如下所示: ```java <suite name="Multi Group Suite" verbose="1"> <test name="Group Test one"> <groups> <run> <include name="group-one" /> </run> </groups> <classes> <class name="com.howtodoinjava.groupExamples.MultiGroupExample" /> </classes> </test> <test name="Group Test two"> <groups> <run> <include name="group-two" /> </run> </groups> <classes> <class name="com.howtodoinjava.groupExamples.MultiGroupExample" /> </classes> </test> </suite> ``` 前面的 testng XML 套件包含兩個測試,每個測試都執行屬于特定組的測試方法。 選擇 testng XML 文件并將其作為 TestNG 套件運行。 您將看到以下測試結果: ```java Test method one belonging to group. Test method two belonging to both group. Test method three belonging to group. Test method two belonging to both group. =============================================== Multi Group Suite Total tests run: 4, Failures: 0, Skips: 0 =============================================== ``` 在這里,我們成功創建了一個測試方法,該方法屬于多個組并且可以成功執行。 如您在先前的測試結果中看到的,在測試套件的兩個測試中都執行了`testMethodTwo()`。 這是因為它屬于兩個由 TestNG 執行測試方法的組。 ## 包括和排除組 TestNG 還允許您**從測試執行**中包括和排除某些組。 這有助于僅執行一組特定的測試,而排除某些測試。 一個簡單的例子是某個功能損壞時,您需要從執行中排除一組固定的測試,因為這些測試將在執行時失敗。 修復功能之后,您可以通過執行相應的測試組來驗證功能。 讓我們創建一個示例程序,并學習如何排除一組測試。 ```java package com.howtodoinjava.groupExamples; import org.testng.annotations.Test; public class ExcludeGroupTest { @Test(groups = { "include-group" }) public void testMethodOne() { System.out.println("Test method one belonging to include group."); } @Test(groups = { "include-group" }) public void testMethodTwo() { System.out.println("Test method two belonging to include group."); } @Test(groups = { "include-group", "exclude-group" }) public void testMethodThree() { System.out.println("Test method three belonging to exclude/include groups."); } } ``` 上一類包含三種測試方法,這些方法在執行時將消息打印到控制臺上。 這三種方法都屬于組`include-group`,而`testMethodThree()`方法也屬于組`exclude-group`。 ```java <suite name="Exclude Group Suite" verbose="1"> <test name="Exclude Group Test"> <groups> <run> <include name="include-group" /> <exclude name="exclude-group" /> </run> </groups> <classes> <class name="com.howtodoinjava.groupExamples.ExcludeGroupTest" /> </classes> </test> </suite> ``` 現在在`testng.xml`文件上方運行,它將產生以下結果。 ```java Test method one belonging to include group. Test method two belonging to include group. =============================================== Exclude Group Suite Total tests run: 2, Failures: 0, Skips: 0 =============================================== ``` 從先前的測試結果中可以看出,TestNG 執行了**組`include-group`中的兩種方法**,并排除了屬于**組`exclude-group`的第三種方法**,它從測試執行中排除。 如果測試方法既屬于包含組又屬于排除組,則排除組具有優先權,并且該測試方法將從測試執行中排除。 ## 使用正則表達式 在將測試配置為包含或排除組時,TestNG 允許用戶使用[**正則表達式**](//howtodoinjava.com/java-regular-expression-tutorials/ "Java Regular Expression Tutorial")。 這類似于包含和排除我們前面介紹的測試方法。 這可以幫助用戶根據名稱搜索包含和排除組。 讓我們學習如何排除基于基于正則表達式的名稱匹配的測試。 ```java package com.howtodoinjava.groupExamples; import org.testng.annotations.Test; public class RegularExpressionGroupTest { @Test(groups = { "include-test-one" }) public void testMethodOne() { System.out.println("Test method one"); } @Test(groups = { "include-test-two" }) public void testMethodTwo() { System.out.println("Test method two"); } @Test(groups = { "test-one-exclude" }) public void testMethodThree() { System.out.println("Test method three"); } @Test(groups = { "test-two-exclude" }) public void testMethodFour() { System.out.println("Test method Four"); } } ``` 和`testng.xml`文件。 ```java <suite name="Regular Exp. Group Suite" verbose="1"> <test name="Regular Exp. Test"> <groups> <run> <include name="include.*" /> <exclude name=".*exclude" /> </run> </groups> <classes> <class name="com.howtodoinjava.groupExamples.RegularExpressionGroupTest" /> </classes> </test> </suite> ``` 前面的 XML 包含一個簡單的測試,其中包含名稱以`include`開頭的所有組,而名稱以`exclude`結尾的所有組都從測試執行中排除。 現在運行`testng.xml`文件,您將在控制臺中獲得以下結果。 ```java Test method one Test method two =============================================== Regular Exp. Group Suite Total tests run: 2, Failures: 0, Skips: 0 =============================================== ``` 在此,TestNG 執行了兩個名稱以`include`開頭的組的方法,并排除了名稱以`exclude`結尾的組的測試方法。 要使用正則表達式包含和排除組,必須使用`.*`來匹配名稱。 通過在搜索字符串的開頭和結尾使用表達式(例如,`.*name.*`),我們也可以將其用于搜索名稱中包含某個字符串的組。 ## 分配默認組 有時我們可能需要**將默認組**分配給屬于一個類的一組測試方法。 這樣,屬于所述類的所有公共方法將自動成為 TestNG 測試方法,并成為所述組的一部分。 這可以通過在類級別使用`@Test`注解并在所述`@Test`注解中定義默認組來實現。 ```java @Test(groups={"default-group"}) public class DefaultGroup { public void testMethodOne(){ System.out.println("Test method one."); } public void testMethodTwo(){ System.out.println("Test method two."); } @Test(groups={"test-group"}) public void testMethodThree(){ System.out.println("Test method three."); } } ``` ## 組中組或“元組” TestNG 允許用戶從現有組中創建組,然后在創建測試套件時使用它們。 **您可以通過包含和排除某些組來創建新組,然后使用它們。** 讓我們創建一個示例測試程序,并學習如何創建稱為**元組**的組中組。 ```java package com.howtodoinjava.groupExamples; import org.testng.annotations.Test; public class RegularExpressionGroupTest { @Test(groups = { "include-test-one" }) public void testMethodOne() { System.out.println("Test method one"); } @Test(groups = { "include-test-two" }) public void testMethodTwo() { System.out.println("Test method two"); } @Test(groups = { "test-one-exclude" }) public void testMethodThree() { System.out.println("Test method three"); } @Test(groups = { "test-two-exclude" }) public void testMethodFour() { System.out.println("Test method Four"); } } ``` 現在創建`testng.xml`文件,如下所示: ```java <suite name="Group of group Suite" verbose="1"> <test name="Group of group Test"> <groups> <define name="include-group"> <include name="include-test-one" /> <include name="include-test-two" /> </define> <define name="exclude-group"> <include name="test-one-exclude" /> <include name="test-two-exclude" /> </define> <run> <include name="include-group" /> <exclude name="exclude-group" /> </run> </groups> <classes> <class name="com.howtodoinjava.groupExamples.RegularExpressionGroupTest" /> </classes> </test> </suite> ``` 這里在測試內部定義了兩組,然后將這些組用于測試執行。 使用`groups`標簽內的`define`標簽創建元組。 使用`define`標簽下的`name`屬性定義新組的名稱。 通過使用`include`和`exclude`標簽從新組中排除組。 現在運行`testng.xml`測試,它將在控制臺中產生以下結果: ```java Test method one Test method two =============================================== Group of group Suite Total tests run: 2, Failures: 0, Skips: 0 =============================================== ``` 在這里,testNG 僅執行兩種方法,如在`include-group`組中提到的,并排除了屬于`exclude-group`的測試方法。 您可以根據需要定義任意多個組。 此功能有助于為回歸,健全性和模塊測試創建特定的組。 這些都與 TestNG 中的測試組有關。 讓我知道您是否有任何疑問。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看