TestNG Framework and testng.xml:
Hello dear readers,
In this blog I will be covering the overview of TestNG framework, its features, creation of testng.xml and how the testng.xml file is used for test execution and how I have implemented the same in my project.
TestNG Framework is an open source automation testing framework which is designed in JAVA language. The NG in TestNG stands for New Generation. It enables us to automate various stages of testing like unit testing, functional testing and integration testing. The picture below shows popular features of TestNG framework.
TestNG makes use of annotations for easier understanding and implementation. It supports the data driven testing which is done using @DataProvider method. It has assertions to validate the tests. It supports parallel execution of tests. It also helps in generating HTML reports to view the output of the test execution. Only one testng.xml file is needed for test suite/test case execution. These features make it more powerful for automation testing.
Now, let’s see how the tests are implemented in TestNG Framework. Test suites are collection of tests/test cases which can be run to test certain behaviors of the system or software. In TestNG the tests are implemented using the TestNG class, which is just nothing but a JAVA class which contains at least one @test method. This @test method will have the actual code to test. However, the test suite cannot be defined or created in the source code. This is achieved using the testng.xml file. It is a configuration file which allows the tester to define the test suite and control the execution of test cases. It can have more than one test suite. We can also execute the tests in parallel by using the parallel attribute in the xml file. Below is the pictorial representation of testng.xml file containing a test suite.
Now we will see how to create the testng.xml file and execute it.
Step 1: Go to your respective project folder. Right-click and select TestNG. Then click Convert to TestNG.
Step2: Generate testng.xml window will be displayed. Now enter the Test suite name and the Test name. It also shows the preview of the testng.xml file that will be generated. Click Finish.
When Finish is clicked, the testng.xml file will be generated and stored in the test-output folder as shown below. By default, it will include all the class files under src/test/java folder.
Step 3: You can modify the testng.xml file to include/remove the tests in the Test suite as per your requirement. Below picture has the sample testng.xml file I have created for LMS API Project. I have used Rest Assured for testing the LMS API and integrated with TestNG framework to automate all the four HTTP methods (GET, POST, PUT, and DELETE). I have implemented these methods inside the TestNG classes using @test methods. The Test Suite name is specified within the suite Tag. The test name is in the test tag. The TestNG class is inside the class tag ,has the actual test code for LMS API.
Step 4: Now to run the testng.xml, right click the testng.xml file and select the Run As -> TestNG Suite.
On successful execution of test suite, we can view the result of the test execution in the console or in test-output folder in the form of HTML reports.
Step 5: Refresh the project folder and go to test-output folder, then open the emailable-report.html to view your test results.
So, these are the steps to execute your test suite with the help of testng.xml. To conclude , we understand how TestNG framework provides features which makes automation simpler and easier. I hope it’s useful for you.