Testng annotations parameters
Hem / Teknik & Digitalt / Testng annotations parameters
This attribute has a default value of .
Parameters from System Properties
TestNG can be passed parameters on the command line of the Java Virtual Machine using system properties ().
No way!! The same logic applies to TestNG. Here is an example:
TestNG - Basic Annotations - Parameters
@Parameters describes how to pass parameters to a @Test method. Try to run it like this. By defining the parameters in the suite level, you can not only pass the same values in the different methods of the same class but also on different methods in different classes.
Below I have created another file that contains another method called 'Multiply' to multiply two numbers.
Note: that this class also contains the same parameters variable names as in the TestNG class we declared above.
Since we want to run all the three functions (Sum, Diff, and Mul) using the same set of variables, our XML file will look like this:
Execute this TestNG suite to see that all the three methods have run even though being in different classes.
As expected, the second test executed with the optional parameters as I declared no parameters in the XML file.
It brings us to the end of this tutorial about a very comfortable topic that is very easy to understand. TestNG will use the test context for the injection. If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the attribute:
The data provider supports injection too.
But what if again at some point I want to add 3 and 5, then again 5 and 8 and so on. TestNG gives preference to the Parameters defined at the test level over the parameters set at the suite level.
Optional Parameters In TestNG
Optional parameters are yet another type of parameter which kind of acts like the "default" attribute of the switch case in programming languages.
For these new features to be consumed, update your suite file to use the (as seen below) so that your IDE can provide you with autocompletion:
- When this attribute is set to true at the suite level, TestNG will start using a shared thread pool for all the data driven tests in a given . This method is annotated with :
A method specifies its Data Provider with the attribute.
The size of the thread pool is determined using the attribute .
As you see, we achieved this by defining the values just once. This is particularly useful if you have a lot of parameter sets to pass to the method and you don’t want to create all of them upfront.
An array of objects (). You can modify this value in the tag of your XML file:
If you want to run a few specific data providers in a different thread pool, you need to run them from a different XML file.
Retries and data providers
TestNG allows you to retry a data provider incase it has encountered any issues when calling it the first time.
This is similar to how regular test methods can be retried as explained in this section.
To be able to retry a data provider, the following needs to be done.
First we would need to implement the interface .
Next you would need to tie this implementation to the data provider annotation using the attribute of the annotation.
With that we can now retry a failed data provider.
Here’s a sample retry implementation:
Here’s how a test class that consumes this retry mechanism can look like:
And when you run this sample, the output would look something like below:
Controlling ThreadPool Usage
Starting from TestNG , there are some additional ways in which the thread-pools that run the parallel tests can be controlled.
So, if this method gets nothing passed onto the , this message will be saved inside the string variable message.
In the XML file, I have passed the parameters only in the second method.
Run the above TestNG test suite and see how the output comes onto the console. The only limitation is that in case of iterator its parameter type can’t be explicitly parameterized itself.
TestNG Parameters
In the test environment, as well as the development environment, we often create functions. This is similar to but causes the test method to be invoked once for each element of the source array.
An . It's run on different values to give the best results like entering different usernames etc.