TestNG parameter testing example

Yet another TestNG parameter testing example, with @DataProvider.

1. CharUtil Class

Let’s say, a class to convert a character to ASCII or vice verse, how can you unit test it with TestNG?

CharUtils.java

package com.mkyong.testng.examples.parameter;

/**
 * Character Utility class
 * 
 * @author mkyong
 * 
 */
public class CharUtils {
	/**
	 * Convert the characters to ASCII value
	 * 
	 * @param character character
	 * @return ASCII value
	 */
	public static int CharToASCII(final char character) {
		return (int) character;
	}

	/**
	 * Convert the ASCII value to character
	 * 
	 * @param ascii ascii value
	 * @return character value
	 */
	public static char ASCIIToChar(final int ascii) {
		return (char) ascii;
	}
}

2. TestNG @DataProvider Example

To test it, create a @Test method which accept two parameters (character and expected ASCII), and the test data is passing from data provider.

CharUtilsTest.java

package com.mkyong.testng.examples.parameter;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
 * Character Utils Testing
 * @author mkyong
 *
 */
public class CharUtilsTest {
 
	@DataProvider
	public Object[][] ValidDataProvider() {
		return new Object[][]{
			{ 'A', 65 },{ 'a', 97 },
			{ 'B', 66 },{ 'b', 98 },
			{ 'C', 67 },{ 'c', 99 },
			{ 'D', 68 },{ 'd', 100 },
			{ 'Z', 90 },{ 'z', 122 },
			{ '1', 49 },{ '9', 57 }
		};
	}
 
	@Test(dataProvider = "ValidDataProvider")
	public void CharToASCIITest(final char character, final int ascii) {
		
		   int result = CharUtils.CharToASCII(character); 
		   Assert.assertEquals(result, ascii);
		   
	}
 
	@Test(dataProvider = "ValidDataProvider")
	public void ASCIIToCharTest(final char character, final int ascii) {
		
		   char result = CharUtils.ASCIIToChar(ascii); 
		   Assert.assertEquals(result, character); 
		   
	}
}

Result


PASSED: CharToASCIITest(A, 65)
PASSED: CharToASCIITest(a, 97)
PASSED: CharToASCIITest(B, 66)
PASSED: CharToASCIITest(b, 98)
PASSED: CharToASCIITest(C, 67)
PASSED: CharToASCIITest(c, 99)
PASSED: CharToASCIITest(D, 68)
PASSED: CharToASCIITest(d, 100)
PASSED: CharToASCIITest(Z, 90)
PASSED: CharToASCIITest(z, 122)
PASSED: CharToASCIITest(1, 49)
PASSED: CharToASCIITest(9, 57)
PASSED: ASCIIToCharTest(A, 65)
PASSED: ASCIIToCharTest(a, 97)
PASSED: ASCIIToCharTest(B, 66)
PASSED: ASCIIToCharTest(b, 98)
PASSED: ASCIIToCharTest(C, 67)
PASSED: ASCIIToCharTest(c, 99)
PASSED: ASCIIToCharTest(D, 68)
PASSED: ASCIIToCharTest(d, 100)
PASSED: ASCIIToCharTest(Z, 90)
PASSED: ASCIIToCharTest(z, 122)
PASSED: ASCIIToCharTest(1, 49)
PASSED: ASCIIToCharTest(9, 57)

===============================================
    com.mkyong.common.CharUtilsTest
    Tests run: 24, Failures: 0, Skips: 0
===============================================
More Parameter Examples.
For more examples, please refer to this TestNG parameter test with XML and DataProvider.

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Alex Gladyshev
7 years ago

Mkyong, thank you for your wonderful tutorials, you are a true modern-day hero!

Kuldeep Rana
8 years ago

Thanks MKYong, used the similar approach to create data driven test in Selenium projects like this – http://artoftesting.com/automationTesting/data-driven-framework-in-selenium-using-testng.html

Vinay
12 years ago

Hi
How can I use @dataprovider with file 10 or say 15 columns.

puspita parida
12 years ago

How can I automate the curl using testNG ??

Vishwa
13 years ago

Hi MKYONG,

It is a great learning.
sometimes these examples work like a boost for us.
just solved a problem using your examples in a very quick.

Thanks,
Vishwa

Sourabh Jain
13 years ago

I think the ValidDataProvider function should look like this. (No name attribute in @DataProvider annotation)

@DataProvider(name="ValidDataProvider")
	public Object[][] ValidDataProvider() {
		return new Object[][]{
				{ 'A', 65 },{ 'a', 97 },
				{ 'B', 66 },{ 'b', 98 },
				{ 'C', 67 },{ 'c', 99 },
				{ 'D', 68 },{ 'd', 100 },
				{ 'Z', 90 },{ 'z', 122 },
				{ '1', 49 },{ '9', 57 },
 
		};
	}
pradeek
14 years ago

Hi i have some doubts on testng suite running which i am doing keyword driven test using spread sheet,i am using complete modularity in my programs so i created my each test in a class so i can call to my main class,but the problem is each class has dependencies with other class,so how i can call these tests in to my main class for running as suite.

Thanks
Pradeek
http://programnerds.blogspot.com

batii
14 years ago

It’s a nice example.
I found another at http://shyarmal.blogspot.com/2011/07/testng-dataprovider.html.

pan
16 years ago

Nice one

MK Singh
4 years ago
Reply to  pan

Mkyong, thank you for your wonderful tutorials, you are a true modern-day hero!