Alexey Zvolinskiy

About Alexey Zvolinskiy

Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

TestNG: Run tests sequentially with @DataProvider inside one test class

Many java developers and automation test engineers use TestNG as a testing framework in their job. I’m not an exception. This is an obvious choice because TestNG provides very powerful set of tools which makes working with all kinds of tests easier. To prove this I’ll show you in this article how can be solved one not trivial task.

The problem

How to run tests within a single class in particular order with different data sets? Well looks like I exposed a formulation of the problem in one sentence. But if you ask me to present this sentence in a more strict form I’ll provide the following list:

  • Multiple test methods
  • One test class
  • Sequence run
  • Different data sets for each test method

Summarizing here is an abstract schema of the problem:

TestClass {
firstTest(String testData)
secondTest(String testData)
thirdTest(String testData)
}

TestDataSets {
“string 1″
“string 2″
}

Running of these tests should leads to the result:

firstTest(string 1)
secondTest(string 1)
thirdTest(string 1)

firstTest(string 2)
secondTest(string 2)
thirdTest(string 2)

After the problem was highlighted and explained, we can go ahead to its solution.

TestNG realisation

I’ll use the most simplified code constructions but you can use such approach customizing it with some specific logic.

package kill.me.later;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

public class SomeTest {

	private int id = 0;
	private String account = "";

	public SomeTest(int id, String account) {
		this.id = id;
		this.account = account;
	}

	@Test
	public void firstTest() {
		System.out.println("Test #1 with data: "+id+". "+account);
		assertTrue(true);
	}

	@Test
	public void secondTest() {
		System.out.println("Test #2 with data: "+id+". "+account);
		assertTrue(true);
	}

	@Test
	public void thirdTest() {
		System.out.println("Test #3 with data: "+id+". "+account);
		assertTrue(true);
	}

}

Examining the code above, everyone can notice that I use a regular TestNG @Test annotation applied to void methods. Also I declared a constructor, but its purpose will be discussed later. TestNG has very useful annotations – @Factory and @DataProvider. I recommend to read about them on the official TestNG documentation site. While you are reading about these annotations I’ll proceed with practical part:

package kill.me.later;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;

public class SampleFactory {

	@Factory(dataProvider="dp")
	public Object[] createInstances(int id, String account) {
		return new Object[] {new SomeTest(id, account)};
	}

	@DataProvider(name="dp")
	public static Object[][] dataProvider() {
		Object[][] dataArray = {
				{1, "user1"},
				{2, "user2"}
		};
		return dataArray;
	}

}

The last code snippet provides run of each test method from the SomeTest class with data sets declared in the dataProvider. But if you try to run the SampleFactory class with help of TestNG you will not get the execution order of the test methods from the “The problem” section. In order to achieve the sequential execution test methods order you need to use TestNG XML launcher:

< !DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

Pay your attention to the group-by-instances parameter. Exactly it provides so desirable sequence order for the test methods execution. So now you can easily organize your tests for this kind of DDT runs.
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


1 × one =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close