HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TestNG / TestNG Tutorial (With Eclipse)

TestNG Tutorial (With Eclipse)

Testing as you may know is the process of validating and verifying that a piece of software or hardware is working according to the way it’s expected to work. Testing is a very important part of the software development life cycle (SDLC) as it helps in improving the quality of the product developed. There are multiple types and levels of testing, for example, white-box, black-box, unit, integration, system, acceptance, performance, security, functional, non-functional, and so on. Each of these types of testing are done either manually or through automation, using automation tools.

Test automation, as the name suggests, refers to automating the testing process. Test automation gives an advantage of running tests in numerous ways such as at regular intervals or as part of the application build. This helps in identifying bugs at the initial phase of development itself, hence reducing the product timeline and improving the product quality. It also helps in reducing the repetitive manual testing effort and allows manual testing teams to focus on testing new features and complex scenarios.

Table of Contents

Introduction of TestNG
Advantages of TestNG
Installing TestNG onto Eclipse
Creating Java Project with TestNG Dependencies
Creating your first TestNG class
Running TestNG test

Introduction of TestNG

TestNG, where NG stands for “next generation” is a test automation framework inspired by JUnit (in Java) and NUnit (in C#). It can be used for unit, functional, integration, and end-to-end testing. TestNG has gained a lot of popularity within a short time and is one of the most widely used testing frameworks among Java developers. It mainly uses Java annotations to configure and write test methods.

A few of the features that TestNG has over JUnit 4 are:

  • Extra Before and After annotations such as Before/After Suite and Before/After Group
  • Dependency test
  • Grouping of test methods
  • Multithreaded execution
  • In-built reporting framework

It is written in Java and can be used with Java as well as with Java-related languages such as Groovy. In TestNG, suites and tests are configured or described mainly through XML files. By default, the name of the file is testng.xml, but we can give it any other name if we want to. TestNG allows users to do test configuration through XML files and allows them to include (or exclude) respective packages, classes, and methods in their test suite. It also allows users to group test methods into particular named groups and to include or exclude them as part of the test execution.

Advantages of TestNG

Now let’s discover more features/advantages offered by TestNG.

  1. Multiple Before and After annotation options
  2. XML-based test configuration and test suite definition
  3. Dependent methods
  4. Groups/group of groups
  5. Dependent groups
  6. Parameterization of test methods
  7. Data-driven testing
  8. Multithreaded execution
  9. Better reporting

We will discuss these features in more detail in coming tutorials.

Installing TestNG onto Eclipse

Before we can download and start using TestNG, make sure you have Java JDK5 or above is installed on your system. Also make sure that JDK is set in the system path.

In case you just want to download the TestNG JAR, you can get it from the following URL:

TestNG Jar – http://testng.org/testng-6.8.zip

Now, let’s start with the installation process of TestNG onto Eclipse. I will try to capture all steps in the process.

1) Open your Eclipse application.

2) Go to Help | Install New Software.

Eclipse - Go to Help - Install New Software

3) Click on the Add… button next to the Work with text box.

Click on the Add

4) Enter TestNG site into the Name box and enter URL http://beust.com/eclipse into the Location box. Once done, click on the OK button.

Enter TestNG site into the Name box

5) On clicking OK, TestNG update site will get added to Eclipse. The available software window will show the tools available to download under the TestNG site.

Select TestNG and click on Next

6) Select TestNG and click on Next.

7) Eclipse will calculate the software requirements to download the selected TestNG plugin and will show the Install Details screen. Click on Next on the details screen.

8) Accept the License Information and click on Finish. This will start the download and installation of the TestNG plugin onto Eclipse.

download and installation of the TestNG

9)In case you get the following warning window, click on the OK button.

TestNG warning window

10) Once the installation is complete, Eclipse will prompt you to restart it. Click on Yes on the window prompt.

11) Once Eclipse is restarted, verify the TestNG plugin installation by going to Window | Preferences. You will see a TestNG section under the preferences window.

Verify the TestNG plugin installation

We have successfully installed the TestNG plugin into our Eclipse installation. This will help us in executing our TestNG tests or suite using Eclipse.

Creating Java Project with TestNG Dependencies

Before we write our first TestNG test, we have to create a Java project in Eclipse and add our TestNG test dependencies.

1) Go to File | New | Other. A window with multiple options will be shown.

2) Select Java Project as shown in the following screenshot and click on Next.

Select Java Project

3) On the next screen, enter a Project name for a Java project, let’s say TestNGExamples, as shown in the following screenshot, and click on Finish:

Enter a Project name for a Java project

This will create a new Java project in Eclipse.

4)Now go to Project | Properties. Select Java Build Path on the left-hand side on the Properties window as shown in the following screenshot. This will display the build path for the newly created project.

Select Java Build Path

5) Click on the Libraries tab and click on the Add Library… option.
6) Select TestNG on the Add Library window as shown in the following screenshot and click on Next:

Add Library

7) Click on Finish on your next window. This will add the TestNG library to your Eclipse project.

Click on Finish Adding TestNG

Great, We have successfully created a new Java project in Eclipse and added a TestNG library to the build path of the project.

Creating your first TestNG class

Perform the following steps to create your first TestNG class:

1) Go to File | New | Other. This will open a new Add wizard window in Eclipse.

Add wizard window

2) Select TestNG class from the Add wizard window and click on Next.

Add wizard window-2

3) On the next window click on the Browse button and select the Java project where you need to add your class.

select the Java project

4) Enter the package name and the test class name and click on Finish.

Enter the package name

5) This window also gives you an option to select different annotations while creating a new TestNG class. If selected, the plugin will generate dummy methods for these annotations while generating the class. This will add a new TestNG class to your project.

package com.howtodoinjava.test;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class MyFirstTest {
	@Test
	public void f() {
	}

	@BeforeTest
	public void beforeTest() {
	}

	@AfterTest
	public void afterTest() {
	}
}

We have successfully added a new TestNG test class to the newly created Java project in Eclipse. Feel free to modify the code as needed. Now let’s run the newly created test class through Eclipse.

Running TestNG test

Perform the following steps to run tests through Eclipse:

1) Select the Java project in the Eclipse and go to Run | Run Configuration.

Run Configuration for TestNG

2) Select TestNG in the given options and click on the New button to create a new configuration.

Create new TestNG configuration

3) Please notice that TestNG plugin provides multiple options for running your test cases as follows:

  • Class: Using this option you can provide the class name along with the package to run only the said specific test class.
  • Method: Using this you can run only a specific method in a test class.
  • Groups: In case you would like to run specific test methods belonging to a particular TestNG group, you can enter those here for executing them.
  • Package: If you would like to execute all the tests inside a package, you can specify these in this box.
  • Suite: In case you have suite files in the form of testing.xml files, you can select those here for execution.

Let’s enter the configuration name as TestNGRunConfig and select the newly created class under the Class section and click on Apply.

4) Now if you would like to run the newly created configuration, just click on Run after clicking on Apply. This will compile and run the TestNG test class that we have written. The result of the test execution is displayed in the Console and Results windows of Eclipse as shown in the following screenshot.

[TestNG] Running:
  C:\Users\somelocalpath\testng-customsuite.xml

PASSED: f
===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@177b3cd: 23 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@b8deef: 46 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@10ab323: 12 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@5e176f: 13 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@d1e89e: 142 ms

You can also run the test class by selecting it and then right-clicking on it, selecting Run as from the menu, and then choosing TestNG Test.

Run TestNG test class

In this TestNG tutorial, we learned about TestNG, features offered by TestNG, installing the TestNG plugin into Eclipse and writing and executing a TestNG test class through Eclipse.

In coming tutorials, we will learn more advanced features of TestNG.

Happy Learning !!

Reference : http://testng.org/

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. naseema

    November 21, 2018

    getting error “You need to specify at least one testng.xml, one class or one method”

  2. Abu Yusuf

    November 4, 2018

    Very helpful and easy to understand

  3. ali raza

    May 4, 2017

    my code is not working, @Test annnotation code is not working any more, there is no error shown in the class or anything but when i excute the code, beforeMethod annotation is working, while @test is not working, please help me out of this 🙁

  4. John

    July 22, 2016

    Easy understandable steps for beginners, keep it up

  5. Pushpa

    September 15, 2015

    Excellent!
    Thanks a lot for clear steps.

  6. Vipin

    November 19, 2014

    Can we test Spring based web application using this tool? Spring based web application used annotations and it has transactions and hibernate also.

    • Lokesh Gupta

      November 20, 2014

      Sure you can. Give me some time, I will post something on it soon.

  7. Adarsh S

    November 19, 2014

    Thanks for this article !

Comments are closed on this article!

Search Tutorials

TestNG Tutorial

  • TestNG – Introduction
  • TestNG – Hello World
  • TestNG – Maven
  • TestNG – Annotations
  • TestNG – Expected Exception
  • TestNG – Disable/Ignore Tests
  • TestNG – Parallel Tests
  • TestNG – Dependent Tests
  • TestNG – Timeout Tests
  • TestNG – @Parameters
  • TestNG – @DataProvider
  • TestNG – @Factory
  • TestNG – @DataProvider
  • TestNG – Before and After
  • TestNG – Test Groups

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces