TestNg之invocationCount和invocationTimeOut

这篇博客探讨了如何使用TestNg进行接口的并发测试,通过`invocationCount`参数设置接口运行次数为10次,而`invocationTimeOut`参数限制总耗时不超过11000毫秒。当超时设置为9000毫秒时,如果接口执行时间超过限制,TestNg会抛出`ThreadTimeoutException`。案例展示了正常和异常场景下的执行结果,突显了超时控制在并发测试中的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
A

org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testA() didn't finish within the time-out 9000

	at 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值