TestNG methods can have arguments also. There are two ways we can inject method arguments:

  1. Using @DataProvider annotation – I have explained about it in TestNG DataProvider tutorial.
  2. Using @Parameters annotation – this annotation allows us to inject parameters from TestNG XML suite file. We will focus on this annotation in this tutorial and learn how to use it.

TestNG Parameters Annotation

Some important points about @Parameters annotation are:

  • TestNG @Parameters annotation can be applied on @Before, @After and @Test methods.
  • If we want to keep changing our test method inputs, this is the preferred way because we don’t need to compile our test classes again.
  • @Parameters annotation requires us to provide string array of parameter names to be looked in the test suite xml file. The number of parameters should match the number of arguments in the method.
  • We can use @Optional with method argument to provide a default value, when the parameter is missing from test suite xml file.

Parameters in TestNG suite XML file can be defined at suite level or test level. If there are parameters with the same name, then test parameters take precedence and override the value.

TestNG Parameters Example

Now that we have gone through the theory part, let’s look into a complete example of TestNG test class where we will use @Parameters annotation.

Here is the test suite xml file:

TestNG Parameters XML Suite Test

If you are using Eclipse TestNG plugin, then you can run the test suite XML file directly as shown in below image.

TestNG-XML-Eclipse-Run-As-TestNG-Suite

We will get following output in eclipse console.

We can also run the test suite XML file from command line through org.testng.TestNG class. All your test classes and TestNG jars should be in the classpath.

That’s all for TestNG Parameters example.

By admin

Leave a Reply

%d bloggers like this: