-->
Parameterization in TestNG assists us in passing data through code and payment hardcoding. Nevertheless, test parameters empower us for giving the values only once per execution cycle. To overcome this, we could use a DataProvider in TestNG annotations order for passing multiple parameters to a single test in a single execution.
Using DataProvider will be very easy for us to give various values in a trial for just one execution cycle. Latest quickly understand more about DataProvider in TestNG and how to efficiently use them in our test scripts for selenium test automation.
Data provider’s interest entries are a way to pass parameters in the test function, the other being test and parameters. DataProvider always gives different values to the TestNG groups cases in a single execution.
In the form of TestNG annotations order, it is a part of the inbuilt data-driven testing for which TestNG is very popular. DataProvider assists us in passing the parameter in different ways.
The TestNG example DataProvider is used in the following manner:
@ DataProvider (name = “name_of_dataprovider”)
public Object[][] dpMethod() {
return new Object [][] { values}
}
Now let us understand the different components of the syntax.
TestNG examples present additional information about the class or method we described above. It is normally represented by the ‘@’ prefix. These annotations are used by TestNG to help in developing a robust framework.
If we have read the above-said points using DataProvider is going to be very easy for us. We will start with a simple and basic data provider test first. Observe the following code, which contains the @Dataprovider.
In the above code, we are trying to pass the values “first value” and “second value” for the test method “my test” using the guidance of data provider method “method().” Refer to the syntax section for remembering the points once again.
Both the values arrived in the output, which means that even though we run the file once, the test case procedure ran twice with distinct values. Even though it is a very small and simple code.
We might know how much the codes could mess up if we have while testing. It wouldn’t be better to declare the test value and the data provider in another class and test case method into another. It is inheriting the DataProvider.
Data provider and the test case procedure could also be used in two separate classes. As it is inheriting the data provider since we are inheriting it from another file. It is expected to slightly change the above code for running the test case like this. Below provided is a @Test file :
Data Provider in TestNG
The only difference here is along with the data provider’s name that we are now required to implement the data provider class using the same attribute name.
Since we used this method for our data provider class in DP.java, we will create another DP.java file and write our data provider code. The DP.java looks like this:
Data Provider in TestNG
Now we will run the test file and see if the output is “Value Passed” or not.
Data Provider in TestNG
The output tells that the variable value “value passed” was passed by the method. In the above code, we have given a single parameter for the execution of the test case. Essentially we require much more. In the next section, we will see how to pass multiple parameters in the TestNG framework data provider.
Passing multiple parameters is a very similar process to single parameters, but we will accommodate various values in a single parameter. Let us observe the following test code, which checks if the sum of two integers is as expected or not.
Multiple Parameters in TestNG
In the above code, we have passed three values a,b and result to check if the sum is equal to the result or not.
The output will come as follows:
Multiple Parameters in TestNG
This course provides us with the switch case for checking the method’s name and returning parameters according to the method name. After running the above code, see how the output compares:
Multiple Parameters in TestNG
A single accomplishment failed where the expectation was the sum of 5 and 7 to be 9 whose failure was obliged to happen. We can see how in 5 lines, we created a data provider for two different test methods. We should notice that for every e technique added to the provided data provide the code, we need to add two lines:
case “method”:
return statement.
This could also happen in a single line as we are doing this alternative li of adding 7-8 lines typically. We can now analyze how efficient this method is.
Using Excel for DataProvider in TestNG is one of the most beneficial ways of reading the data. By doing so, our job becomes remarkably easy when we deal with vast amounts of data. For doing so, we must understand some simple steps for achieving our target of using Excel as a data provider.
It was all about the topic of DataProvider in TestNG. If we get the codes applied in this tutorial, we need to tell how efficient DataProvider is for passing the parameters. On the other hand, It will be mistaken if we assume a better choice than parameters in the TestNG framework.
Each of these concepts is used individually and depends totally upon the needs of the tester. So we should master both of them with different scenarios and different data sets.