TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. With the help of testNG we can achieve our testing requirements like functional, regression, sanity and end-to-end testing and more. It is an open-source automated testing framework; where NG of TestNG means Next Generation.
TestNG is a testing framework for the Java programming language created by Cédric Beust.
Benefits of using TestNG
- Reporting: Default Selenium tests do not generate a proper format for the test results. Using TestNG we can generate test results. It generate the report in a proper format including a number of test cases runs, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.
- Grouping of Test Cases: Multiple test cases can be grouped more easily by converting them into testng.xml file.
- Invocation Count: The same test case can be executed multiple times without loops just by using keyword called 'invocation count.'
- Cross browser Testing: Using testng, you can execute multiple test cases on multiple browsers.
- Integration with Maven and Jenkins: The testing framework can be easily integrated with tools like Maven, Jenkins, etc.
- Annotations: Annotations used in the testing are very easy to understand eg: @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest. It provide more annotations then JUNIT, this means more control over test and suite.
- Parallel Testing: TestNG provide support for parallel Testing. You can execute multiple threads at same time.
- Execution without main: You can execute your test without using main and by tagging the method with @Test.
- Parameterization: Parameterized tests allow developers to run the same test over and over again using different values. With this technique, you define the simple parameters in the testng.xml file and then reference those parameters in the source files.
- Data Driven Testing: You can use Data Provider in TestNG. Data Provider in TestNG is a method used when a user needs to pass complex parameters. Complex Parameters need to be created from Java such as complex objects, objects from property files or from a database can be passed by the data provider method.
- Test Prioritization: You can assign priority to test methods.
- Test Dependency: It allow dependency of one test case over other test case.
- Logs: Logs can be generated using TestNG using its reporter class.
Sample TestNG File
!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Nopackage" > <classes> <class name="NoPackageTest" /> </classes> </test> <test name="Regression1"> <classes> <class name="test.sample.ParameterSample"/> <class name="test.sample.ParameterTest"/> </classes> </test> </suite>
In above file
- A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
- A test is represented by <test> and can contain one or more TestNG classes.
- A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the <class> tag and can contain one or more test methods.
XML file contains more content this sample one, which we will discuss later in more detail.