top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Understanding basics of testng.xml and Parameterization in testng.

TestNG.xml is a configuration file used in TestNG framework and this XML file can be used to run and organize our test. Using Testng.xml, user can customize test execution settings as per his requirement and can control various aspects of their test suites .


Let us first understand how to create a TestNG XML file. There are two methods to create a TestNG XML file.


Method 1:

  • Right-click on the project folder & from the options displayed, select TestNG and then select “Convert to TestNG”

  • Click on Next

  • The next window that appears will have the refactored source code, which would be applicable after you click Finish.

  • A new file will be added to your project directory would be displayed named as testng.xml.


Method 2:

  • Right-click on Project, then select New, and finally select File.

  • Next, enter the following file name “testng.xml” and then click on Finish.

  • In this step, you will have to enter the XML code manually.

Below is the screenshot of how testng.xml file looks like


Advantages Of TestNG.xml


TestNG.xml offers several advantages in test automation, lets discuss few of them here.


  • Parallel Execution: TestNG.xml supports parallel execution of tests, allowing you to run multiple tests at the same time. Using the parallel execution in TestNG, we can allow multiple threads to run simultaneously on the test case providing independence in the execution and saving testers lot of time and effort. Below is the example testng.xml for running methods in parallel : <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name = "Parallel Testing Suite"> <test name = "Parallel Tests" parallel = "methods"> <classes> <class name = "ParallelTest" /> </classes> </test> </suite>


  • Parameterization: TestNG.xml enables parameterization of test cases, allowing you to run the same test with different input data or configurations. This enhances test coverage by testing various scenarios with different date. For example we can pass user name and password through testng.xml instead of hard coding it in test methods or we can pass browser name as parameter and pass its value in testng.xml file to execute our tests in different browsers. In the sample of testng.xml shown above, the same test scenarios are tested on two browser (Chrome and firefox). On successful execution of the suite , the test results will be shown for both the browsers.


Similarly we can pass username and password also from testng.xml. Below is the example then using @Parameter annotation at test level we can use the values at run time




@Parameters({ "username", "password" }) 
@Test
 public void testCaseTwo(String username, String password) { 
System.out.println("Parameter for User Name passed as :- " + username); 
System.out.println("Parameter for Password passed as :- " + password); 
}

As of now, Parameters have been scoped to only Suite or Test tag in testng.xml file. If the same parameter value is passed in both Suite and Test, the priority is given to parameter value passed in the Test tag.


Use of @Optional annotation

If the parameter that we defined in our method is not present in testng.xml then a testNG exception would come up and our test would skip. To overcome this situation, we can use @Optional annotation. Let us first have a look at the syntax for this:



Parameterization Using Data Providers


The DataProviders in TestNG are another way to pass the parameters in the test function. DataProviders pass different values to the TestNG Test Case in a single execution and in the form of TestNG Annotations.


To provide the test data, first declare a method that returns the data set in the form of a two-dimensional object array Object[][].

  • The first array represents a data set, whereas the second contains parameter values.

  • The DataProvider method can be in the same test class or one of its superclasses.


After adding this method, annotate it using @DataProvider to let TestNG know it is a DataProvider method.


In the below code, I am trying to pass the values of username and password to the Test method "LoginUserwithValidCredentialsValidationTest "with the help of the DataProvider method "getLoginData()".In this example the login data is stored in the excel file.


The execution of the test method is dependent upon the number of data sets defined by the @DataProvider annotated method. In the belowexample, testMethod() will be executed 10 times as I have added 10 username and passwords . So if we have entered 10 users in the excel file, the same testcase will be executed 10 times but for different credentials everytime. This type of testing is also called Data Driven Testing which can be achieved conveniently by @DataProvider annotation in our TestNG framework.




Data providers can run in parallel with the attribute parallel: It takes the value true if we want to run in parallel and value is false if we donot want to run tests in parallel.

@DataProvider(parallel = true)
@DataProvider(parallel = false)

Summary


  • Parameterization is required to create Data Driven Testing.

  • TestNG support two kinds of parameterization, using @Parameter+TestNG.xml and using @DataProvider

  • In @Parameter+TestNG.xml parameters can be placed in suite level and test level. If t he Same parameter name is declared in both places; test level parameter will get preference over suit level parameter.

  • using @Parameter+TestNG.xml only one value can be set at a time, but @DataProvider return an 2d array of Object.

48 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page