手动抛出异常_TestNG和SpringBoot2.1.6整合后,导致TestNG的断言异常无法抛出,是为什么?...

最近在做自动化测试项目。项目用到了TestNG、SpringBoot等框架。

在无意中发现,在@Test(description = "case1", timeOut = 180000)注解后,TestNG的Assert断言异常无法抛出。

先来看一下代码:

package com.woodpecker.testcase.demo;
import com.woodpecker.WoodpeckerApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;/** * 类描述:〈〉 * * @author: jinjianxu * @since: 1.0 */
@SpringBootTest(classes = {WoodpeckerApplication.class})
public class D extends AbstractTestNGSpringContextTests {
@Test(description = "case1", timeOut = 180000)
public void test() {
System.out.println("begin");
Assert.assertEquals("1", "2", "断言1和2相等");
System.out.println("end");
}
}

我在用例上加了timeOut = 180000,用于控制用例执行最大时间,防止case长时间卡住。咋们来运行一下这个代码,截图如下:

6017e46f7e99015d501d636ecc05cfdd.png

什么情况?断言失败的异常呢?

不懂了。

接下去我把用例的timeOut = 180000去掉,代码如下:

package com.woodpecker.testcase.demo;
import com.woodpecker.WoodpeckerApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;/** * 类描述:〈〉 * * @author: jinjianxu * @since: 1.0 */
@SpringBootTest(classes = {WoodpeckerApplication.class})
public class D extends AbstractTestNGSpringContextTests {
// @Test(description = "case1", timeOut = 180000)
@Test(description = "case1")
public void test() {
System.out.println("begin");
Assert.assertEquals("1", "2", "断言1和2相等");
System.out.println("end");
}
}
我把timeOut注释掉了。

运行一下,结果如下:

c82c7ca11451221ed792dccf9db1dc9d.png

咦,断言失败异常抛出来了。

什么鬼!

莫非是SpringBoot或者TestNG的 BUG?

求各位大神指导一下,万分感谢!

进一步发现,不管是任何异常,都不会被抛出。

代码如下:

951347ed52feb41a7f5cfc432f7984ef.png

运行代码,结果如下:

6e23e479955939fc951593c7f00a5806.png

你看,我手动抛出

throw new RuntimeException("断言1和2相等");

都没异常抛出来,被捕获掉了。

一切的问题,都在于 @Test(description = "case1", timeOut = 180000) 这个代码,设置了 timeOut 就会出现这个问题,去掉 timeOut 就没问题。可以正常抛出异常。

但是我用例,需要设置超时时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值