TestNG Introduction


In this post, we will be studying – What is TestNG? It is a widely used testing framework. It supports all levels of testing from unit testing to automated functional testing. We will see its usage in Selenium tests and also have a look at its advantages.

What is TestNG?

TestNG is a JUnit-inspired testing framework. It provides several features like data-driven testing, parameterization support, running test cases in parallel, and test case grouping, etc. All these features help in creating robust and powerful testing projects.

We can simply convert a method or function to a test by adding an annotation – @Test on top of it. A simple test in Java looks like this-

@Test
public void sampleTest() {   
   //Any test logic
   System.out.println("Hi! ArtOfTesting here!");   
}

Now, either in case of exception or in case of assertion failure, the test case will be marked as Failed. Else, the test will be marked as Passed.


Features of TestNG

  1. Different Types of Assertions
    It has an Assert class that provides multiple methods supporting different types of assertions. For instance, these assertions help in checking equality of expected and actual result; asserting a condition as True or False; asserting if a value is Null or not null, etc.

  2. Run tests in Parallel
    Using its testNG.xml file, we can run test cases in parallel. This helps in reducing the overall test execution time. The parallel execution can be done in multiple levels – method level, class level, suite level. For details checkĀ running Selenium Tests in Parallel.

  3. Make tests dependent on one another
    We can make the test dependent on other tests. This can be done with the help of the ‘dependsOnMethods’ and ‘dependsOnGroups’ attributes with @Test annotation.

  4. Prioritizing tests
    With this, we can assign a numerical priority value to the Tests. The default priority is 0.

  5. Grouping of tests
    It supports the logical grouping of test cases. Thus, providing the ability to run the test groups in parallel, performing certain operations before or after the execution of the tests belonging to a group.

  6. Data-Driven Testing
    Using @DataProvider, we can create data-driven tests. With this dataProvider annotation, we can pass the test data to the test method.

  7. Reporting
    After test execution, an HTML report gets created providing tabular reporting of test results. The reporting format is also configurable by implementing listeners.

  8. Parameterization
    It provides inherent support for parameterization using its @Parameters annotation. Using @Parameters annotation, we can pass the value of a parameter to the test logic from the testng.xml file.


Advantages of TestNG

  • It helps in reducing overall test execution time by providing the capability to run tests in parallel.

  • It can be easily integrated with CICD tools like Jenkins. This can be done with the help of the testNG.xml file.

  • No need to hardcode the data. This can be done with different annotations like – @DataProvider, @Parameter, etc.

  • We can make one test dependent on another. Thus, saving time by skipping the dependent test in case the parent tests fail.

  • With the grouping of tests, we can create different test suites like – sanity, smoke, regression, etc. Moreover, we can integrate and trigger those test suites with CICD tools like – Jenkins.


TestNG Installation

There is no special installation of TestNG. You can simply download the testNG jar file and add it as a library or include it through maven.

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.10</version>
  <scope>test</scope>
</dependency>

TestNG with Eclipse Setup

In order to integrate TestNG with Eclipse IDE, we can follow the below steps mentioned in – TestNG.org. For this, we can either use Eclipse MarketplaceĀ or the update site.

Via Eclipse Marketplace

Go to theĀ TestNG page on the Eclipse Market PlaceĀ and drag the icon called “Install” onto your workspace.

Install from update site

  1. SelectĀ “Help / Install New Software…”
  2. Enter the update site URL in the “Work with:” field-
    Update site for release – https://dl.bintray.com/testng-team/testng-eclipse-release/.
  3. Check the check box next to the URL.
  4. ClickĀ on ‘Next’.
  5. After that follow the steps mentioned on Eclipse.

Once complete, create a sample test like this.

@Test
public void sampleTest() {
   System.out.println("Hello World!");   
}
What is testng

Now, we can run the test by right-clicking the class and clicking on the “Run as” option. After that click on “TestNG Test”. With this, we can execute the test cases.

If you see, the test results in the form of Passed, Failed, and Skipped. Then, it means we have set up properly.

This completes our tutorial on the topic – “What is TestNG”. For a complete step-by-step tutorial series check – TestNG tutorial. Thank you.


Leave a Comment