Over a million developers have joined DZone.
Platinum Partner

TestNG Dependency Test – Inheited Test Dependency

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Dependency is a feature in TestNG that allows a test method to depend on a single or a group of test methods. This will help in executing a set of tests to be executed before a test method.

Dependency on test methods can only be mentioned for test methods that belong to the same class or any of the inherited base classes.

The following example shows a test class where dependent methods are part of the inherited base class.

Code

Base Test Class – BaseTest

package com.skilledmonster.example;
import org.testng.annotations.Test;
/**
* Example to demonstrate TestNG inherited dependency method execution
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class BaseTest {
@Test
public void start() {
System.out.println("Starting the server");
} 
}

Inherited Test Class – InheritedDependencyTest extends BaseTest

packagecom.skilledmonster.example;
importorg.testng.annotations.Test;
/**
* Example to demonstrate TestNG inherited dependency method execution
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
publicclassInheritedDependencyTest extendsBaseTest {
@Test(dependsOnMethods = { "start"})
publicvoidprocess() {
System.out.println("Processing the data!");
}
@Test(dependsOnMethods = { "process"})
publicvoidstop() {
System.out.println("Stopping the server");
}
}

Output

testng_dependency_inheited

As seen in the above console output, start() method which is part of the BaseTest class is executud first before executing process() method.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Published at DZone with permission of Jagadeesh Motamarri , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}