Sunday 15 March 2020

TestNG: dependsOnMethods: Specify dependent test cases

We can specify list of dependent test methods using 'dependsOnMethods' property. There is no guarantee on the order on which the methods depended upon will be run, but it is guaranteed that all these methods will run before the test method that contains this annotation is run.

Example: Test method A depend on methods B, C and D
@Test(dependsOnMethods = { "B", "C", "D" })
public void A() {
         System.out.println("I am in A");
}

DependentOnMethodsTest1.java
package com.sample.app.tests;

import org.testng.annotations.Test;

public class DependentOnMethodsTest1 {

 @Test(dependsOnMethods = { "B", "C", "D" })
 public void A() {
  System.out.println("I am in A");
 }

 @Test
 public void B() {
  System.out.println("I am in B");
 }

 @Test
 public void C() {
  System.out.println("I am in C");
 }

 @Test
 public void D() {
  System.out.println("I am in D");
 }
}

Run 'DependentOnMethodsTest1' class, you will see below messages in console.
[RemoteTestNG] detected TestNG version 7.0.0
I am in B
I am in C
I am in D
I am in A
PASSED: B
PASSED: C
PASSED: D
PASSED: A

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


===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

As you see the console messages, you can confirm methods B, C and D are executed before method A gets executed.

if any of the dependent methods failed, then the test method will not be run and will be flagged as a SKIP.

DependentOnMethodsTest2.java

package com.sample.app.tests;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

public class DependentOnMethodsTest2 {

 @Test(dependsOnMethods = { "B", "C", "D" })
 public void A() {
  System.out.println("I am in A");
 }

 @Test
 public void B() {
  System.out.println("I am in B");
 }

 @Test
 public void C() {
  System.out.println("I am in C");
  assertTrue(false);
 }

 @Test
 public void D() {
  System.out.println("I am in D");
 }
}

As you see above snippet, test method A depend on methods B, C and D. I am failing the test method ‘C’ intentionally.

Run ‘DependentOnMethodsTest2.java’, you will see below messages in console.
[RemoteTestNG] detected TestNG version 7.0.0
I am in B
I am in C
I am in D
PASSED: B
PASSED: D
FAILED: C
java.lang.AssertionError: did not expect to find [true] but found [false]
 at org.testng.Assert.fail(Assert.java:97)
 at org.testng.Assert.failNotEquals(Assert.java:969)
 at org.testng.Assert.assertTrue(Assert.java:43)
 at org.testng.Assert.assertTrue(Assert.java:53)
 at com.sample.app.tests.DependentOnMethodsTest2.C(DependentOnMethodsTest2.java:22)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
 at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
 at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
 at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
 at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
 at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
 at java.util.ArrayList.forEach(ArrayList.java:1257)
 at org.testng.TestRunner.privateRun(TestRunner.java:770)
 at org.testng.TestRunner.run(TestRunner.java:591)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
 at org.testng.SuiteRunner.run(SuiteRunner.java:304)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
 at org.testng.TestNG.runSuites(TestNG.java:1032)
 at org.testng.TestNG.run(TestNG.java:1000)
 at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

SKIPPED: A
java.lang.Throwable: Method DependentOnMethodsTest2.A()[pri:0, instance:com.sample.app.tests.DependentOnMethodsTest2@5ce81285] depends on not successfully finished methods
 at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:99)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
 at java.util.ArrayList.forEach(ArrayList.java:1257)
 at org.testng.TestRunner.privateRun(TestRunner.java:770)
 at org.testng.TestRunner.run(TestRunner.java:591)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
 at org.testng.SuiteRunner.run(SuiteRunner.java:304)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
 at org.testng.TestNG.runSuites(TestNG.java:1032)
 at org.testng.TestNG.run(TestNG.java:1000)
 at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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


===============================================
Default suite
Total tests run: 4, Passes: 2, Failures: 1, Skips: 1
===============================================

From the console messages you can confirm that the test method A is skipped.


Previous                                                    Next                                                    Home

No comments:

Post a Comment