Welcome aboard to the TestNG Framework tutorial from ToolsQA. TestNG is an open-source testing framework that has evolved as the testing standards and practices evolved over the years. Moreover, the TestNG Framework covers a wide area of tests such as unit tests, functional tests, end-to-end tests, integration tests, etc. At the time of writing this tutorial, we are working on TestNG 7.0.0 version. Designed “out of frustration from JUnit deficiencies,” the TestNG Framework today stands strong among the bouquet of java testing framework, and the community is strong as well. Subsequently, in this tutorial, we will brief you about the TestNG Framework in general and make you a little familiar with its powerful features, such as annotations and listeners. Indexing the main points of this tutorial, we will be discussing:
- What is TestNG or TestNg Framework?
- Advantage Of TestNG Over Junit.
- How to Write TestNG Testcases?
- Basic TestNG Annotations.
- TestNG Listeners
- TestNG Framework With Selenium.
What is TestNG or TestNG Framework?
TestNG is a testing framework for performing testing in the java programming language and is inspired by JUnit and NUnit. It helps in performing java tests with enhanced functionality and easy-to-use interface with a lot more added functionalities and overcomes JUnit’s deficiencies.
Additionally, the TestNG Framework is an open-source automated testing framework, where NG of TestNG means Next Generation. The TestNG Framework is better than JUnit, especially when testing integrated classes. Pay special thanks to Cedric Beust, who is the creator of TestNG!! How and in what ways it is powerful, is discussed in the next section.
In the test area, the TestNG testing framework provides the ability to the user to write more flexible tests with the help of the TestNG Framework’s annotations, grouping, sequencing, and parametrizing. It gives the TestNG Framework an upper hand over JUnit. Additionally, the TestNG Framework provides a lot of benefits for the tester’s community. We will list them out in the next section.
What are the Benefits of TestNG Over JUnit?
As we mentioned in the introductory section, TestNG is more advanced than JUnit. Below we have listed the significant benefits of TestNG over JUnit and a brief explanation of its features.
1. TestNG gives the ability to produce HTML Reports of execution. In other words, these reports contain detailed test case results that we can distribute to other team members. TestNG reports are generated automatically and are of two types, emailable reports, and the index report. Subsequently, the detailed discussion on them is in the TestNG Reports post.
2. TestNG Annotations are very easy to use. In a way, these annotations indicate TestNG about the point when these tests should execute. In TestNG, the tester gets advantages of a lot of annotations and can use them according to their need. Moreover, TestNG annotations are written in English and denote their literal meaning, which makes it easier to remember. For example, @BeforeMethod means executing before the test method, etc. To know more, visit TestNG Annotations.
3. Test cases can be Grouped & Prioritized more easily. That is to say, Grouping the tests in TestNG allows the tester to execute a bunch of tests altogether with just one line of code in the XML file. Since we also need a method to define the order of execution of tests, TestNG helps us by providing a parameter called “priority.” By providing the priority to a test case and assigning it, some integer value changes the order of the sequence of test execution. An example below denotes the same scenario. To know more, you can visit what are groups in TestNG and how to create prioritized tests in TestNG?
4. Parallel testing is possible with TestNG Framework. Parallel testing refers to the creation of threads and running different methods in different threads parallelly. TestNG also gives us the facility to run not only methods but also classes and even suites parallelly. It saves time and effort for a tester. You can learn more about Parallel Testing and Threads by visiting the TestNG parallel execution post.
5. TestNG Framework helps in generating logs. User-defined logs are anything that the tester wants to retain onto the reports. Additionally, these logs get printed over the reports as well as the console when we execute tests in TestNG Framework. These logs play a very vital role when other team members or the tester wants to study these tests after some time. To know more, you can visit How to generate reports in TestNG?
6. Data Parameterization is possible in TestNG Framework. With data parameterization, the tester can pass the data onto the parameters, which help in running the same method again and again with different data values. Moreover, Data parameterization saves a lot of time since the tester need not write multiple tests with different values. Additionally, to know more, you can visit what are parameters in TestNG and How to use them?
These were just the basic concepts available in the TestNG Framework, which makes it stand apart from the JUnit and other similar frameworks. While we move ahead with the course, we will not only discover a lot more topics and features but also study these above-given features in detail, including the exceptions. In the next section, we will see the typical flow followed while writing the test cases in the TestNG Framework.
Test Case Writing Process In TestNG Framework
Writing a TestNG Test is quite simple. The basic flow of writing to running the tests is as below.
Writing the test cases in TestNG involves the following steps:
- 1st Step – Write the business logic of the test.
- 2nd Step – Insert TestNG annotations in the code.
- 3rd Step – Add the information about your test (e.g., the class names, methods names, groups names, etc.) in a testng.xml file.
- 4th Step – Run TestNG tests.
TestNG Annotations
TestNG’s main power comes from its annotations. TestNG annotations are very easy to use and help the testers provide commands to run the tests accordingly.
A tester can run multiple annotations in a single test. Below is the given TestNG annotations list:
- @BeforeSuite: TestNG BeforeSuite method will run before all tests in this suite have run.
- @AfterSuite: TestNG AfterSuite method will be run after all tests in this suite have run.
- @BeforeTest: TestNG BeforeTest method will execute before any test method belonging to the classes inside the tag is run.
- @AfterTest: TestNG AfterTest method will 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 execute before. TestNG BeforeGroups method is ensured to run immediately before the first test method that belongs to any of these groups is invoked.
- @AfterGroups: The list of groups that this configuration method will execute after. TestNG AfterGroups method is confirmed to run immediately after the last test method that belongs to any of these groups is invoked.
- @BeforeClass: TestNG BeforeClass method will run before the first test method in the current class invokes.
- @AfterClass: TestNG AfterClass method will run after all the test methods in the current class run.
- @BeforeMethod: TestNG BeforeMethod method will run before each test method.
- @AfterMethod: TestNG AfterMethod method will run after each test method.
- @Test: This annotated method is a part of a test case.
TestNG Listeners
TestNG listeners are one of the most used features that come bundled with TestNG. Its listeners are the piece of code that “listens” to certain events and act accordingly. TestNG listeners have a lot of advantages in the field of testing as they change the default behavior of TestNG in many aspects. TestNG Framework provides us with a lot of TestNG listeners that are discussed in-depth in the What is TestNG Listeners tutorial. Here is the list of listeners which are provided by TestNG Framework:-
- ITestListener
- IReporter Listener
- ISuiteListener
- IInvokedMethod Listener
- IHookable Listener
- IConfigurationListener
- IConfigurableListener
- IAnnotationTransformer Listener
- IExecution Listener
- IMethodInterceptor Listener
Researching the TestNG Framework and hearing it from your friends, you will encounter that they are used quite heavily with Selenium. Selenium, if you are unaware, is a web driver that performs efficient tests that run on a browser. Watching this on so many sources, a question might pop-up in your mind that should I use TestNG with Selenium? Or how can I use TestNG? Let’s answer this question.
Should I Use TestNG Framework With Selenium?
TestNG Framework is more straightforward, more comfortable, and friendlier when used with Selenium. In the tester’s community, too, TestNG with Selenium is a popular combo, and they use a lot of the TestNG’s features very effectively with Selenium. Therefore, in the course of this tutorial, we will use Selenium’s written tests a lot (and sometimes plain TestNG-Java tests). It does not convey that we have to use TestNG with Selenium only. As you will see during this tutorial, TestNG can be used with whatever type of testing the tester prefers. TestNG is nowhere related to Selenium, but knowing Selenium is an added advantage for the tester.
On the other hand, if you are efficient in Selenium, then you must know that Selenium, in particular, does not have a specific feature to generate test results. TestNG Framework can help testers in this regard and generate test reports along with Selenium to analyze the tests done by the web driver. TestNG also helps in handling the uncaught exceptions while writing the test cases. Don’t worry! We will have a dedicated section in the course for using the TestNG Framework with Selenium.
Conclusion
TestNG developed as a framework that could overcome the anomalies of JUnit, which was an already existing framework in Java. Once the TestNG Framework released, it took the tester’s community by storm. As it turned out, not only Beust, but a lot of testers were frustrated by JUnit, and since the TestNG Framework resembled JUnit, nobody had a problem moving to this Framework.
TestNG has a lot of features, one of the most powerful ones being the annotations. Its annotations help the tester determine the meaning of the test method underneath. TestNG parameters work as steroids with annotations and help in the creation of efficient and meaningful test code without writing much logic. Along with this, TestNG provides listeners who listen to the events and execute the code associated with that event. TestNG Framework has strong community support today, and if you are thinking of starting your career journey into it, I will try to provide you with the best content possible on this platform.