Testng dependency description (ii) ----- mandatory dependency and sequential dependency

Source: Internet
Author: User

Testng uses the dependsongroups attribute for dependency testing,

The test method depends on one or some methods. this/these methods are used as pre-dependent conditions.

Forced dependency: if an exception occurs for a method that is dependent, subsequent methods will not be executed (default)

Sequential dependency: no matter whether the dependent method is abnormal or not, subsequent methods will be executed and configured through alwaysrun = "true ".



/***** <P> * Title: testngdependongroups * </P> ** <p> * Description: The test method depends on one or more methods, this/these methods are used as pre-dependent conditions ** if the dependent methods are abnormal and cannot be correctly executed, the following methods will not be executed by default (mandatory dependency, default) if alwaysrun = * true is set on the annotation of the method body, in this case, any method exception in the dependency chain will not affect the execution of other methods ** dependsongroups ** </P> ** <p> * company: * </P> ** @ Author: Dragon ** @ Date: July 15, 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:

<?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>

Running Result: After the sequential dependency is configured, even if the initenvironment () method throws an exception, Method1 () still runs and passed

initEnvironment.....serverStartedOk.....I am over here.....PASSED: serverStartedOkPASSED: method1FAILED: initEnvironmentjava.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 mandatory dependency: 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.....");}}

Running Result: After initenvironment () throws an exception, Method1 () is skipped and not executed.

initEnvironment.....serverStartedOk.....PASSED: serverStartedOkFAILED: initEnvironmentjava.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 description (ii) ----- mandatory dependency and sequential dependency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.