TestNG in Selenium

TestNG is a unit test framework designed for testing needs, TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc.

It is inspired from JUnit and NUnit by adding new functionalities, which made TestNG more powerful than other unit test frameworks.

Features of TestNG

  1. Support for annotations
  2. Test that your code is multi-thread safe.
  3. Support for data-driven testing (with @DataProvider).
  4. Support for parameters.
  5. It is supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
  6. Allows user to set execution priorities for the test methods
  7. Helps to generate reports
  8. To run test cases in the order which we provide
  9. To create Dependent Test cases

Drawbacks of Junit

  • JUnit still requires the before/after class methods to be static, which limits what you can do prior to the running of tests; TestNG never has this issue.
  • TestNG @Configuration methods can all take an optional argument to their annotated methods in the form of an ITestResult, XmlTest, Method, or ITestContext. This allows you to pass things around that JUnit wouldn't provide you. JUnit only does this in listeners, and it is limited in use.
  • TestNG comes with some pre-made report generation classes that you can copy and edit and make into your own beautiful test output with very little effort. Just copy the report class into your project and add a listener to run it. Also, ReportNG is available.
  • TestNG has a handful of nice listeners that you can hook onto so you can do additional AOP style magic at certain phases during testing.
  • Some units of code can only be tested in a multiple-JVM scenario. For example, testing the opening of a connection (TCP socket or HTTP connection) and the integrity of the information retrieved from it is not possible (or very hard) to test with only one JVM, which is the only option that exists with JUnit.
  • JUnit was designed to have all tests run within a single process within a single class. It is also designed with the constraint that all tests are atomic and independent of any other code outside the test being run and that the lifecycle for these tests are independent for each test. This is all great for low-level testing units of code, but not for client/server scenario tests.

TestNG vs. Junit

There are advantages that make TestNG superior to JUnit. Some of them are

  • Advance and easy annotations
  • Execution patterns can be set
  • Concurrent execution of test scripts
  • Test case dependencies can be set
  • Parallel Execution

Installing TestNG

Follow the below steps to install TestNG to java project in eclipse.

1. Open Eclipse IDE, then goto help

2. Select install new software

3. Click on Add

4.Specify the name as TestNG

5.Specify the Location as https://brust.com/eclipse

6. Click “Ok᾿

7.Select TestNG checkbox

8.Click Next

9. Accept license

10. Click OK on the warning message

11. Click Yes Which will re-start the Eclipse IDE,

Try..Catch..Finally in Selenium and Java

Associate TestNG to Project

Follow the below steps to associate TestNG to java project in eclipse.

1. Right Click on the Java Project & Goto Properties>Java Build Path



2. Goto Libraries>Add Library



3.Select TestNG>Click Next>Finish




4. After adding TestNG, write code like @Test, then you should get options like below.


Learn about Parameterized Constructor in Selenium and Java

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions