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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # TestNG 并行執行測試,類和套件 > 原文: [https://howtodoinjava.com/testng/testng-executing-parallel-tests/](https://howtodoinjava.com/testng/testng-executing-parallel-tests/) [TestNG](https://testng.org/) **并行執行**測試,類和套件以及示例。 了解如何在多個線程中并行或單個運行 testng 測試和套件。 在軟件方面,并行性或多線程定義為軟件,操作系統或程序同時執行另一個程序的多個部分或子組件的能力。 TestNG 允許測試以并行或多線程模式運行。 這意味著基于測試套件的配置,不同的線程將同時啟動,并在其中執行測試方法。 與正常執行相比,這給用戶帶來了很多優勢,主要是減少了執行時間和驗證多線程代碼的能力。 ```java Table Of Contents 1\. Advantages of parallel rests execution 2\. Run parallel testcases 3\. Run test classes in parallel 4\. Run tests suite in parallel 5\. Configure a testcase to run in multiple threads ``` ## 1\. 并行執行測試的優點 并行或多線程執行可以為用戶提供很多優勢。 以下是兩個: 1. **減少了執行時間** – 并行執行測試時,會同時執行多個測試,因此減少了執行測試所需的總時間。 2. **允許多線程測試** – 使用此功能,我們可以編寫測試來驗證應用中的某些多線程代碼。 *并行測試執行*被 QA 行業廣泛用于功能自動化測試。 此功能可幫助質量檢查人員配置其測試,以使其易于同時在多個瀏覽器或操作系統中執行。 在 TestNG 中可以使用多種不同的方式配置并行功能。 ## 2\. 并行運行 testng 測試用例 TestNG 提供了多種方法**在多線程條件**中執行測試,其中一種方法是在單個線程中執行每種測試方法。 此模式顯著減少了執行時間,因為可以并行執行更多測試,從而減少了總執行時間。 testng 中用于**并行測試執行**的 Java 程序。 這是一個如何使用單個類文件中的`testng xml`并行運行方法的示例。 ```java package com.howtodoinjava.parallelism; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class ParallelMethodTest { @BeforeMethod public void beforeMethod() { long id = Thread.currentThread().getId(); System.out.println("Before test-method. Thread id is: " + id); } @Test public void testMethodsOne() { long id = Thread.currentThread().getId(); System.out.println("Simple test-method One. Thread id is: " + id); } @Test public void testMethodsTwo() { long id = Thread.currentThread().getId(); System.out.println("Simple test-method Two. Thread id is: " + id); } @AfterMethod public void afterMethod() { long id = Thread.currentThread().getId(); System.out.println("After test-method. Thread id is: " + id); } } ``` 前面的測試類包含兩個測試方法,它們在執行時將消息打印到控制臺上。 使用`Thread.currentThread.getId()`代碼求值正在執行當前方法的線程的 ID。 它還包含`before`和`after`方法,它們在執行時還將當前線程的線程 ID 打印到控制臺上。 在項目下創建一個名為`methods-test-testng.xml`的新文件,并編寫以下代碼。 ```java <suite name="Test-method Suite" parallel="methods" thread-count="2" > <test name="Test-method test" group-by-instances="true"> <classes> <class name="com.howtodoinjava.parallelism.ParallelMethodTest" /> </classes> </test> </suite> ``` 在 Eclipse 中選擇此文件并將其作為 TestNG 套件運行。 您將在“控制臺”窗口中看到以下測試結果: ```java Before test-method. Thread id is: 10 Before test-method. Thread id is: 9 Simple test-method Two. Thread id is: 10 Simple test-method One. Thread id is: 9 After test-method. Thread id is: 10 After test-method. Thread id is: 9 ``` 注意,上一個屏幕截圖中顯示的`Id`值在控制臺輸出中可能會不同。 `Id`值是在執行期間由 Java 虛擬機(JVM)在運行時分配的。 先前的測試結果清楚地表明,每種測試方法及其各自的`before`和`after`方法都是在不同的線程中執行的。 這由控制臺上打印的線程的 ID 標識。 ## 2\. 并行運行 testng 測試類 在此示例中,我們將了解并行執行 testng 測試類; 屬于測試執行的每個測試類都將在其自己的線程中執行。 ```java public class ParallelClassesTestOne { @BeforeClass public void beforeClass() { long id = Thread.currentThread().getId(); System.out.println("Before test-class. Thread id is: " + id); } @Test public void testMethodOne() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method One. Thread id is: " + id); } @Test public void testMethodTwo() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method Two. Thread id is: " + id); } @AfterClass public void afterClass() { long id = Thread.currentThread().getId(); System.out.println("After test-class. Thread id is: " + id); } } ``` ```java public class ParallelClassesTestTwo { @BeforeClass public void beforeClass() { long id = Thread.currentThread().getId(); System.out.println("Before test-class. Thread id is: " + id); } @Test public void testMethodOne() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method One. Thread id is: " + id); } @Test public void testMethodTwo() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method Two. Thread id is: " + id); } @AfterClass public void afterClass() { long id = Thread.currentThread().getId(); System.out.println("After test-class. Thread id is: " + id); } } ``` 在項目下創建一個名為`classes-test-testng.xml`的新文件,并編寫以下代碼。 ```java <suite name="Test-class Suite" parallel="classes" thread-count="2" > <test name="Test-class test" > <classes> <class name="com.howtodoinjava.parallelism.ParallelClassesTestOne" /> <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" /> </classes> </test> </suite> ``` 在 Eclipse 中選擇此文件并將其作為 TestNG 套件運行。 您將在“控制臺”窗口中看到以下測試結果: ```java Before test-class. Thread id is: 10 Before test-class. Thread id is: 9 Sample test-method One. Thread id is: 9 Sample test-method One. Thread id is: 10 Sample test-method Two. Thread id is: 10 After test-class. Thread id is: 10 Sample test-method Two. Thread id is: 9 After test-class. Thread id is: 9 ``` 先前的測試結果清楚地表明,每個測試類及其各自的`beforeClass`和`afterClass`方法都是在不同的線程中執行的。 這由控制臺上打印的線程的 ID 標識。 ## 4\. 并行運行 TestNG 套件 讓我們學習一下**在套件**中并行執行每個測試,也就是說,作為測試套件執行一部分的每個測試都將在各自獨立的線程中執行。 ```java package com.howtodoinjava.parallelism; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParallelSuiteTest { String testName = ""; @BeforeTest @Parameters({ "test-name" }) public void beforeTest(String testName) { this.testName = testName; long id = Thread.currentThread().getId(); System.out.println("Before test " + testName + ". Thread id is: " + id); } @BeforeClass public void beforeClass() { long id = Thread.currentThread().getId(); System.out.println("Before test-class " + testName + ". Thread id is: " + id); } @Test public void testMethodOne() { long id = Thread.currentThread().getId(); System.out.println("Sample test-method " + testName + ". Thread id is: " + id); } @AfterClass public void afterClass() { long id = Thread.currentThread().getId(); System.out.println("After test-method " + testName + ". Thread id is: " + id); } @AfterTest public void afterTest() { long id = Thread.currentThread().getId(); System.out.println("After test " + testName + ". Thread id is: " + id); } } ``` 在項目下創建一個名為`suite-test-testng.xml`的新文件,并編寫以下代碼。 ```java <suite name="Test-class Suite" parallel="tests" thread-count="2"> <test name="Test-class test 1"> <parameter name="test-name" value="test-method One" /> <classes> <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" /> </classes> </test> <test name="Test-class test 2"> <parameter name="test-name" value="test-method One" /> <classes> <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" /> </classes> </test> </suite> ``` 在 Eclipse 中選擇此文件并將其作為 TestNG 套件運行。 您將在“控制臺”窗口中看到以下測試結果: ```java Before test Test One. Thread id is: 9 Before test Test Two. Thread id is: 10 Before test-class Test One. Thread id is: 9 Before test-class Test Two. Thread id is: 10 Sample test-method Test One. Thread id is: 9 Sample test-method Test Two. Thread id is: 10 After test-method Test Two. Thread id is: 10 After test-method Test One. Thread id is: 9 After test Test One. Thread id is: 9 After test Test Two. Thread id is: 10 ``` 先前的測試結果清楚地表明,套件中的每個測試都在其各自的線程中執行。 這由控制臺上打印的線程的`ID`標識。 ## 5\. 配置 testng 測試以在多個線程中運行 前面我們討論了如何以并行或多線程模式運行類,方法和測試。 TestNG 還提供了靈活性,可以**配置要在多線程環境**中運行的測試方法。 這是通過在方法上使用`@Test`注解時對其進行配置來實現的。 ```java public class IndependentTest { @Test(threadPoolSize = 3, invocationCount = 6, timeOut = 1000) public void testMethod() { Long id = Thread.currentThread().getId(); System.out.println("Test method executing on thread with id: " + id); } } ``` 通過使用`threadPoolSize`屬性以及“測試”注解,該方法被配置為在多線程模式下運行。 `threadPoolSize`的值設置為 3; 這將測試方法配置為在三個不同的線程中運行。 其他兩個屬性`invocationCount`和`timeOut`,將測試配置為多次調用,并且如果執行花費很多,則失敗 更多時間。 在項目下創建一個名為`independent-test-testng.xml`的新文件,并編寫以下代碼。 ```java <suite name="Independent test Suite" > <test name="Independent test"> <classes> <class name="com.howtodoinjava.parallelism.IndependentTest" /> </classes> </test> </suite> ``` 在 Eclipse 中選擇此文件并將其作為 TestNG 套件運行。 您將在“控制臺”窗口中看到以下測試結果: ```java Test method executing on thread with id: 11 Test method executing on thread with id: 10 Test method executing on thread with id: 9 Test method executing on thread with id: 11 Test method executing on thread with id: 11 Test method executing on thread with id: 10 ``` 在此,根據`invocationCount`屬性值多次執行測試方法。 每次執行均在單獨的線程中完成,該線程從測試報告輸出中清晰可見。 當您只想在多線程模式下而不是整個測試套件中運行固定數量的測試方法時,此功能很有用。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看