DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Test Driven Development Using TestNG, Gradle
  • Selenium WebDriver and TestNG: Find Perfect Match for Automation Testing
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • The Cypress Edge: Next-Level Testing Strategies for React Developers

Trending

  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Unlocking AI Coding Assistants Part 2: Generating Code
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. TestNG: Run Tests Sequentially With @DataProvider Inside One Test Class

TestNG: Run Tests Sequentially With @DataProvider Inside One Test Class

By 
Alexey Zvolinskiy user avatar
Alexey Zvolinskiy
·
Jan. 22, 14 · Interview
Likes (1)
Comment
Save
Tweet
Share
42.4K Views

Join the DZone community and get the full member experience.

Join For Free

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 @Testannotation 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" >

<suite name="LocalSuite" verbose="1">
	<test name="ParticularTest" group-by-instances="true">
		<classes>
			<class name="kill.me.later.SampleFactory"></class>
		</classes>
	</test>
</suite>

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.

Testing TestNG

Published at DZone with permission of Alexey Zvolinskiy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Test Driven Development Using TestNG, Gradle
  • Selenium WebDriver and TestNG: Find Perfect Match for Automation Testing
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • The Cypress Edge: Next-Level Testing Strategies for React Developers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: