Testing is an important part of software development and holds a key position in the software development life cycle. Testing can be of multiple types such as unit, integration, functional, manual, automation, and so on; it's a huge list. TestNG is one of the most popular testing, or test automation frameworks in Java, which is widely used nowadays. This book will familiarize you with the different features offered by TestNG and how to make best use of them.
In this chapter we'll cover the following topics:
Testing and test automation
Features of TestNG
Downloading TestNG
Installing TestNG onto Eclipse
Writing your first test program
Running your first test program
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. This can be done for different testing types and levels such as unit testing, integration testing, functional testing, and so on, through different means either by coding or by using tools. 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.
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.
TestNG was developed by Cedric Beust. He developed it to overcome a deficiency in JUnit. 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
So, let's get familiarized with TestNG. As I mentioned earlier, TestNG is a testing 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.
Parameterization of test methods is very easy using TestNG and it also provides an easy method of creating data-driven tests.
TestNG exposes its API which makes it easy to add custom functionalities or extensions, if required.
Now that you are at least a little familiarized with TestNG, let's go forward and discover more about the features offered by TestNG. The following are a few of the most important features:
Multiple Before and After annotation options: TestNG provides multiple kinds of Before/After annotations for support of different setup and cleanup options.
XML-based test configuration and test suite definition: Test suites in TestNG are configured mainly using XML files. An XML file can be used to create suites using classes, test methods, and packages, as well as by using TestNG groups. This file is also used to pass parameters to test methods or classes.
Dependent methods: This is one of the major features of TestNG where you can tell TestNG to execute a dependent test method to run after a given test method. You can also configure whether the dependent test method has to be executed or not in case the earlier test method fails.
Groups/group of groups: Using this feature you can assign certain test methods into particular named groups and tell TestNG to include or exclude a particular group in a test.
Dependent groups: Like dependent methods, this feature allows test methods belonging to one group being dependent upon another group.
Parameterization of test methods: This feature helps users to pass parameter values through an XML configuration file to the test methods, which can then be used inside the tests.
Data-driven testing: TestNG allows users to do data-driven testing of test methods using this feature. The same test method gets executed multiple times based on the data.
Multithreaded execution: This allows execution of test cases in a multithreaded environment. This feature can be used for parallel test execution to reduce execution time or to test a multithreaded test scenario.
Better reporting: TestNG internally generates an XML and HTML report by default for its test execution. You can also add custom reports to the framework if required.
Open API: TestNG provides easy extension of API, this helps in adding custom extensions or plugins to the framework depending upon the requirement.
We will discuss these features in more detail in coming chapters.
Before we can download and start using TestNG, there are certain prerequisites we need. So, let's go ahead with the prerequisites first.
Before you start using TestNG please make sure that Java JDK5 or above is installed on your system. Also make sure that JDK is set in the system path. In case JDK is not available on your system, you can download it from the following link:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
TestNG can be downloaded, installed, and run in multiple ways as follows:
Using command line
As an Eclipse plugin
As an IntelliJ IDEA plugin
Using ANT
Using Maven
In case you just want to download the TestNG JAR, you can get it from the following URL:
Before we go forward with installing the TestNG plugin onto Eclipse, please make sure you have Eclipse installed on your system. You can get the latest version of eclipse from http://www.eclipse.org/downloads/. At the time of writing this book, I am using Eclipse JEE Juno-SR1 version.
Let's start with the installation process of TestNG onto Eclipse:
Open your Eclipse application.
Go to Help | Install New Software.
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.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.
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.
Accept the License Information and click on Finish. This will start the download and installation of the TestNG plugin onto Eclipse.
In case you get the following warning window, click on the OK button.
Once the installation is complete, Eclipse will prompt you to restart it. Click on Yes on the window prompt.
Once Eclipse is restarted, verify the TestNG plugin installation by going to Window | Preferences. You will see a TestNG section under the preferences window.
Before we write our first TestNG test, we have to create a Java project in Eclipse to add our TestNG test classes.
A Java project is a place which contains Java source code and related files to compile your program. It can be used to maintain your source code and related files for proper management of the files. Let's create a Java project in Eclipse. If you already know how to create a Java project in Eclipse, you can skip this section.
Perform the following steps to create a Java project:
Open Eclipse.
Go to File | New | Other. A window with multiple options will be shown.
Select Java Project as shown in the following screenshot and click on Next:
On the next screen, enter a Project name for a Java project, let's say
FirstProject
, as shown in the following screenshot, and click on Finish:This will create a new Java project in Eclipse.
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.
Click on the Libraries tab and click on the Add Library... option.
Select TestNG on the Add Library window as shown in the following screenshot and click on Next:
Click on Finish on your next window. This will add the TestNG library to your Eclipse project.
Perform the following steps to create your first TestNG class:
Go to File | New | Other. This will open a new Add wizard window in Eclipse.
Select TestNG class from the Add wizard window and click on Next.
On the next window click on the Browse button and select the Java project where you need to add your class.
Enter the package name and the test class name and click on Finish.
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.
Write the following code to your newly created test class:
package test; import org.testng.annotations.Test; public class FirstTest { @Test public void testMethod() { System.out.println("First TestNG test"); } }
The preceding code contains a class named
FirstTest
, which has a test method namedtestMethod
, denoted by the TestNG annotation@Test
mentioned before thetestMethod()
function. The test method will printFirst TestNG test
when it is executed.
Perform the following steps to run tests through Eclipse:
Select the Java project in the Eclipse and go to Run | Run Configuration.
Select TestNG in the given options and click on the New button to create a new configuration.
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
testng.xml
files, you can select those here for execution.
Let's enter the configuration name as
FirstProject
and select the newly created class under the Class section and click on Apply.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.
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.
Following is the results output on the Eclipse Console window for the test execution:
Following is the results output on the TestNG Results window in Eclipse for the test execution:
Run a particular method of a test class through TestNG using the Run Configuration feature in Eclipse.