<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # TestNG – 超時測試教程 > 原文: [https://howtodoinjava.com/testng/testng-timeout-test-tutorial/](https://howtodoinjava.com/testng/testng-timeout-test-tutorial/) 在運行測試時,某些情況下可能會卡住某些測試,或者可能花費比預期更長的時間。 在這種情況下,您可能需要將所述測試用例標記為失敗,然后繼續。 在本教程中,我們將學習**將 TestNG 測試配置為在某些配置的持續時間后超時**。 TestNG 允許用戶設置**時間段**,以等待測試完全執行。 超時可以通過兩種方式配置: * **在套件級別**:這將適用于上述 TestNG 測試套件中的所有測試 * **在每個測試方法級別**:這將適用于所述測試方法,并且如果在套件級別配置,則將覆蓋時間段 要指定超時持續時間,請使用`@Test`注解的“`timeOut`”屬性。 ```java @Test ( timeOut = 500 ) ``` 讓我們創建一個示例測試,并了解 TimeNG 在 TestNG 中的工作原理。 ## 套件級別的超時測試示例 在下面的測試中,我們有兩種測試方法,即`timeTestOne()`和`timeTestTwo()`。 `timeTestOne()`將需要 1000 毫秒才能完全執行,而`timeTestTwo()`將需要 400 毫秒才能完全執行。 我們已經使用`Thread.sleep()`方法來強制執行時間。 ```java public class TimeoutSuite { @Test public void timeTestOne() throws InterruptedException { Thread.sleep(1000); System.out.println("Time test method one"); } @Test public void timeTestTwo() throws InterruptedException { Thread.sleep(400); System.out.println("Time test method two"); } } ``` 現在將`testng.xml`文件添加到項目根目錄,并將以下代碼放入其中。 該代碼將超時時間定義為 500ms。 ```java <suite name="Time test Suite" time-out="500" verbose="1" > <test name="Timeout Test" > <classes> <class name="com.howtodoinjava.test.TimeoutSuite" /> </classes> </test> </suite> ``` 現在使用`testng.xml`運行以上測試。 以上測試運行的輸出如下: ```java [TestNG] Running: C:\somepath\TestNGExamples\testng.xml Time test method two =============================================== Time test Suite Total tests run: 2, Failures: 1, Skips: 0 =============================================== ``` 從測試結果中可以看出,只有`timeTestTwo()`被執行,因為它的執行時間少于`testng.xml`文件中定義的超時時間。 `timeTestOne()`執行被取消,因為完成所需的時間超過配置的超時時間。 現在開始學習在測試方法級別設置超時的方法。 ## 方法級別的超時測試示例 如前所述,您也可以在方法級別指定超時。 這將使您可以靈活地給每個單獨的測試方法特定的運行時間。 ```java public class TimeoutMethod { @Test(timeOut = 500) public void timeTestOne() throws InterruptedException { Thread.sleep(1000); System.out.println("Time test method one"); } @Test(timeOut = 500) public void timeTestTwo() throws InterruptedException { Thread.sleep(400); System.out.println("Time test method two"); } } ``` 以上測試運行的輸出如下: ```java [[TestNG] Running: C:\Users\somepath\testng-customsuite.xml Time test method two PASSED: timeTestTwo FAILED: timeTestOne org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.timeTestOne() didn't finish within the time-out 500 =============================================== Default test Tests run: 2, Failures: 1, Skips: 0 =============================================== ``` 在上述測試方法中,`timeTestOne()`失敗,因為它未在指定的超時時間內完全執行。 **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看