TestNG
TestNG is an Automation Testing Framework in which NG stands for "Next Generation". It is inspired from JUnit which uses the annotations(@). TestNG overcomes the disadvantages of JUnit and is designed to make end to end testing easy.
Using TestNG, we can generate a proper report, and we can easily come to know how many test cases are passed, failed and skipped. we can execute failed test scripts separately.
Example:
Suppose, we have five test scripts, one method is written for each test case (Assume that the program is written using the main method without using TestNG). When we run this program first, three methods are executed successfully, and the fourth method is failed. Then correct the errors present in the fourth method, now you want to run only fourth method because firs three methods are anyway executed successfully. This is not possible without using TestNG.
Note :
The TestNG in Selenium provides an option, i.e, testing -failed.xml file in test output folder. If we want to run only failed test cases means we run this XML file. It will execute only failed test cases.
Advantages of TestNG over JUNIT :
- Annotations are easier to understand
- Test scripts can be grouped more easily
- Parallel testing is possible.
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.
Example:
}
Note : Ability to run tests in parallel is available in TestNG but not in JUnit, so the TestNG framework is more preferred of testers using selenium grid.
Features Of TestNG
Default Selenium tests do not generate a proper format for the test results. Using TestNG in Selenium, we can generate test results.
Following are the features:
1. Generate the report in a proper format including a number of test cases runs, the number of test cases passed, the number of test scripts failed and the no of test script skipped.
2. Multiple test cases can be grouped more easily by converting them into testing.xml file. In which we can make priorities which test cases should be executed first.
3. The same test case can be executed multiple times without loops just by using keyword called invocation count.
4. Using TestNg, We can execute multiple test cases on multiple browsers, i.e. cross browser testing
5. The TestNg framework can be easily integrated with tools like TestNG Maven, Jenkins etc...
6. Annotations used in the testing are very easy to understand.
7. WebDriver has not native mechanism for generating reports, TestNg can generate the report in a readable format.
8. 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.
9. Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.
Annotations :
@BeforeSuite : The annotated method will be run before all tests in this suite have run.
@AfterSuite : The annotated method will be run after all tests in this suite have run.
@BeforeTest : The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest : The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeGroups : The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups : The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass : The annotated method will be run before the first test method in the current class is invoked.
@AfterClass : The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod : The annotated method will be run before each test method.
@AfterMethod : The annotated method will be run after each test method.
@Test : The annotated method is a part of a test case.