TestNG Parameters – DataProvider and XML

Filed Under: TestNG

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.


package com.journaldev.parameters;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestParameters {

	@Test
	@Parameters("arguments")
	public void singleParameterTest(String s) {
		System.out.println("Testing for input parameter = " + s);
	}

	@Test
	@Parameters({ "id", "name" })
	public void multipleParameterTest(int id, String s) {
		System.out.println("Testing for multiple input parameter = " + id + " and " + s);
	}

	@BeforeSuite
	@Parameters("before_suite")
	public void beforeSuite(String s) {
		System.out.println("Before Suite Parameter = " + s);
	}

	@AfterSuite
	@Parameters("after_suite")
	public void afterSuite(@Optional("Default Parameter") String s) {
		System.out.println("After Suite Parameter = " + s);

	}

}

Here is the test suite xml file:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite" guice-stage="DEVELOPMENT">
	<parameter name="before_suite" value="My Before Suite Parameter"></parameter>
	<parameter name="id" value="100"></parameter> <!-- Suite Level Parameters -->
	<test thread-count="5" name="Default test" verbose="2">
		<parameter name="arguments" value="Pankaj"></parameter>
		<parameter name="name" value="JournalDev"></parameter> <!-- Test Level Parameters -->
		<classes>
			<class name="com.journaldev.parameters.TestParameters" />
		</classes>
	</test>
</suite>

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 Suite Eclipse run as TestNG Suite

We will get following output in eclipse console.


[RemoteTestNG] detected TestNG version 6.14.3
Before Suite Parameter = My Before Suite Parameter
Testing for multiple input parameter = 100 and JournalDev
Testing for input parameter = Pankaj
PASSED: multipleParameterTest(100, "JournalDev")
PASSED: singleParameterTest("Pankaj")

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

After Suite Parameter = Default Parameter

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

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.


$java -cp .:../../lib/testng-6.14.3.jar:../../lib/jcommander-1.72.jar:../../lib/bsh-2.0b6.jar org.testng.TestNG test_parameters.xml

TestNG XML Suite run from command line

That’s all for TestNG Parameters example.

You can download example code from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages