TestNG之依赖测试&超时测试

依赖测试

依赖测试,也是我们在自动化测试过程中经常会遇到的问题,比如支付接口,会依赖生成订单号的接口,等等...

那么,@Test标签中的dependsOnMethods属性来定义依赖关系

package com.boge.testng;
import org.testng.annotations.Test;
publicclassDependTest{
@Test
publicvoid test1(){
System.out.println("DependTest.test1执行了!");
}
@Test(dependsOnMethods = {"test1"})
publicvoid test2(){
System.out.println("DependTest.test2执行了!");
}
}

Run 'test2()'执行结果:

DependTest.test1执行了!
DependTest.test2执行了!
===============================================
DefaultSuite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

当被依赖的方法执行发生异常的时候,需要依赖的这个方法就会被忽略不会被执行。

package com.boge.testng;
import org.testng.annotations.Test;
publicclassDependTest{
@Test
publicvoid test1(){
System.out.println("DependTest.test1执行了!");
thrownewRuntimeException();
}
@Test(dependsOnMethods = {"test1"})
publicvoid test2(){
System.out.println("DependTest.test2执行了!");
}
}

执行结果:

DependTest.test1执行了!
java.lang.RuntimeException
    at com.boge.testng.DependTest.test1(DependTest.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Test ignored.
===============================================
DefaultSuite
Total tests run: 2, Failures: 1, Skips: 1
===============================================
Process finished withexit code 0

TestNG之超时测试

package com.boge.testng;
import org.testng.annotations.Test;
publicclassTimeOutTest{
@Test(timeOut = 2000)
publicvoid testSuccess() throwsInterruptedException{
System.out.println("在期望的超时时间范围内,测试成功!");
Thread.sleep(1000);
}
@Test(timeOut = 1000)
publicvoid testFailed() throwsInterruptedException{
System.out.println("超出期望的超时时间范围内,测试失败!");
Thread.sleep(2000);
}
}

执行结果:

超出期望的超时时间范围内,测试失败!
java.lang.ArrayIndexOutOfBoundsException: 0
    at org.testng.internal.MethodInvocationHelper.invokeWithTimeoutWithNewExecutor(MethodInvocationHelper.java:289)
    at org.testng.internal.MethodInvocationHelper.invokeWithTimeout(MethodInvocationHelper.java:246)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:667)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
在期望的超时时间范围内,测试成功!

推荐系列文章:

TestNG测试框架介绍

TestNG之注解@BeforeMethod、@AfterMethod、@BeforeClass、@AfterClass

TestNG之测试套件用法

TestNG之忽略测试及分组测试

TestNG之异常测试

更多精彩文章,请关注如下公众号

更多架构方便精彩文章,请关注如下公众号:

TestNG之依赖测试&超时测试_第1张图片

你可能感兴趣的:(TestNG之依赖测试&超时测试)