TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

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

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:

https://testng.org

 

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ TestNG Assert Lifecycle› TestNG Annotations Order Example

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version