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. 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 a 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 testing low level units of code, but not for client/server scenario tests.

TestNG vs Junit

There are advantages which 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 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 http://brust.com/eclipse

6.Click “Ok᾿

7.Select TestNG check box

8.Click Next

9.Accept license

10.Click OK on warning message

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

Try..Catch..Finally in Selenium and Java

Associate TestNG to Project

Follow below steps to associate TestNG to java project in eclipse

1.Right Click on the Java Project & Goto Properties>Java Build Path
associate testng selenium webdriver

java build path testng selenium webdriver

2.Goto Libraries>Add Library testng library selenium webdriver

add library selenium webdriver

3.Select TestNG>Click Next>Finish
add library  testng selenium webdriver

finish library  testng selenium webdriver

ok properties  testng selenium webdriver

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

Learn about Parameterized Constructor in Selenium and Java

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech