TestNg之invocationCount和invocationTimeOut

114 阅读1分钟

TestNg之invocationCount和invocationTimeOut

场景:我想要一个接口运行10次,一共消耗的时间不能大于11000毫秒

package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour  {
    @Test(invocationCount = 10,invocationTimeOut =11000 )
    public void testA() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("A");
    }
    @Test
    public void testB(){
        System.out.println("B");
    }
​
}

结果

A
A
A
A
A
A
A
A
A
A
B

我们继续来试一下异常的场景

package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour  {
    @Test(invocationCount = 10,invocationTimeOut =9000 )
    public void testA() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("A");
    }
    @Test
    public void testB(){
        System.out.println("B");
    }
​
}

结果

A
A
A
A
A
A
A
Aorg.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testA() didn't finish within the time-out 9000at sun.misc.Unsafe.compareAndSwapInt(Native Method)
  at java.util.concurrent.FutureTask.setException(FutureTask.java:248)
  at java.util.concurrent.FutureTask.run(FutureTask.java:271)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  at java.lang.Thread.run(Thread.java:748)
​
B