TestNG dependency and sequential dependence------testng dependence of advanced usage (II.)

Source: Internet
Author: User
Tags testng

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
  1. /**
  2. *
  3. * <p>
  4. * Title:testngdependongroups
  5. * </p>
  6. *
  7. * <p>
  8. * Description: The test method relies on some or some method, this/These methods as the pre-dependent conditions
  9. *
  10. * 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 =
  11. * True, any exception in the dependency chain at this time will not affect the execution of other methods
  12. *
  13. * Dependsongroups
  14. *
  15. * </p>
  16. *
  17. * <p>
  18. * Company:
  19. * </p>
  20. *
  21. * @author: Dragon
  22. *
  23. * @date: October 21, 2014
  24. */
  25. Public class Testngdependongroups {
  26. @Test (groups = { "init"})
  27. public void Serverstartedok () {
  28. System.out.println ("Serverstartedok ...");
  29. }
  30. @Test (groups = { "Init2"})
  31. public void Initenvironment () {
  32. System.out.println ("initenvironment ...");
  33. throw New RuntimeException ("unexpected fail ...");
  34. }
  35. @Test (dependsongroups = { "init.*"}, Alwaysrun = true)
  36. public void Method1 () {
  37. System.err.println ("I am over here ...");
  38. }
  39. }

Configuration file:

[HTML]View PlainCopy 
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd">
  3. <suite name="framework_testng" >
  4. <test verbose="2" name= "testngdependongroups">
  5. <classes>
  6. <class name="com.dragon.testng.annotation.TestngDependOnGroups">
  7. </class>
  8. </Classes>
  9. </Test>
  10. </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  
  1. Initenvironment .....
  2. Serverstartedok .....
  3. I am over here .....
  4. Passed:serverstartedok
  5. Passed:method1
  6. Failed:initenvironment
  7. java.lang.RuntimeException:unexpected fail ...
  8. At Com.dragon.testng.annotation.TestngDependOnGroups.initEnvironment (testngdependongroups.java:41)
  9. At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
  10. At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57)
  11. At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
  12. At Java.lang.reflect.Method.invoke (method.java:606)
  13. At Org.testng.internal.MethodInvocationHelper.invokeMethod (methodinvocationhelper.java:84)
  14. At Org.testng.internal.Invoker.invokeMethod (invoker.java:714)
  15. At Org.testng.internal.Invoker.invokeTestMethod (invoker.java:901)
  16. At Org.testng.internal.Invoker.invokeTestMethods (invoker.java:1231)
  17. At Org.testng.internal.TestMethodWorker.invokeTestMethods (testmethodworker.java:127)
  18. At Org.testng.internal.TestMethodWorker.run (testmethodworker.java:111)
  19. At Org.testng.TestRunner.privateRun (testrunner.java:767)
  20. At Org.testng.TestRunner.run (testrunner.java:617)
  21. At Org.testng.SuiteRunner.runTest (suiterunner.java:334)
  22. At Org.testng.SuiteRunner.runSequentially (suiterunner.java:329)
  23. At Org.testng.SuiteRunner.privateRun (suiterunner.java:291)
  24. At Org.testng.SuiteRunner.run (suiterunner.java:240)
  25. At Org.testng.SuiteRunnerWorker.runSuite (suiterunnerworker.java:52)
  26. At Org.testng.SuiteRunnerWorker.run (suiterunnerworker.java:86)
  27. At Org.testng.TestNG.runSuitesSequentially (testng.java:1224)
  28. At Org.testng.TestNG.runSuitesLocally (testng.java:1149)
  29. At Org.testng.TestNG.run (testng.java:1057)
  30. At Org.testng.remote.RemoteTestNG.run (remotetestng.java:111)
  31. At Org.testng.remote.RemoteTestNG.initAndRun (remotetestng.java:204)
  32. At Org.testng.remote.RemoteTestNG.main (remotetestng.java:175)
  33. ===============================================
  34. Testngdependongroups
  35. Tests Run:3, failures:1, skips: 0
  36. ===============================================

Default forced dependency: Java code:

[Java]View PlainCopy 
  1. Public class Testngdependongroups {
  2. @Test (groups = { "init"})
  3. public void Serverstartedok () {
  4. System.out.println ("Serverstartedok ...");
  5. }
  6. @Test (groups = { "Init2"})
  7. public void Initenvironment () {
  8. System.out.println ("initenvironment ...");
  9. throw New RuntimeException ("unexpected fail ...");
  10. }
  11. @Test (dependsongroups = { "init.*"})
  12. public void Method1 () {
  13. System.err.println ("I am over here ...");
  14. }
  15. }

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  
    1. Initenvironment .....
    2. Serverstartedok .....
    3. Passed:serverstartedok
    4. Failed:initenvironment
    5. java.lang.RuntimeException:unexpected fail ...
    6. At Com.dragon.testng.annotation.TestngDependOnGroups.initEnvironment (testngdependongroups.java:41)
    7. At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    8. At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57)
    9. At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
    10. At Java.lang.reflect.Method.invoke (method.java:606)
    11. At Org.testng.internal.MethodInvocationHelper.invokeMethod (methodinvocationhelper.java:84)
    12. At Org.testng.internal.Invoker.invokeMethod (invoker.java:714)
    13. At Org.testng.internal.Invoker.invokeTestMethod (invoker.java:901)
    14. At Org.testng.internal.Invoker.invokeTestMethods (invoker.java:1231)
    15. At Org.testng.internal.TestMethodWorker.invokeTestMethods (testmethodworker.java:127)
    16. At Org.testng.internal.TestMethodWorker.run (testmethodworker.java:111)
    17. At Org.testng.TestRunner.privateRun (testrunner.java:767)
    18. At Org.testng.TestRunner.run (testrunner.java:617)
    19. At Org.testng.SuiteRunner.runTest (suiterunner.java:334)
    20. At Org.testng.SuiteRunner.runSequentially (suiterunner.java:329)
    21. At Org.testng.SuiteRunner.privateRun (suiterunner.java:291)
    22. At Org.testng.SuiteRunner.run (suiterunner.java:240)
    23. At Org.testng.SuiteRunnerWorker.runSuite (suiterunnerworker.java:52)
    24. At Org.testng.SuiteRunnerWorker.run (suiterunnerworker.java:86)
    25. At Org.testng.TestNG.runSuitesSequentially (testng.java:1224)
    26. At Org.testng.TestNG.runSuitesLocally (testng.java:1149)
    27. At Org.testng.TestNG.run (testng.java:1057)
    28. At Org.testng.remote.RemoteTestNG.run (remotetestng.java:111)
    29. At Org.testng.remote.RemoteTestNG.initAndRun (remotetestng.java:204)
    30. At Org.testng.remote.RemoteTestNG.main (remotetestng.java:175)
    31. Skipped: method1
    32. ===============================================
    33. Testngdependongroups
    34. Tests Run:3, failures:1, skips: 1
    35. ===============================================

TestNG dependency and sequential dependence------testng dependence of advanced usage (II.)

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.