TestNg使用dependsOnGroups属性来进行依赖测试,
测试方法依赖于某个或某些方法,这个/这些方法作为前置依赖条件
强制依赖:如果被依赖的某一个方法发生了异常,那么之后的方法都不会被执行(默认)
顺序依赖:无论被依赖的方法是否出现异常,后续的方法都会被执行,通过alwaysRun=“true”来配置
- /**
- *
- * <p>
- * Title: TestngDependOnGroups
- * </p>
- *
- * <p>
- * Description: 测试方法依赖于某个或某些方法,这个/这些方法作为前置依赖条件
- *
- * 如果依赖的方法发生异常而不能正确的执行时,默认情况下,接下来的方法将不会被执行(强制依赖,默认) 如果在方法体的注解上设置了alwaysRun =
- * true,那么此时在依赖链中的任何一个方法出现异常都不会影响其它方法的执行
- *
- * dependsOnGroups
- *
- * </p>
- *
- * <p>
- * Company:
- * </p>
- *
- * @author : Dragon
- *
- * @date : 2014年10月21日
- */
- public class TestngDependOnGroups {
- @Test(groups = { "init" })
- public void serverStartedOk() {
- System.out.println("serverStartedOk.....");
- }
- @Test(groups = { "init2" })
- public void initEnvironment() {
- System.out.println("initEnvironment.....");
- throw new RuntimeException("unexpected fail......");
- }
- @Test(dependsOnGroups = { "init.*" }, alwaysRun = true)
- public void method1() {
- System.err.println("I am over here.....");
- }
- }
配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
- <suite name="framework_testng" >
- <test verbose="2" name="TestngDependOnGroups">
- <classes>
- <class name="com.dragon.testng.annotation.TestngDependOnGroups">
- </class>
- </classes>
- </test>
- </suite>
运行结果:我们发现在配置了顺序依赖之后,即使initEnvironment()方法抛出了异常,method1()还是执行了并且passed
- initEnvironment.....
- serverStartedOk.....
- I am over here.....
- PASSED: serverStartedOk
- PASSED: method1
- FAILED: initEnvironment
- java.lang.RuntimeException: unexpected fail......
- at com.dragon.testng.annotation.TestngDependOnGroups.initEnvironment(TestngDependOnGroups.java:41)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:606)
- at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
- at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
- at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
- at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
- at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
- at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
- at org.testng.TestRunner.privateRun(TestRunner.java:767)
- at org.testng.TestRunner.run(TestRunner.java:617)
- at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
- at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
- at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
- at org.testng.SuiteRunner.run(SuiteRunner.java:240)
- at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
- at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
- at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
- at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
- at org.testng.TestNG.run(TestNG.java:1057)
- at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
- at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
- at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
- ===============================================
- TestngDependOnGroups
- Tests run: 3, Failures: 1, Skips: 0
- ===============================================
默认的强制依赖:Java code:
- public class TestngDependOnGroups {
- @Test(groups = { "init" })
- public void serverStartedOk() {
- System.out.println("serverStartedOk.....");
- }
- @Test(groups = { "init2" })
- public void initEnvironment() {
- System.out.println("initEnvironment.....");
- throw new RuntimeException("unexpected fail......");
- }
- @Test(dependsOnGroups = { "init.*" })
- public void method1() {
- System.err.println("I am over here.....");
- }
- }
原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong
运行结果: 这时我们发现initEnvironment() 抛出异常后, method1() 被跳过了,并没有被执行
- initEnvironment.....
- serverStartedOk.....
- PASSED: serverStartedOk
- FAILED: initEnvironment
- java.lang.RuntimeException: unexpected fail......
- at com.dragon.testng.annotation.TestngDependOnGroups.initEnvironment(TestngDependOnGroups.java:41)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:606)
- at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
- at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
- at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
- at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
- at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
- at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
- at org.testng.TestRunner.privateRun(TestRunner.java:767)
- at org.testng.TestRunner.run(TestRunner.java:617)
- at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
- at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
- at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
- at org.testng.SuiteRunner.run(SuiteRunner.java:240)
- at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
- at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
- at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
- at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
- at org.testng.TestNG.run(TestNG.java:1057)
- at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
- at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
- at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
- SKIPPED: method1
- ===============================================
- TestngDependOnGroups
- Tests run: 3, Failures: 1, Skips: 1
- ===============================================