PAGE 1 : INTRODUCTION TO TestNG

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that make it more powerful and easier to use.

It is an open source automated testing framework; where NG of TestNG means Next Generation. 'Cedric Beust' is the creator of TestNG.



Before we learn how to use TestNG, let’s take a look at some of the advantages of using this testing framework:
  • Simplified annotations :Annotations have been simplified, making it easier for testers to understand them.
  • Results in HTML :It generates reports in HTML.
  • Multiple test-cases at once :Using TestNG Suite (testing.xml), which is essentially an xml file, testers can execute a number of test cases at a time. In this xml file, users need to mention the number of classes and test-cases they wish to execute.
  • Runs failed test-cases :TestNG can also be used to run failed test-cases. This is one of most important advantages of TestNG over JUnit.
  • Allows grouping : TestNG allows supports grouping. Using this feature, testers can group test-cases without too much effort, which was not possible with JUnit.
  • Allows running on multiple browsers :TestNG allows testers to execute and run one script in multiple browsers.
  • Parametric testing : Most of the time, testers have to execute a large number of varied tests, mainly due to the nature of their business logic. However, this lengthy process can be eliminated as parametric testing allows you to run the same test a number of times by simply changing the values. It allows you to pass parameters to your test methods in two ways: parameter and @dataprovider.
  • Bypassing or ignoring test-cases :This feature is useful if you do not want execute a particular testcase(s). In such instances, TestNG, with the help of annotation @Test(enabled = false), allows you to disable or bypass the particular test-case(s).
  • Reporter class (it generates logs :In TestNG, with the help of the reporter class, users can log messages for the test. Let’s say you are running a test-case, and you want to log related information. This could be just surface or in-depth information, depending on what you want.
  • Expected exceptions :It allows you to trace the exception handling of the code. While writing a code, there may be situations where testers want to verify if an exception is being presented when executed. This method will give you the details of the exceptions that are expected to be presented by that particular method. Use this method with @Test annotation.
  • Dependent On Method :TestNG supports the ‘dependence’ method. We can have dependence as an attriubute of a method, for example, if one method is dependent on another. This is not available in JUnit.
  • TestCase priority :We can execute the test cases in a particular order. To accomplish this, we define the order in @Test Annotation.

Advantages of TestNG are :



  1. It gives the ability to produce HTML Reports of execution
  2. Annotations made testers life easy
  3. Test cases can be Grouped & Prioritized more easily
  4. Parallel testing is possible
  5. Generates Logs
  6. Data Parameterization is possible

JUnit: JUnit is a Regression Testing Framework used by developers to implement unit testing in Java and accelerate programming speed and increase the quality of code.



Similarities Between JUnit and TestNG :
  1. We can create test suite in JUnit and TestNG both frameworks.
  2. Timeout Test Is possible very easily in both the frameworks.
  3. We can ignore specific test case execution of software web application from suite in both the frameworks.
  4. It is possible to create expected exception test for software web application in both the frameworks.
  5. Annotations - Few annotations are similar in both frameworks suite like @Test, @BeforeClass, @AfterClass. JUnit's Annotations @Before and @After are similar to TestNG's @BeforeMethod and @AfterMethod annotations.





Difference Between JUnit and TestNG
  1. In TestNG, Parameterized test configuration is very easy while It is very hard to configure Parameterized test in JUnit.
  2. TestNG support group test but it is not supported in JUnit.
  3. TestNG has a feature to configure dependency test. Dependency test configuration for software web application is not possible in JUnit.
  4. TestNG support @BeforeTest, @AfterTest, @BeforeSuite, @AfterSuite, @BeforeGroups, @AfterGroups which are not supported in JUnit.
  5. Test prioritization, Parallel testing is possible in TestNG. It is not supported by JUnit.


ANNOTATIONS IN TestNG

@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.

Benefits of using Annotations

  1. It identifies the methods it is interested in by looking up annotations. Hence method names are not restricted to any pattern or format.
  2. We can pass additional parameters to annotations.
  3. Annotations are strongly typed, so the compiler will flag any mistakes right away.

Why do we require TestNg in Selenium 
Although selenium is able to do such magic stuffs, there are something which selenium can't/needn't do. Say you need to take a piece of code and name it as a test case. Similarly you need to combine multiple tests into a test suite. At the end of every test run, you need a nice report of what tests pass/fail. You need to integrate this with your build system.  This is where TESTNG comes and is used for!

Steps to installing TestNG for a project

  1. Go to eclipse.
  2. Click on Help.
  3. Select Eclipse Marketplace.
  4. In Find section, search for TestNG.
  5. Install TestNG for Eclipse.
  6. Right-click on your project, navigate to Build Path, and then click on Add Libraries.
  7. Select TestNG and click on next, and then finish.






  • Click on to the "Confirm" button after clicking on to the "Install" button.
  • Accept the "Terms and Conditions" and click on to the "Finish" button.

Can we use TestNG in selenium for Regression Testing?
This is entirely dependent on how you define "integration" and "functional". Selenium/Webdriver is just an API. It allows access to a browser's functions (like click, read, type). You can use just about any programming language to talk to that API.
If you think of it that way, you can utilize it for whatever kind of testing you would like.

Execution Procedure of methods in TestNG:
  1. beforeSuite
  2. beforeTest
  3. beforeClass
  4. beforeMethod
  5. test case
  6. afterMethod
  7. beforeMethod
  8. test case 2
  9. afterMethod
  10. afterClass
  11. afterTest
  12. afterSuite







No comments:

Post a Comment

About Me

My photo
You can reach me out at : jimmiamrit@gmail.com

Total Pageviews