TestNG Dependency

We understood in the Priority section that if you want to run your test in a particular order. What will you do if a test is dependent to another test? Yes. You can use the TestNG Dependency feature for this. Both TestNG and JUnit support this feature

Another reason to use TestNG Dependency is sharing the data or state. If you want to share some data or state, then you can implement TestNG Dependency and share  the required data.

One question can come to your mind that how we will implement this.

Let’s understand with and Example

You can implement the dependency with TestNG by using following 2 ways

  • Using the attribute dependsOnMethod in @Test Annotation
  • Using the attribute dependsOnGroup in @Test Annotation

Let’s understand the flow of execution of this method.

  • The parent test method will execute before the dependent Test on the execution of the whole test class or test method.
  • When you will run only the dependent Test then parent test also run also that too before the dependent Test.
  • If Parent test method failed, in that case dependent Test method will not run and marked as skipped.

:

 Syntax  :  @Test(dependsOnMethod = {“Parent Test”})

Like dependsOnMethod, the dependsOnGroup attribute is used to make a test method dependent on a collection of tests belonging to a particular groups.
Some points to remember about dependsOnGroup-

  • In case, anyone or all the tests of the parent group method gets failed then the dependentTest method will not run and will be marked as skipped.
@Test(dependsOnGroups = "groupA"

Leave a Reply

Your email address will not be published. Required fields are marked *