TestNG methods can have arguments also. There are two ways we can inject method arguments:
- Using
@DataProvider
annotation – I have explained about it in TestNG DataProvider tutorial. - 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://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.
We will get following output in eclipse console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[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.
1 2 3 4 |
$java -cp .:../../lib/testng-6.14.3.jar:../../lib/jcommander-1.72.jar:../../lib/bsh-2.0b6.jar org.testng.TestNG test_parameters.xml <img class="alignnone wp-image-33715 size-full" src="https://all-learning.com/wp-content/uploads/2018/05/testng-xml-suite-run-from-command-lines.png" alt="testng-xml-suite-run-from-command-line" width="1200" height="628" /> |
That’s all for TestNG Parameters example.