testNG依赖测试
testNG依赖测试是指一个用例需要依赖另外一个用例才能运行,只有运行成功后才会执行,如果被依赖的用例执行失败,那边这个用例将会Skips掉。
以下代码运行结果
Total tests run: 4, Failures: 1, Skips: 1
java代码如下
1 package com.course.testng; 2 3 import org.testng.annotations.Test; 4 5 public class DependTest { 6 /** 7 * 依赖测试 8 */ 9 // test1 可以作为test2的前置条件 10 @Test 11 public void test1() { 12 System.out.println("这是test1运行"); 13 } 14 15 /** 16 * 可以成功运行,因为test1()能运行成功 17 */ 18 @Test(dependsOnMethods = {"test1"}) 19 public void test2() { 20 System.out.println("这是test2运行"); 21 22 } 23 24 /** 25 * 异常测试失败,抛出的异常和期望结果ArithmeticException不符 26 */ 27 @Test(expectedExceptions = ArithmeticException.class) 28 public void runTimeExceptionFail() { 29 System.out.println("这应该抛出一个RuntimeException异常"); 30 throw new RuntimeException(); 31 32 } 33 34 /** 35 * 这个用例会因runTimeExceptionFail()运行失败,而Skips 36 */ 37 @Test(dependsOnMethods = {"runTimeExceptionFail"}) 38 public void test3() { 39 System.out.println("这个用例会因runTimeExceptionFail()运行失败,而Skips"); 40 41 } 42 43 }
运行结果
1 [TestNG] Running: 2 C:\Users\Administrator\.IntelliJIdea2019.3\system\temp-testng-customsuite.xml 3 4 5 这应该抛出一个RuntimeException异常 6 7 8 9 java.lang.RuntimeException 10 at com.course.testng.DependTest.runTimeExceptionFail(DependTest.java:30) 11 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 12 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 13 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 14 at java.lang.reflect.Method.invoke(Method.java:498) 15 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) 16 at org.testng.internal.Invoker.invokeMethod(Invoker.java:645) 17 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851) 18 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177) 19 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 20 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 21 at org.testng.TestRunner.privateRun(TestRunner.java:756) 22 at org.testng.TestRunner.run(TestRunner.java:610) 23 at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) 24 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) 25 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 26 at org.testng.SuiteRunner.run(SuiteRunner.java:289) 27 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 28 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 29 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) 30 at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) 31 at org.testng.TestNG.runSuites(TestNG.java:1133) 32 at org.testng.TestNG.run(TestNG.java:1104) 33 at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66) 34 at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110) 35 36 37 这是test1运行 38 39 40 这是test2运行 41 42 43 44 45 Test ignored. 46 47 =============================================== 48 Default Suite 49 Total tests run: 4, Failures: 1, Skips: 1 50 =============================================== 51 52 53 Process finished with exit code 0