Skip test case and create dependency in TestNG

In the previous post, we have learnt about grouping of test cases in TestNG. In this post, we will learn how to Skip test case and create dependency in TestNG.

Sometimes, there is a requirement that you need to skip some test cases in the test Suite. we can achieve this requirement in 2 ways.

  • Making parameter “enable” as false.
  • Using “exclude” parameter in testng.xml.

Let’s understand them one by one.

Making parameter “enable” as false: 

We will take the same example , we have used in the previous post. Now, if we set the parameter “enable” as false for any method. That method will be skipped during the test execution. In our example, we have set this parameter for the method “Branch2“.

Right click on the testng.xml and run it as ‘TestNG Suite’.

Output: 

Skip test in TestNG

As you can see that ,Branch2 is skipped during execution.


Using “exclude” parameter in testng.xml:

This is another way to skip or exclude the test case during execution. For this, you can set the particular test case with exclude parameters in testng.xml.

Execute the program again.

Output: This time you will observe that Branch3 is also not executed along with the Branch2 which was already skipped .

Exclude keyword in TestNg

 


 

Creating dependency between test cases: 

Sometimes, it is required not to run the second test case unless until first test case is passed. As an example, if Login test case is failed , you can not go inside application to perform some other functionality. So, in that case it makes sense to skip this test case. But we have to tell Selenium that there is a dependency between both the test cases.

Dependency can be specified either with

  • Using attributes dependsOnMethods in @Test annotations OR
  • Using attributes dependsOnGroups in @Test annotations.

Example of creating dependency between test cases:

Suppose , there are 3 methods like “StartCar“, “DriveCar” and “StopCar“. “DriveCar” will depend on “StartCar” bacause we can driver unless Car is started. “StopCar” will depend on “DriveCar” because Car will be stopped once it has been driven.

Output:

TestNG test case dependsonmethods


This is box title
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Grouping of Test Cases in TestNG
Assertions and Reporting in TestNG
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

1 Response

  1. I knew about the “dependsOnMethods” attribute but thanks to you for adding “dependsOnGroups” too my knowledge.

Leave a Reply

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