TestNG Grouping and Dependency Test

Understanding grouping the test cases in TESTNG
While running automation scripts sometimes we need to run some specific group of test. we can achieve this by grouping the test cases in TestNG like this

    @Test(groups="Regression")
for each test method we can add groups as the parameter with some name as "Regression"

Create TestNG file as GroupTest as; this has two groups basically regression and sanity




Next create grouptestng.xml file and call only regression group test cases;



Note : here include tag is nothing but at run time we are including the group name and at same time if we add tag as exclude then this will exclude the test cases belonging to group "Regression"


Right click on grouptestng.xml file and run as testng suite and output should be something like this 

[TestNG] Running:
  /Users/gururaj/workspace/ManipalPro/GroupTestRun.xml

Im in testCaseFour - And in Regression Group
Im in testCaseOne - And in Regression Group
Im in testCaseTwo - And in Regression Group

===============================================
Sample Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================


Understanding dependency test in TESTNG


Sometimes, you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG as it supports the declaration of explicit dependencies between test methods.
TestNG allows you to specify dependencies either with: 
Using attributes dependsOnMethods in @Test annotation or groups, in this post we understand 

Test with single test method dependency
Test with multiple test methods dependencies
Test that depends on a group

Create a simple testng class as DependencyTestExample

 


This contains two methods and testOne depends on testTwo method, so we can add dependsOnMethods parameter in Test annotation.

Right click and run as Testng Suite




Output : 
Test method two
Test method one
PASSED: testTwo
PASSED: testOne


Test with multiple test methods dependencies




Sometimes it may be required for a test method to depend upon multiple other methods. This feature is very well supported by TestNG as part of the dependency support.


Output :

Test method three
Test method two
Test method one
PASSED: testThree
PASSED: testTwo
PASSED: testOne
 


Test that depends on a group
 
Similar to dependent methods TestNG also allows test methods to depend on groups. This makes sure that a group of test methods get executed before the dependent test method.


Output : 
Group Test method three
Group test method two
Group Test method one
PASSED: groupTestThree
PASSED: groupTestTwo
PASSED: groupTestOne