Using the TestNG DataProvider

In the preceding RockBandsTest.java example, the dataProvider and dataProviderClass were used as attributes to the @Test method. This tells TestNG that it should extract all the sets of data in the JSON file that match the method name. In the previous chapter, we built a basic JSON DataProvider, and one of the parameters to it was the method name. TestNG passes this in when the test method is run.

Now, as far as the data is concerned, the JSON DataProvider builds a Java object on the fly and the rowID and description parameter values are stuffed into the object. That functionality was built into the DataProvider. This will be used later on for reporting purposes, but it is also handy for determining which set of data failed the test. Again, the @DataProvider annotation is used to tag the method created that fetches the data in this class.

It is also worth noting that the @Parameters annotation can be used with the @Test annotation to pass in parameters for the test method to use, but it is more useful when using them in @Before type annotations. This will be covered later on when we go over using the TestNG XML suite file parameters.

So, since we outlined the JSON data file datasets and the Java objects for the RockBandsTest.java class already in Chapter 13Building a JSON Data Provider, let's add the instances of those classes and call a method in them in the test class:

/**
* Rock Bands Test Class
*
* @author Name
*
*/
public class RockBandsTest {
// local vars
public static final String DATA_FILE = "myPath/RockBands.json";

// setup/teardown method go here
@BeforeClass(alwaysRun=true,enabled=true)
protected void testClassSetup() throws Exception {
// set data file...
JSONDataProvider.dataFile = DATA_FILE;
}

// test method using Java POJO class object
@Test(groups={"REGRESSION"},
dataProvider="fetchData_JSON",
dataProviderClass=JSONDataProvider.class,
enabled=true)
public void tc001_getBandInfo(String rowID,
String description,
JSONObject testData)
throws Exception {

// fetch object data and pass into Java object
RockBands rockBands = new RockBands(testData);

// print the key:value pairs
System.out.println(rockBands.toString() + " ");
}

// test method using Java Builder class object
@Test(groups={"REGRESSION"},
dataProvider="fetchData_JSON",
dataProviderClass=JSONDataProvider.class,
enabled=true)
public void tc002_getBandInfo(String rowID,
String description,
JSONObject testData)
throws Exception {

// fetch object data and pass into Java object
RockBandsBuilder rockBands = new RockBandsBuilder.Builder()
.name(testData.get("name").toString())
.year(testData.get("year").toString())
.song(testData.get("song").toString())
.members((JSONObject) testData.get("members"))
.build();

// print the key:value pairs
System.out.println(rockBands.toString() + " ");
}

}

Using the JSON datasets we previously outlined for the RockBandsTest.java class, the data and output would look like this for each set of data:

{  
"tc001_getBandInfo":[
{
"rowID":"tc001_getBandInfo.01",
"description":"Kiss Data",
"name":"Kiss",
"year":"1973",
"song":"Rock and Roll All Nite",
"members":{
"Vocals":"Paul Stanley",
"Bass":"Gene Simmons",
"Guitar":"Ace Frehley",
"Drums":"Peter Criss"
}
}
]
}

The output would look like this:

RockBands {name = 'Kiss', year = '1973', song = 'Rock and Roll All Nite', 
members = {"Bass":"Gene Simmons","Guitar":"Ace Frehley","Vocals":"Paul Stanley","Drums":"Peter Criss"}}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.246