TestNG uses the Dependsongroups property to test for dependencies,
The test method relies on one or some of these methods as a precondition
Forced dependency: If an exception is made to a method that is dependent, then the subsequent method will not be executed (default)
Sequential dependencies: Subsequent methods are executed regardless of whether the method being relied on is an exception, configured by alwaysrun= "true"
[Java]View PlainCopy
- /**
- *
- * <p>
- * Title:testngdependongroups
- * </p>
- *
- * <p>
- * Description: The test method relies on some or some method, this/These methods as the pre-dependent conditions
- *
- * If the dependent method fails to execute correctly, by default, the next method will not be executed (forced dependency, default) if Alwaysrun is set on the annotation of the method body =
- * True, any exception in the dependency chain at this time will not affect the execution of other methods
- *
- * Dependsongroups
- *
- * </p>
- *
- * <p>
- * Company:
- * </p>
- *
- * @author: Dragon
- *
- * @date: October 21, 2014
- */
- 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 ...");
- }
- }
Configuration file:
[HTML]View PlainCopy
- <? 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>
Run Result: We found that after the order dependency was configured, even if the Initenvironment () method throws an exception, method1 () is executed and passed
[HTML]View PlainCopy
- 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
- ===============================================
Default forced dependency: Java code:
[Java]View PlainCopy
- 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 ...");
- }
- }
Original articles, all rights reserved, permitted reprint, Mark Source: Http://blog.csdn.net/wanghantong
Run Result: When we find Initenvironment () throws an exception, Method1 () is skipped and is not executed
[HTML]View PlainCopy
- 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 dependency and sequential dependence------testng dependence of advanced usage (II.)