鍍金池/ 教程/ Java/ TestNG超時測試
TestNG參數(shù)測試(DataProvider)
TestNG配置注解實例
TestNG預(yù)期異常測試
TestNG + Selenium負載測試
TestNG參數(shù)化測試
TestNG參數(shù)測試(XML和DataProvider)
TestNG配置注釋
TestNG套件測試
TestNG依賴測試
TestNG忽略測試
TestNG忽略測試
TestNG分組測試
TestNG預(yù)期異常測試
TestNG + Selenium
TestNG參數(shù)測試實例
TestNG超時測試
TestNG Hello World入門示例
JUnit 4 Vs TestNG比較
TestNG + Spring集成示例
TestNG分組測試
TestNG教程
TestNG超時測試
JUnit 4 Vs TestNG比較
TestNG基本注解
TestNG依賴性測試
TestNG套件測試
TestNG + Spring集成測試

TestNG超時測試

在本教程中,我們將演示如何在TestNG中執(zhí)行超時測試。 “超時”表示如果單元測試花費的時間超過指定的毫秒數(shù),那么TestNG將會中止它并將其標記為失敗。

“超時”也可用于性能測試,以確保方法在合理的時間內(nèi)返回。

創(chuàng)建一個名稱為:TimeoutTest 的 Maven 項目,其結(jié)構(gòu)如下所示 -

創(chuàng)建一個測試類:TestTimeout.java,其代碼如下 -

package com.yiibai;

import org.testng.annotations.Test;

public class TestTimeout {

    @Test(timeOut = 5000) // time in mulliseconds
    public void testThisShouldPass() throws InterruptedException {
        Thread.sleep(4000);
    }

    @Test(timeOut = 1000)
    public void testThisShouldFail() {
        while (true){
            // do nothing
        }

    }

}

執(zhí)行上面代碼,得到以下結(jié)果 -

[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1892041198\testng-customsuite.xml

PASSED: testThisShouldPass
FAILED: testThisShouldFail
org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.testThisShouldFail() didn't finish within the time-out 1000
    at com.yiibai.TestTimeout.testThisShouldFail(TestTimeout.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:46)
    at org.testng.internal.InvokeMethodRunnable.run(InvokeMethodRunnable.java:37)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@1b40d5f0: 12 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6ea6d14e: 38 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4563e9ab: 8 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 15 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2aaf7cc2: 57 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@45c8e616: 4 ms