Hard and Soft dependency in TestNG Framework
Overview
In this tutorial, we will understand Hard and Soft dependency in the TestNG framework with code examples.
Hard dependency
Hard dependency is a strict dependency. By default if the methods depended upon fail, the method depends on will be Skipped. We can specify hard dependency using the below clauses:
dependsOnMethods=
dependsOnGroups=
TestNG guarantees that all the depended methods and all methods in the depended groups are invoked before the method. In Hard dependency all the methods should PASS, in order for the method to run. If any methods fail the method would be skipped.
Example
//www.TestingDocs.com - TestNG Tutorials import org.testng.annotations.Test; public class WebTests { /*************************************************************** * This test belongs to "web" group, * Run only if all the methods from "deployApplication" group are passed. ***************************************************************/ @Test(groups="web", dependsOnGroups="deployApplication") public void init() { System.out.println("Test Base URL.."); } /************************************************************** * This test belongs to "web" group, * Run if "init" method is passed. **************************************************************/ @Test(dependsOnMethods = {"init"}, groups="web") public void testAppMethod() { System.out.println("Web Application Test..."); } }
Sample deploy tests
package com.testingdocs.testng.deploy; import org.testng.annotations.Test; public class DeployTest { @Test(groups="deployApplication") public void deployApp() { System.out.println("Application War deploy ..."); } @Test(groups="deployApplication",dependsOnMethods="deployApp") public void basicTest() { System.out.println("Basic Application Test..."); } }
In this example, if the deployApp() method fails all the dependent methods would be skipped. To simulate this, we will mark the deployApp() method fail.
Soft Dependency
In some cases, we may want to softened the dependency rule and we want to run the method even if the depended method(s) fail. We can achieve soft dependency with the @Test annotation attribute “alwaysRun=true”
The test method will always run even if the depended test method is failed or skipped.
//www.TestingDocs.com - TestNG Tutorials import static org.testng.Assert.fail; import org.testng.annotations.Test; public class SampleTests { @Test(groups="P1") public void runBeforeTest() { System.out.println("Base Test ..."); fail(); } @Test(dependsOnMethods= {"runBeforeTest"},alwaysRun=true) public void independentTest() { System.out.println("This test will run ..."); } }
Soft dependency is used to order the tests so that certain tests are invoked before the test method. The PASS/FAIL of those methods do not matter for the test method to run.
TestNG Tutorials on this website can be found at:
https://www.testingdocs.com/testng-framework-tutorial/
For more details on the TestNG Framework, visit the official website of TestNG at: