Pass HashMap to DataProvider

2,393 views
Skip to first unread message

shreeji...@gmail.com

unread,
Nov 5, 2015, 11:36:55 PM11/5/15
to testng-users
Hello All,

I am basically looking if a Hash Map can be provided to a @Test annotated method in TestNG.
I am implementing a greedy provider with all my test data extracted from an excel spreadsheet in a Hash Map.

Each key in the Map is a test method in a business sense.

So ideally i want to do something like

@Test
runTest(HashMap data)
such that the test method gets called repeatedly for each key in the HashMap
and the test console prints out X number of tests executed (where X would be the number of keys in the Map).
Please let me know if there is any such feature available.

Currently, i am looping through all the keys in the Map within the test method to perform my steps
and my console prints 1 test method executed which is ideally not what i would like.

Regards
Shreejit

Krishnan Mahadevan

unread,
Nov 8, 2015, 2:02:05 AM11/8/15
to testng...@googlegroups.com
Shreejit,

I haven't understood your usecase well enough. So you would need to explain a bit more with some examples as to what exactly are you trying to do with your excelsheet and how it relates to a Map. Feel free to add more context.
That said and done, is the below sample something that you are looking for ?

Here I am assuming that the entire data set from a datasource such as xls will be one Map, wherein every row will correspond to a key in the Map, and the values represents the column values for each row.

public class HashmapsAsDataLazy {
private Collection<String> values;
private String testName;

@Test (dataProvider = "insideDp")
public void testMethod(String eachValue) {
Reporter.log(testName + " has its value was " + eachValue, true);
}

@DataProvider (name = "insideDp")
public Object[][] getMoreData() {
Object[][] testData = new Object[values.size()][1];
int i = 0;
for (String eachValue : values) {
testData[i++][0] = eachValue;
}
return testData;
}

@Factory (dataProvider = "dp")
public HashmapsAsDataLazy(String testName, Collection<String> values) {
this.values = values;
this.testName = testName;
}

@DataProvider (name = "dp")
public static Object[][] getData() {
Map<String, String> object1 = new HashMap<>();
object1.put("height", "5.5 ft");
object1.put("weight", "45 kgs");

Map<String, String> object2 = new HashMap<>();
object2.put("age", "2");
object2.put("weight", "45 kgs");

Map<String, String> object3 = new HashMap<>();
object3.put("color", "brown");
object3.put("orientation", "portrait");

return new Object[][] {
{"object1", object1.values()},
{"object2", object2.values()},
{"object3", object3.values()}
};
}
}

Output :

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running organized.chaos.testng.HashmapsAsDataLazy
Configuring TestNG with: TestNG652Configurator
object1 has its value was 45 kgs
object1 has its value was 5.5 ft
object3 has its value was portrait
object3 has its value was brown
object2 has its value was 45 kgs
object2 has its value was 2
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.48 sec - in organized.chaos.testng.HashmapsAsDataLazy

Results :

Tests run: 6, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.917 s
[INFO] Finished at: 2015-11-08T12:26:00+05:30
[INFO] Final Memory: 22M/174M
[INFO] ------------------------------------------------------------------------

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages