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...