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“.
package com.testsuite.example; import org.testng.annotations.Test; public class GroupExample { @Test(groups={"HDFCBank"}) public void Branch1(){ System.out.println("Test 1 : HDFC Branch"); } @Test(groups={"HDFCBank"},enabled=false) public void Branch2(){ System.out.println("Test 2 : HDFC Branch"); } @Test(groups={"SBIBank"}) public void Branch3(){ System.out.println("Test 3 : SBI Branch"); } @Test(groups={"SBIBank"}) public void Branch4(){ System.out.println("Test 4 : SBI Branch"); } @Test(groups={"HDFCBank","Bank"}) public void Branch5(){ System.out.println("Test 5 : HDFC Branch and Main Bank"); } }
Right click on the testng.xml and run it as ‘TestNG Suite’.
Output:
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.
<suite name="ExampleTestSuite" > <test name="Bank functionality"> <groups> <run> <include name="HDFCBank"/> <include name="SBIBank"/> </run> </groups> <classes> <class name="com.testsuite.example.GroupExample"/> </classes> <methods> <exclude name="Branch3"/> </methods> </test> </suite>
Execute the program again.
Output: This time you will observe that Branch3 is also not executed along with the Branch2 which was already skipped .
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.
import org.testng.annotations.Test; public class Car { @Test(dependsOnMethods={"startCar"}) public void driveCar(){ System.out.println("Car driving"); } @Test public void startCar(){ System.out.println("Car started"); } @Test(dependsOnMethods={"driveCar"}) public void stopCar(){ System.out.println("Car stopped"); } }
Output:
I knew about the “dependsOnMethods” attribute but thanks to you for adding “dependsOnGroups” too my knowledge.