Jagadeesh is a DZone MVB and is not an employee of DZone and has posted 27 posts at DZone. You can read more from them at their website. View Full User Profile

TestNG Depedency Test – Multiple Test Method Dependency

09.22.2013
| 4253 views |
  • submit to reddit

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.

The dependency on multiple test methods is configured for a test by providing comma separated dependent test method names to the attribute dependsOnMethods while using the Test annotation.

The following example shows a test class where process() test method depends on multiple test methods start() and initi() of the same class.

Code

?
package com.skilledmonster.example;
import org.testng.annotations.Test;
/**
* Example to demonstrate TestNG multiple dependency method execution
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class MultipleDependencyTest {
@Test
public void start() {
System.out.println("Starting the server");
}
@Test(dependsOnMethods = { "start" })
public void init() {
System.out.println("Initializing the data for processing!");
}
@Test(dependsOnMethods = { "start", "init" })
public void process() {
System.out.println("Processing the data!");
}
@Test(dependsOnMethods = { "process" })
public void stop() {
System.out.println("Stopping the server");
}
}

Output


TestNG Depedency Test - Multiple Test Method Dependency - Output

As seen in the above console output, process() method executed after start() and init() methods are executed and like wise stop() method is executed after process()  method is executed.

Download

[GitHub]

Published at DZone with permission of Jagadeesh Motamarri, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Genzer Hawker replied on Sun, 2013/09/22 - 9:34pm

Isn't one of the characteristics of good unit tests is independent?

Martin Vaněk replied on Mon, 2013/09/23 - 6:25am in response to: Genzer Hawker

I haven't seen unit test written in article. He is starting and stopping server, so it is quite clear he is planing to do integration test. 

Maven lifecycle phases can be used to start/stop server but then testing will become dependent on maven...

Balázs Bessenyei replied on Tue, 2013/09/24 - 12:24am in response to: Genzer Hawker

Yes it is, but this is test is clearly intended to be an integration test. On a related note, the same method can be used to create component tests as well. 

Jagadeesh Motamarri replied on Mon, 2013/10/07 - 7:17am in response to: Genzer Hawker

You are correct. Good unit tests are always independent. TestNG is a very good framework to perform integration tests as well. I should have clearly pointed at that out in the article. Let me see if I can update the article and add a clear note.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.