what is TestNG Annotations in Selenium Webdriver? – Learning Guide
Last updated on 15th Jul 2020, Blog, General
What is TestNG?
- TestNG is an automation testing framework in which NG stands for “Next Generation”. TestNG 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, 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.
- Suppose, you have five test cases, one method is written for each test case (Assume that the program is written using the main method without using testNG). When you 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 first three methods are anyway executed successfully. This is not possible without using TestNG.
- The TestNG provides an option, i.e., testng-failed.xml file in test-output folder. If you want to run only failed test cases means you run this XML file. It will execute only failed test cases.
Introduction to TestNG
- TestNG stands for Test Next Generation and it is an open-source test automation framework inspired by JUnit and NUnit. Well, TestNG is not just inspired but, it is an upgraded version of these two frameworks.
- So what is the upgrade here? The upgrade with TestNG is that it provides additional functionality like test annotations, grouping, prioritization, parameterization and sequencing techniques in the code which was not possible earlier.
- It not only manages test cases, but even detailed reports of tests can also be obtained by using TestNG. There will be a detailed summary which will display the number of test cases that have failed.
- Also, the bugs can be located accurately and fixed at the earliest. Now that you know what is TestNG, let’s see why to use TestNG in Selenium.
Some of the noteworthy features of TestNG are:
- Powerful and wide variety of annotations to support your test cases.
- Helps to perform parallel testing, dependent method testing.
- Flexibility of running your tests through multiple sets of data through TestNG.xml file or via data-provider concept.
- Test cases can be grouped and prioritized as per need basis.
- Provides access to HTML reports and can be customized through various plugins.
- Test logs can be generated across tests.
- Can be easily integrated with eclipse, Maven, Jenkins etc.
A basic process flow of a TestNG programs involves the following steps:
- So, before jumping onto the annotations in TestNG for Selenium, it would be better to refer the prerequisites are required to setup TestNG.
- Java Development Kit
- Setup Eclipse or any other IDE.
- Install TestNG in Eclipse or any other IDE.
Why use TestNG in Selenium?
- Software developers from around the world will unanimously agree that writing code in test cases saves a good part of their debugging time.
- Why? That is because test cases help in creating robust and error-free code by breaking the entire code into smaller test cases, and then by evaluating each of these test cases to pass/ fail conditions, you can create error-free code.
- Since Selenium does not support execution of code in test cases, TestNG comes into the picture which will help in the execution of the test cases.
Learn From Experts Selenium WebDriver Training to Build Your Skills & Knowledge
- Instructor-led Sessions
- Real-life Case Studies
TestNG also supports the following features:
- It generates the report in a proper format that includes a number of executed test cases, the number of failed test cases, and test cases that have been skipped.
- Multiple test cases can be grouped more easily by converting them into TestNG.xml file. Here, you can set the priorities to execute the test cases.
- Using TestNG, you can execute multiple test cases on multiple browsers, i.e., cross-browser testing.
- The testing framework can be easily integrated with tools like Maven, Jenkins, etc.
- Now that you know what is TestNG and why it is used, let’s move further and know the various Annotations that TestNG support in Selenium.
- Default Selenium tests do not generate a proper format for the test results. Using TestNG 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. Following are key features of TestNG
- 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.
- Multiple test cases can be grouped more easily by converting them into testng.xml file. In which you can make priorities which test case should be executed first.
- The same test case can be executed multiple times without loops just by using keyword called ‘invocation count.’
- Using testng, you can execute multiple test cases on multiple browsers, i.e., cross browser testing.
- The testing framework can be easily integrated with tools like 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.
TestNG Annotations in Selenium WebDriver Project
- In our last post on TestNG, we’d explained the three unique ways to install the TestNG plugin in Eclipse IDE. Now the next important item is to learn about the TestNG annotations.
- Annotations are nothing but a piece of instruction for the compiler that you apply to classes, methods or variables in your Java code. It is a predominant feature of the TestNG framework.
- There are multiple TestNG annotations which you can use for different tasks. It is essential to know each of the annotations so that you could apply them correctly while working on a TestNG Selenium Webdriver project.
- Let’s see what are the TestNG annotations for Selenium Webdriver and what is their objective?
- We’ve already defined the generic concept of annotations in the first paragraph. And the TestNG annotations also work on the same lines and control the execution sequence of the test code and test methods.
- We’ve drilled down the list of primary TestNG annotations along with a little detail about each of them.
List of TestNG Annotations
Selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download
|@Test||Attaches a class or a method to become the part of the test.|
|@BeforeTest||Instructs the method to run before any test method related to the classes which are inside the <test> tag as per the <testng.xml> file.|
|@AfterTest||Halts a method from execution till all the test methods finish their execution. These methods belong to the classes defined in the <test> tag of <testng.xml> file.|
|@BeforeMethod||Allows a method to run before executing any of the @test annotated methods.|
|@AfterMethod||Allows a method to take off after all of the @test annotated methods finish their execution.|
|@Parameters||You can use this annotation for passing the parameters to the test methods.|
|@DataProvider||It marks a method as a data source for the test. Every @DataProvider annotated method must always return the value as <Object[ ][ ]>. You can use it in any of the @Test annotated methods.|
|@BeforeClass||The method annotated with @BeforeClass gets executed once before the first test method of the current class.|
|@AfterClass||The method annotated with @AfterClass gets run once after finishing all the test methods in the current class.|
|@BeforeGroups||It sets up the method to run before the first test method belonging to any of the groups involved in the execution.|
|@AfterGroups||It sets up the method to run after the execution of all the test methods belonging to any of the groups participating in the test.|
|@BeforeSuite||Any such method will get called before any of the suites runs from the test.|
|@AfterSuite||Any such method will stay its execution until all other methods in the current test suite get executed.|
|@Factory||You use it to execute any specific group of test cases with different values. It returns an array of test class objects as the <Object[ ]>.|
|@Listeners||You can use them with the test classes for the logging function.|