testNG超时测试

超时测试是指如果测试未超时则通过,反之则自动停止并置为运行失败;

testNG超时设置的时间是通过 测试脚本方法上添加@Test()timeOut参数控制的,如果@Test(timeOut = 3000),3000为毫秒数,1000毫秒=1秒。

java代码如下

 1 package com.course.testng;
 2 
 3 import org.testng.annotations.Test;
 4 
 5 public class TimeOutTest {
 6     @Test(timeOut = 3000)//单位是毫秒值  1000等于1秒
 7     public void testSuccess() throws InterruptedException {
 8         Thread.sleep(2000);
 9     }
10 
11     @Test(timeOut = 3000)
12     public void testFailed() throws InterruptedException {
13         Thread.sleep(4000);
14     }
15 }

运行结果如下

  C:UsersAdministrator.IntelliJIdea2019.3system	emp-testng-customsuite.xml



org.testng.internal.thread.ThreadTimeoutException: Method com.course.testng.TimeOutTest.testFailed() didn't finish within the time-out 3000

    at java.util.concurrent.ThreadPoolExecutor.tryTerminate(ThreadPoolExecutor.java:706)
    at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:1007)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1160)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)


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


Process finished with exit code 0
原文地址:https://www.cnblogs.com/linxinmeng/p/12593711.html