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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # TestNG 與 JUnit:有什么區別? > 原文: [https://www.guru99.com/junit-vs-testng.html](https://www.guru99.com/junit-vs-testng.html) Testng 和 Junit 均為[測試](/software-testing.html)框架,用于[單元測試](/unit-testing-guide.html)。 TestNG 與 JUnit 相似。 幾乎沒有其他功能可以使 TestNG 比 JUnit 強大。 本教程主要側重于分析 JUnit 和 TestNG 的功能。 它可以幫助開發人員確定用于單元測試的框架。 首先讓我們分析一下 TestNG 和 JUnit4 之間的相似之處。 TestNG 是一個受 JUnit 和 NUnit 啟發的測試框架。 下表顯示了 JUnit 和 TestNG 支持的功能。 ## JUnit4 和 TestNG 功能比較 除了一個或兩個功能外,TestNG 和 JUnit4 看起來都很相似。 讓我們對兩者進行比較以快速確定哪種技術更適合單元測試。 下表突出顯示了兩者均支持的功能: ![JUnit Vs TestNG](https://img.kancloud.cn/73/61/736179737fefc6fe13774b815afc51ba_541x225.png "JUnit Vs TestNG") ## 注解 JUnit 和 TestNG 都使用注釋,并且幾乎所有注釋看起來都相似。 TestNG 使用@BeforeMethod,@ AfterMethod 類似于 JUnit4 中的@Before,@ After。 TestNG 和 Junit4 都使用@Test(timeout = 1000)進行超時。有關更多詳細信息,請查看下表- | **S.N.** | **說明** | **TestNG** | **JUnit 4** | | 1 | 測試注解 | @測試 | @Test | | 2 | 在當前類中調用第一個測試方法之前執行 | @課前 | @BeforeClass | | 3 | 在當前類中的所有測試方法之后執行 | @下課以后 | @AfterClass | | 4 | 在每種測試方法之前執行 | @BeforeMethod | @之前 | | 5 | 在每種測試方法之后執行 | @AfterMethod | @后 | | 6 | 注解忽略測試 | @Test(啟用=假) | @忽視 | | 7 | 異常注釋 | @Test(expectedExceptions = ArithmeticException.class) | @Test(expected = ArithmeticException.class) | | 8 | 超時 | @Test(超時= 1000) | @Test(timeout = 1000) | | 9 | 在套件中的所有測試之前執行 | @BeforeSuite | 不適用 | | 10 | 在套件中的所有測試之后執行 | @AfterSuite | n/a | | 11 | 在測試運行之前執行 | @BeforeTest | n/a | | 12 | 在測試運行后執行 | @AfterTest | n/a | | 13 | 在調用屬于任何這些組的第一個測試方法之前執行 | @BeforeGroups | n/a | | 14 | 在屬于這里任何組的最后一個測試方法之后運行 | @AfterGroups | n/a | ## 套件測試 套件用于一起執行多個測試。 套件可以使用 TestNG 和 JUnit4 創建。 但是,套件在 TestNG 中功能更強大,因為它使用非常不同的方法來執行測試。 讓我們使用下面給出的代碼片段來了解它: **使用 JUnit4** 下面的類描述了在使用 JUnit4 時套件的使用: ``` package guru99.junit; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ SuiteTest1.class, SuiteTest2.class, }) public class JunitTest { // This class remains empty,it is used only as a holder for the above annotations } ``` **使用 TestNG** TestNG 使用 xml 將所有測試捆綁在一個地方。下面的 xml 描述了使用 TestNG 時套件的使用: ``` <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="My test suite"> <test name="testing"> <classes> <class name="com.guru99.SuiteTest1" /> <class name="com.guru99.SuiteTest2" /> </classes> </test> </suite> ``` ## 忽略測試 兩者都可以跳過測試,下面用代碼示例來看一下: **Using JUnit4** 下面的代碼片段描述了在使用 JUnit4 時使用@ignore 批注: ``` @Ignore public void method1() { System.out.println("Using @Ignore , this execution is ignored"); } ``` **Using TestNG** 以下代碼段描述了在使用 TestNG 時使用@Test(enabled = false)批注: ``` @Test(enabled=false) public void TestWithException() { System.out.println("Method should be ignored as it's not ready yet"); } ``` ## 異常測試 在 TestNG 和 JUnit4 中都可以使用異常測試。 它用于檢查測試中拋出了哪個異常? **Using JUnit4** 下面的代碼片段描述了使用 JUnit4 時異常測試的使用: ``` @Test(expected = ArithmeticException.class) public void divideByZero() { Int i = 1/0; } ``` **Using TestNG** 下面的代碼片段描述了在使用 TestNG 時使用異常測試的方法: ``` @Test(expectedExceptions = ArithmeticException.class) public void divideByZero() { Int i = 1/0; } ``` ## 超時 TestNg 和 JUnit4 中都實現了此功能。Timeout 用于終止測試,該測試花費的時間超過指定的時間(毫秒)。 **Using JUnit4** 下面的代碼片段描述了使用 JUnit4 時超時測試的使用: ``` @Test(timeout = 1000) public void method1() { while (true); } ``` **使用 TestNG** 下面的代碼段描述了在使用 TestNG 時超時測試的用法: ``` @Test(timeOut = 1000) public void method1() { while (true); } ``` ## 參數化測試 JUnit 提供了一種更簡單易懂的測試方法,稱為參數化測試。 TestNG 和 JUnit 都支持參數化測試,但是它們定義參數值的方式不同。 讓我們一一看。 **Using JUnit4** “ @RunWith”和“ @Parameter”批注用于為單元測試提供參數值。 注釋@Parameters 必須返回 List []。此參數將作為參數傳遞到類構造函數中。 ``` @RunWith(value = Parameterized.class) public class JunitTest{ privateint number; public JunitTest6(int number) { this.number = number; } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } }; returnArrays.asList(data); } @Test public void parameterTest() { System.out.println("Parameterized Number is : " + number); } } ``` **Using TestNG** 在 TestNG 中,XML 文件或“ @DataProvider”用于提供測試參數。 這里在方法中聲明的@Parameters 批注需要一個參數進行測試。 用作參數的數據將在 TestNG 的 XML 配置文件中提供。 通過這樣做,我們可以將具有不同數據集的單個[測試用例](/test-case.html)重用,并且可以獲得不同的結果。 ``` public class Test1 { @Test @Parameters(value="number") public void parameterTest(int number) { System.out.println("Parameterized Number is : " + number); } } ``` 請參閱以下用于以上類的 xml 文件: ``` <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="My test suite"> <test name="testing"> <parameter name="number" value="2"/> <classes> <class name="com.guru99.Test1" /> </classes> </test> </suite> ``` ## 總結: 我們詳細看到了 JUnit4 和 TestNG 的比較。 我們還看到除了參數化測試和依賴測試以外,兩者都相似。 簡而言之,我們可以說,基于靈活性和要求,我們可以選擇其中任意一種進行單元測試。 * [下一個](/selenium-tutorial.html)
                  <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>

                              哎呀哎呀视频在线观看