Manage Dependency between Test Cases
There are Two Dependencies, hard dependencies and soft dependencies describe different ways of handling relationships between test cases.
Hard Dependencies:
- A hard dependency means that a dependent test case will only execute if the test case it depends on passes. If the test it depends on fails or is skipped, the dependent test is skipped entirely. This is typically the default behavior when managing test dependencies.
Soft Dependencies:
- A soft dependency allows a dependent test to still run, even if the test it depends on fails or is skipped. This is useful when the test execution should continue regardless of a failure in a dependent test. However, this is not always a default feature and often requires custom handling in the test framework being used.
Example for hard Dependency
package asc; import org.testng.Assert; import org.testng.annotations.Test; public class dependency{ @Test public void UserRegistration() { System.out.println("This is test 1"); Assert.assertTrue(false); } @Test(dependsOnMethods="UserRegistration") public void UserSearch() { System.out.println("This is test 2"); } @Test public void reporterTest3() { System.out.println("This is test 3"); } @Test public void reporterTest4() { System.out.println("This is test 4"); } @Test public void reporterTest5() { System.out.println("This is test 5"); } }
Example for Soft Dependency
package asc; import org.testng.Assert; import org.testng.annotations.Test; public class dependency{ @Test public void UserRegistration() { System.out.println("This is test 1"); Assert.assertTrue(false); } @Test(dependsOnMethods="UserRegistration",alwaysRun=true) public void UserSearch() { System.out.println("This is test 2"); } @Test public void reporterTest3() { System.out.println("This is test 3"); } @Test public void reporterTest4() { System.out.println("This is test 4"); } @Test public void reporterTest5() { System.out.println("This is test 5"); } }
Key Differences
- Hard Dependency: Dependent test case will not run if the base test fails or is skipped.
- Soft Dependency: Dependent test case will still run even if the base test fails, but may adjust its behavior based on the outcome.