TestNG is an automation testing framework in which NG stands for “Next Generation”. TestNG is inspired by JUnit which uses the annotations (@). TestNG overcomes the disadvantages of JUnit and is designed to make end-to-end testing easy.
Using TestNG, you can generate a proper report, and you can easily come to know how many test cases are passed, failed, and skipped. You can execute the failed test cases separately.
Annotations in TestNG are lines of code that can control how the method below them will be executed. They are always preceded by the @ symbol.
Use of TestNG with Selenium:
Default Selenium tests do not generate a proper format for the test results. Using TestNG in Selenium, we can generate test results.
Most Selenium users use this more than Junit because of its advantages. There are so many features of TestNG, but we will only focus on the most important ones that we can use in Selenium. The following are the key features of Selenium TestNG:
- Generate the report in a proper format including the number of test case runs, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.
- Multiple test cases can be grouped more easily by converting them into testng.xml files. In which you can make priorities on which test case should be executed first.
- The same test case can be executed multiple times without loops just by using a keyword called ‘invocation count.’
- Using TestNG, you can execute multiple test cases on multiple browsers, i.e., cross-browser testing.
- The TestNG framework can be easily integrated with tools like TestNG Maven, Jenkins, etc.
- Annotations used in the testing are very easy to understand ex: @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest
- WebDriver has no native mechanism for generating reports. TestNG can generate the report in a readable format like the one shown below.
- TestNG simplifies the way the tests are coded. There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static.
- Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely.
Advantages of TestNg over JUnit
Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely.
- Annotations are easier to understand
- Test cases can be grouped more easily
- Parallel testing is possible