TestNG Tutorials 64: Dependency in TestNG – ignoreMissingDependencies – Another Way of Achieving Soft Dependencies

Hello Folks,

There are two types of dependencies in TestNG:

1. Hard Dependency : All the methods you depend on must have run and succeeded for you to run. If at least one failure occurred in your dependencies, you will not be invoked and marked as a SKIP in the report.

2. Soft Dependency : You will always be run after the methods you depend on, even if some of them have failed. This is useful when you just want to make sure that your test methods are run in a certain order but their success doesn’t really depend on the success of others. A soft dependency is obtained by adding “alwaysRun=true” in your @Test annotation.

You can go through about above concept in this post.

But consider below scenarios:

  1. How you will run your test which depends on another tests and that tests does not exist always or need to execute based on some conditions?
  2. How you will run your test which depends on another tests which are marked false for “enabled” attribute.

 

First, let’s see what happens when you run  a test which depends on another test and that test is missing.

package Dependency;


import org.testng.annotations.Test;

public class HardDependeny {
	
	
	/*
	 * This test method depends on another test method named "Test1" which does not exist.	
	 */
	@Test(dependsOnMethods= {"Test1"})
	public void Test2()
	{
		System.out.println("I am Test2");
	}
	
	
}

 

Output:

[java]
[RemoteTestNG] detected TestNG version 6.14.2
org.testng.TestNGException:
Dependecy.HardDependeny.Test2() depends on nonexistent method Test1
at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:114)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:241)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:317)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:64)
at org.testng.TestRunner.initMethods(TestRunner.java:438)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

[/java]

You can see it gives exception as “depends on nonexistent method”. Because when we run a test in TestNG, it looks for test/s on which your test depends on. If TestNG finds them, those will be executed first. If TestNG does not find them, it will throw an exception stating test does not exist.

Consider another scenario. What will be behaviour if test on which your test depends on, is marked as “enabled=false”? Let’s try this:

Java Code:

package Dependecy;


import org.testng.annotations.Test;

public class HardDependeny {
	
	
	@Test(enabled= false)
	public void Test1()
	{
		System.out.println("I am Test1");
	}
	
	
	/*
	 * This test method depends on another test method named "Test1" which is not enabled.	
	 */
	@Test(dependsOnMethods= {"Test1"})
	public void Test2()
	{
		System.out.println("I am Test2");
	}
	
	
}

Output:

[java]
[RemoteTestNG] detected TestNG version 6.14.2
org.testng.TestNGException:
Dependency.HardDependeny.Test2() is depending on method public void Dependency.HardDependeny.Test1(), which is not annotated with @Test or not included.
at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:111)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:241)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:317)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:64)
at org.testng.TestRunner.initMethods(TestRunner.java:438)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

[/java]

You can see in both the cases, your test will not run. This is also called Hard Dependency. If you want your test to be run in both above case, you can use an attribute called “ignoreMissingDependencies” as “true” which is by default set as “false”. It will run your test even dependencies are missing or disabled. Let’ see an example below:

Java Code:

package Dependecy;


import org.testng.annotations.Test;

public class HardDependeny {
	
	
	@Test(enabled= false)
	public void Test1()
	{
		System.out.println("I am Test1");
	}
	
	
	/*
	 * This test method depends on another test method named "Test1" which is not enabled.	
	 * Setting ignoreMissingDependencies as true will make it soft dependency. 
	 */
	@Test(dependsOnMethods= {"Test1"}, ignoreMissingDependencies= true)
	public void Test2()
	{
		System.out.println("I am Test2");
	}
	
	
}

Output:

[java]
[RemoteTestNG] detected TestNG version 6.14.2
I am Test2
PASSED: Test2

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[/java]

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Leave a Reply

Your email address will not be published. Required fields are marked *