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.
- Multiple Before and After annotation options
- XML-based test configuration and test suite definition
- Dependent methods
- Groups/group of groups
- Dependent groups
- Parameterization of test methods
- Data-driven testing
- Multithreaded execution
- 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:
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.
3) Click on the Add… button next to the Work with text box.
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.
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.
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.
9)In case you get the following warning window, click on the OK button.
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.
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.
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:
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.
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:
7) Click on Finish on your next window. This will add the TestNG library to your Eclipse project.
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.
2) Select TestNG class from the Add wizard window and click on Next.
3) On the next window click on the Browse button and select the Java project where you need to add your class.
4) Enter the package name and the test class name and click on Finish.
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.
2) Select TestNG in the given options and click on the New button to create a new 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.
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/
naseema
getting error “You need to specify at least one testng.xml, one class or one method”
Abu Yusuf
Very helpful and easy to understand
ali raza
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 🙁
John
Easy understandable steps for beginners, keep it up
Pushpa
Excellent!
Thanks a lot for clear steps.
Vipin
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
Sure you can. Give me some time, I will post something on it soon.
Adarsh S
Thanks for this article !