TestNG provides “@Test” annotation along with attribute “dependsOnMethods”. There may be a requirement to make a test case to depends on other test cases. To make a test case to depends on other test cases, we need to use dependsOnMethods attribute with @Test annotation.
DependsOnMethods Testng Example
1. WorkflowTest depends on LoginTest. If LoginTest test case failed then WorkflowTest test case will skipped.
2. LogoutTest depends on LoginTest and WorkflowTest test cases. If LoginTest or WorkflowTest test case failed then LogoutTest test case will skipped.
TestNG DependsOnMethods
import org.testng.Assert; import org.testng.annotations.Test; public class TestCasePriority { @Test(priority=1) public void LoginTest(){ Assert.assertEquals("true", "false"); } @Test(priority=2, dependsOnMethods={"LoginTest"}) public void WorkflowTest(){ System.out.println(" This test case should Skipped, if LoginTest test case Failed"); } @Test(priority=3, dependsOnMethods={"LoginTest", "WorkflowTest"}) public void LogoutTest(){ System.out.println(" This test case should Skipped, if LoginTest or WorkflowTest test case Failed"); System.out.println(" "); } }