Home Application-development TestNG Beginner's Guide

TestNG Beginner's Guide

By Varun Menon
books-svg-icon Book
Subscription
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
Subscription
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Getting Started
About this book

Unit/Functional testing has now become part of every development life cycle. Junit was once the main framework that was used by developers for the purpose of Unit testing when it came to Java. But Junit had certain limitations in terms of execution and features. This book explains about the features and usage of TestNg, a new framework that overcomes Junit’s limitations and provides a lot of extra features.

TestNg Beginner’s Guide is a practical, hands-on guide that aims to increase your knowledge of TestNg. This step-by-step guide will help you to learn and understand the different TestNg features, and you will learn about the advantages and how to use and configure each feature according to your needs.

This book explains the various features of the TestNG testing framework. It provides a step-by-step guide that explains the different features with practical examples and sample programs.You will also learn about how to use and configure each feature based on different test scenarios. We will also take a look at extending TestNG to add custom logging and test-reports. If you are a beginner in TestNG or test frameworks, then this book will help you in learning, practising, and getting started with TestNg.

Publication date:
July 2013
Publisher
Packt
Pages
276
ISBN
9781782166009

 

Chapter 1. Getting Started

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 and test automation


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


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.

 

Features of TestNG


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.

 

Downloading TestNG


Before we can download and start using TestNG, there are certain prerequisites we need. So, let's go ahead with the prerequisites first.

Prerequisites

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:

http://testng.org/testng-6.8.zip

 

Installing TestNG onto Eclipse


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.

 

Time for action – installing TestNG onto Eclipse


Let's start with the installation process of TestNG onto Eclipse:

  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.

What just happened?

We have successfully installed the TestNG plugin into our Eclipse installation. This will help us in executing our TestNG tests or suite using Eclipse. Now we can go ahead and write our first TestNG test.

 

Writing your first TestNG test


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

The Java project

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.

 

Time for action – creating a Java project


Perform the following steps to create a Java project:

  1. Open Eclipse.

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

  3. Select Java Project as shown in the following screenshot and click on Next:

  4. 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.

  5. 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.

  6. Click on the Libraries tab and click on the Add Library... option.

  7. Select TestNG on the Add Library window as shown in the following screenshot and click on Next:

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

What just happened?

We have successfully created a new Java project in Eclipse and added a TestNG library to the build path of the project. Now we can go ahead and add new test classes for adding TestNG tests. Now let's create our first TestNG test class for this newly created Java project.

 

Time for action – 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.

    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.

  5. 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 named testMethod, denoted by the TestNG annotation @Test mentioned before the testMethod() function. The test method will print First TestNG test when it is executed.

What just happened?

We have successfully added a new TestNG test class to the newly created Java project in Eclipse. Now let's run the newly created test class through Eclipse.

 

Running your first test program


Now we will learn about how to run the newly added test class through Eclipse as well as about different options available for running your tests.

 

Time for action – running tests through Eclipse


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. 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.

  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.

    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:

Have a go hero

Run a particular method of a test class through TestNG using the Run Configuration feature in Eclipse.

Pop quiz – about TestNG

Q1. TestNG is a unit testing framework.

  1. True

  2. False

Q2. Suites in TestNG are configured using:

  1. The XML file

  2. The HTML file

  3. The CSV file

 

Summary


In this chapter 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 the next chapter, we will learn about testng.xml and how to define test suites using XML.

About the Author
  • Varun Menon

    Varun Menon is a QA consultant with several years of experience in developing automation frameworks on various technologies and languages such as Java, JavaScript, Ruby, and Groovy. He has worked on web and mobile applications for some of the leading clients in the field of supply chain management, online photo books, video analytics, and market research. He blogs at http://blog.varunin.com and is active on Stack Overflow, Selenium, and robotium groups. He is also the author of an upcoming open source android automation tool Bot-bot, which has record and re-play features like Selenium. He currently holds the position of QA Architect at Pramati Technologies Private Limited, Hyderabad, India.

    Browse publications by this author
TestNG Beginner's Guide
Unlock this book and the full library FREE for 7 days
Start now