首页 > 代码库 > TestNg依赖详解(二)-----强制依赖与顺序依赖
TestNg依赖详解(二)-----强制依赖与顺序依赖
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....."); } }
运行结果: 这时我们发现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 ===============================================
TestNg依赖详解(二)-----强制依赖与顺序依赖
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。