`

testNG学习之超时设置和异常测试

阅读更多
1、设置测试方法的超时时间
       有些测试用例中某些部分可能会导致耗时异常,比如里面有个循环,可能会导致死循环,我们不可能一直等待下去,可以通过设定用例运行最大时间来避免无休止的等待,超过这个时间,就认为用例失败,继续执行其他用例。
       用@Test(timeOut=xxx) 指定超时时间,单位是毫秒。
<code="java">
@Test(timeOut=1000)
public void tester1() throws InterruptedException {
System.out.println("this is tester1");
Thread.sleep(2000);
System.out.println("time not out");
}
@Test(timeOut=3000)
public void tester2() throws InterruptedException {
System.out.println("this is tester2");
Thread.sleep(2000);
System.out.println("time not out");
}
</code>
       上面tester1将执行失败,报错java.lang.ArrayIndexOutOfBoundsException,tester2执行成功。

2、异常测试
        有些测试用例需要输入异常数据,验证程序是否抛出异常,通过expectedExceptions参数来测试预期的异常。
<code="java">
@Test(expectedExceptions = RuntimeException.class)
  public void exceptiontest1() {
  System.out.println("exceptiontest1");
  }
  @Test(expectedExceptions = ArithmeticException.class)
  public void exceptiontest2() {
  int i = 1/0;
  System.out.println(i);
  }
@Test(expectedExceptions = RuntimeException.class)
  public void exceptiontest3() {
  System.out.println("exceptiontest1");
          throw new RuntimeException();
  }
</code>
        上面第一个测试用例失败,第二个和第三个成功。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics