TestNG Dependencies | TestNG Tutorial
TestNG Dependencies:
Sometimes, you may need to invoke methods in a test case in a certain order. Here comes TestNG Dependencies into the picture.
TestNG allows you to specify dependencies either with annotations or in XML.
First, we see Dependencies with annotations:
TestNG allows you to specify dependencies either with:
- Using attribute dependsOnMethods in @Test annotations, OR.
- Using attribute dependsOnGroups in @Test annotations.
Check below video to see “TestNG Dependencies with Annotations”
Please be patient. The video will load in some time.
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.
package softwareTestingMaterial; import org.testng.annotations.Test; public class DependsOnMethodsTestCase { @Test public void testCase1(){ System.out.println("Test Case 1"); } @Test public void testCase2(){ System.out.println("Test Case 2"); } }
testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="softwaretestingmaterial"> <test name="testngTest"> <classes> <class name="softwareTestingMaterial.DependsOnMethodsTestCase" /> </classes> </test> </suite>
Console Output:
[TestNG] Running: Test Case 1 Test Case 2 =============================================== softwaretestingmaterial Total tests run: 2, Failures: 0, Skips: 0 ===============================================
Now we add the dependsOnMethods attribute to the @Test Annotations and execute the same program.
See the script below.
package softwareTestingMaterial; import org.testng.annotations.Test; public class DependsOnMethodsTestCase { @Test(dependsOnMethods = {"testCase2"}) public void testCase1(){ System.out.println("Test Case 1"); } @Test public void testCase2(){ System.out.println("Test Case 2"); } }
Execute the same testng.xml which was placed above and see the difference in Console Output
Console Output:
[TestNG] Running: Test Case 2 Test Case 1 =============================================== softwaretestingmaterial Total tests run: 2, Failures: 0, Skips: 0 ===============================================
Let’s see Dependencies with XML:
Check below video to see “TestNG Dependencies with XML”
Please be patient. The video will load in some time.
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.
package softwareTestingMaterial; import org.testng.annotations.Test; public class DependsOnMethodsTestCase { @Test(groups = {"FirstGroup"}) public void testCase1(){ System.out.println("Test Case 1"); } @Test(groups = {"SecondGroup"}) public void testCase2(){ System.out.println("Test Case 2"); } }
testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="softwaretestingmaterial"> <test name="testngTest"> <groups> <dependencies> <group name="FirstGroup" depends-on="SecondGroup"></group> </dependencies> </groups> <classes> <class name="softwareTestingMaterial.DependsOnMethodsTestCase" /> </classes> </test> </suite>
Console Output:
[TestNG] Running: Test Case 2 Test Case 1 =============================================== softwaretestingmaterial Total tests run: 2, Failures: 0, Skips: 0 ===============================================
You could find the complete TestNG tutorial here.
Hi Rajkumar,
you are doing really well keep it up.
in the last example testng.xml middle line code should display like
but it’s displaying
thank’s
bharath.b
Hi Bharath, I didnt get you. Could you please put your code without these symbols ‘<' and '>‘