Varun Menon NG Beginner's Guide 2013
NG%20Beginner's%20Guide%20-%20First%20Ed
User Manual: Pdf
Open the PDF directly: View PDF
.
Page Count: 276
TestNg Beginner's Guide
Write robust unit and functional tests with the
power of TestNG
Varun Menon
BIRMINGHAM - MUMBAI
TestNg Beginner's Guide
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system,
or transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2013
Production Reference: 1190713
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-600-9
www.packtpub.com
Cover Image by Suresh Mogre (suresh.mogre.99@gmail.com)
Credits
Author
Varun Menon
Reviewers
Project Coordinator
Rahul Dixit
Proofreaders
Yagna Narayana Dande
Lindsey Thomas
Mihai Vilcu
Bernadette Watkins
Acquisition Editor
Usha Iyer
Indexers
Hemangini Bari
Tejal R. Soni
Lead Technical Editor
Anila Vincent
Technical Editors
Pragati Singh
Mausam Kothari
Dipika Gaonkar
Sampreshita Maheshwari
Hardik B. Soni
Rekha Nair
Priya Subramani
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
About the Author
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.
Acknowledgement
First of all I would like to thank my mother and father for supporting me and guiding me on
the correct path throughout my life.
I would like to thank my wife, Sandhya, who has tolerated me and my passion towards work
and has always been supportive. Thanks for all your support.
I would like to thank Pramati Technologies where I have learned most of the things that
I know now. I would like to thank Mr Jay and Vijay Pullur for starting such a wonderful
company and providing such a great environment to learn and work.
I would like to thank my managers, Reddy Raja and Sharad Solanki, without their support,
inspiration, and motivation I may not have been able to reach my current position. A special
thanks to Apurba Nath and Rohit Rai for relying on me and my skills.
I would like to thank all my friends without whom life may not be as fruitful as it is now.
I would also like to thank Cedric Beust, the creator of TestNG unit testing framework, for
coming up with such a good unit test framework, for solving developer and QA engineer's
problems and for being an inspiration of what we can aspire to in QA.
Last but by no means the least I would like to thank Packt Publishing for giving this wonderful
opportunity to write this book and share my knowledge.
About the Reviewers
Yagna Narayana Dande is currently working as a Lead Software Engineer in Testing
at Komli Media, a digital advertising and technology company. She previously worked
as a QA Engineer at MapR Technologies. MapR Technologies focuses on engineering
game-changing, Map/Reduce-related technologies.
She has also worked as a Software Engineer at Symantec, helping consumers and
organizations secure and manage the information-driven world.
Mihai Vilcu has been involved in large-scale testing projects for several years
and has exposure to top technologies for both automated and manual testing
and functional and non-functional testing. "Software testing excellence" is the
motto that drives Mihai's career.
Some of the applications covered by Mihai in his career are CRMs, ERPs, billing
platforms, and rating, collection and business process management applications.
Since software platforms are spread and intensely used in many industries in our
times, Mihai has performed in fields such as telecom, banking, healthcare, software
development, and more.
Readers are welcome to contact Mihai for questions regarding testing as well as requesting
his involvement in your projects. He can be contacted via his e-mail: mvilcu@mvfirst.ro
or directly on his website: www.mvfirst.ro.
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to
your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files
available? You can upgrade to the eBook version at www.PacktPub.com and as a print book
customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@
packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a
range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library.
Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib
today and view nine entirely free books. Simply use your login credentials for immediate access.
Table of Contents
Preface
Chapter 1: Getting Started
Testing and test automation
TestNG
Features of TestNG
Downloading TestNG
Prerequisites
Installing TestNG onto Eclipse
Time for action – installing TestNG onto Eclipse
Writing your first TestNG test
The Java project
Time for action – creating a Java project
Time for action – creating your first TestNG class
Running your first test program
Time for action – running tests through Eclipse
Summary
Chapter 2: Understanding testng.xml
About testng.xml
Creating a test suite
Time for action – creating a test suite
Running testng.xml
Using command prompt
Time for action – running testng.xml through the command prompt
Using Eclipse
Time for action – executing testng.xml using Eclipse
Time for action – configuring Eclipse to run a particular TestNG XML file
Creating multiple tests
Time for action – testng XML with multiple tests
1
5
6
6
7
8
8
8
9
13
13
13
16
18
18
21
23
23
24
24
26
26
26
28
28
29
31
31
Table of Contents
Adding classes, packages, and methods to test
Sample project
Creating a test with classes
Time for action – creating a test with classes
Creating a test using packages
Time for action – creating a test with packages
Creating a test with methods
Time for action – creating a test with methods
Creating a test with packages, classes, and methods
Time for action – creating a test suite with package, class, and test method
Including and excluding
Include/exclude packages
Time for action – test suite to include a particular package
Time for action – test suite to exclude a particular package
Include/exclude methods
Time for action – test suite to exclude a particular method
Using regular expressions to include/exclude
33
34
34
35
36
36
38
38
39
40
42
42
42
43
45
45
46
Time for action – using regular expressions for test
Summary
48
50
Prerequisite – creating a sample project
Chapter 3: Annotations
Annotations in TestNG
Before and After annotations
Time for action – running the Before and After annotations
Time for action – Before and After annotation when extended
Test annotation
Time for action – using test annotation on class
Disabling a test
Time for action – disabling a test method
Exception test
Time for action – writing an exception test
Time for action – writing a exception test verifying message
Time test
Time for action – time test at suite level
Time for action – time test at test method level
Parameterization of test
Parameterization through testng.xml
Time for action – parameterization through testng.xml
Time for action – providing optional values
DataProvider
Time for action – using Test annotation on Class
[ ii ]
46
51
52
53
54
59
62
63
64
65
66
66
68
69
70
71
73
73
73
76
78
79
Table of Contents
Time for action – DataProvider in different class
Summary
Chapter 4: Groups
Grouping tests
Time for action – creating test that belong to a group
Running a TestNG group
Using Eclipse
Time for action – running a TestNG group through Eclipse
Using the testng XML
Time for action – running a TestNG group using the testng XML
Test that belong to multiple groups
Time for action – creating a test having multiple groups
Including and excluding groups
Time for action – including/excluding groups using the testng XML
Using regular expressions
Time for action – using regular expressions in the testng XML
Default group
Time for action – assigning a default group to a set of tests
Group of groups
Time for action – running a TestNG group using the testng XML
Summary
Chapter 5: Dependencies
Dependency test
Test with single test method dependency
Time for action – creating a test that depends on another test
Test that depends on multiple tests
Time for action – creating a test that depends on multiple tests
Inherited dependency test
Time for action – creating a test that depends on inherited tests
Dependent groups
Time for action – creating a test that depends on a group
Depending on methods from different classes
Time for action – depending on a method from a different class
Using regular expressions
Time for action – using regular expressions
XML-based dependency configuration
Simple group dependency
Time for action – using simple dependency in XML
Multigroup dependency
Time for action – defining multigroup dependency in XML
[ iii ]
81
84
85
85
86
87
88
88
89
89
91
91
93
93
95
96
98
98
100
101
103
105
105
105
106
107
108
109
110
112
112
113
114
115
115
117
117
117
119
119
Table of Contents
Using regular expressions for defining dependency
Time for action – using regular expressions for dependency
Summary
121
121
124
Chapter 6: The Factory Annotation
125
Chapter 7: Parallelism
139
Chapter 8: Using Build Tools
155
What is factory?
First factory program
Time for action – first factory test
Passing parameters to test classes
Time for action – passing parameters to test classes
Using DataProvider along with the @Factory annotation
Time for action – using DataProvider with Factory
DataProvider or Factory
Time for action – the DataProvider test
Time for action – the Factory test
Dependency with the @Factory annotation
Time for action – dependency with the @Factory annotation
Time for action – running a dependency test sequentially
Summary
Parallelism
A simple multithreaded test
Time for action – writing first parallel test
Running test methods in parallel
Time for action – running test methods in parallel
Running test classes in parallel
Time for action – running test classes in parallel
Running tests inside a suite in parallel
Time for action – running tests inside a suite in parallel
Configuring an independent test method to run in multiple threads
Time for action – running independent test in threads
Advantages and uses
Summary
Build automation
Advantages of build automation
Different build tools available
Ant
125
125
126
127
128
129
130
131
132
133
135
135
137
138
140
140
140
142
142
144
145
148
148
151
151
153
154
155
156
156
156
Installing Ant
Using Ant
Time for action – using Ant to run TestNG tests
[ iv ]
156
157
157
Table of Contents
Different configurations to be used with TestNG task
Maven
Installing Maven
Using Maven
161
162
162
163
Time for action – using Maven to run TestNG tests
163
Summary
167
Different configurations to be used with Maven
166
Chapter 9: Logging and Reports
169
Chapter 10: Creating a Test Suite through Code
193
Chapter 11: Migrating from JUnit
211
Logging and reporting
Writing your own logger
Time for action – writing a custom logger
Writing your own reporter
Time for action – writing a custom reporter
TestNG HTML and XML report
Time for action – generating TestNG HTML and XML reports
Generating a JUnit HTML report
Time for action – generating a JUnit report
Generating a ReportNG report
Time for action – generating a ReportNG report
ReportNG configuration options
Generating a Reporty-ng (former TestNG-xslt) report
Time for action – generating a Reporty-ng report
Configuration options for Reporty-ng report
Summary
Running TestNG programmatically
Time for action – running TestNG programmatically
Parameterization of tests
Time for action – passing parameter values
Include and exclude
Include/exclude methods
Time for action – including test methods
Include/exclude groups
Time for action – including/excluding groups
Dependency test
Time for action – configuring a dependency test
Summary
Running your JUnit tests through TestNG
Time for action – writing a JUnit test
169
170
170
175
175
177
177
180
180
182
183
186
187
187
190
191
193
194
197
197
200
200
201
204
204
207
207
210
211
212
[v]
Table of Contents
Running your JUnit Tests through TestNG using the testng XML
Time for action – running JUnit tests through TestNG
Running JUnit and TestNG tests together with TestNG XML
Time for action – running JUnit and TestNG tests together
Running JUnit tests along with TestNG through Ant
Time for action – running JUnit and TestNG tests through Ant
Migrating from JUnit to TestNG
Time for action – converting a JUnit test to a TestNG test
Summary
214
214
215
215
217
217
220
221
224
Chapter 12: Unit and Functional Testing
225
Pop Quiz Answers
Index
247
251
Unit testing with TestNG
Time for action – unit testing with TestNG
Assertion with TestNG
Mocking
Different mocking strategies
Mocking with TestNG
Jmock
Time for action – using JMock with TestNG
Mockito
Time for action – using Mockito
Functional testing
TestNG with Selenium
Time for action – using Selenium with TestNG
Summary
[ vi ]
225
226
228
228
229
229
229
230
235
235
239
239
240
245
Preface
Introduction
Currently, TestNG is the most widely used testing framework in the software industry.
It provides a lot of features over the conventional JUnit framework and is used for
different kinds of testing like unit, functional, integration testing, and so on. This book
explains different features of TestNG with examples. You will learn about the basic
features as well as some of the advanced features provided by TestNG.
What this book covers
Chapter 1, Getting Started, explains TestNG and its advantages over other existing
frameworks. It also explains how to install and run your first TestNG test.
Chapter 2, Understanding testng.xml, explains the testng.xml file which is used to configure
the TestNG tests. It also explains different ways to create test suites by adding test packages,
test classes, and test methods to the respective test suite, according to test needs.
Chapter 3, Annotations, explains the various annotations in TestNG and the different features
supported by using them.
Chapter 4, Groups, explains the grouping feature provided by TestNG and how you
can use it to include or exclude a group of tests in test execution.
Chapter 5, Dependencies, explains the dependency feature provided by TestNG. You will
learn how test methods can depend upon another method or a group of methods.
Chapter 6, The Factory Annotation, explains the Factory annotation and how tests can be
created at runtime based on a set of data. You will also learn about the difference between
Factory and DataProvider annotation and how they can be used together.
Preface
Chapter 7, Parallelism, explains a very important feature of TestNG which allows different
configurations for different tests to be run in parallel.
Chapter 8, Using Build Tools, explains build automation and its advantages. It also explains
the different build automation tools available and how TestNG can be used along with them.
Chapter 9, Logging and Reports, explains about the default logging and report options
available with TestNG. It also explains how to extend and write your own logging and
reporting framework above it.
Chapter 10, Creating a Test Suite through Code, explains the different ways to write and
configure TestNG tests through code without the need of an XML configuration file.
Chapter 11, Migrating from JUnit, explains different ways to migrate to JUnit from TestNG
and things that need to be taken care of while migrating.
Chapter 12, Unit and Functional Testing, explains the unit and functional testing usage of
TestNG. It also explains the few mocking techniques to be used for Unit testing and covers
the use of Selenium with TestNG for functional testing.
What you need for this book
Java JDK
Eclipse
Ubuntu/Linux or Windows
Basic knowledge of Java and testing
Who this book is for
This book is for any Java developer who would like to improve their unit tests and would
like to do more with functional, integration, and API testing. This book will also interest QA
guys who are exploring new unit testing frameworks for their functional automation, API, or
integration testing needs.
Conventions
In this book, you will find several headings appearing frequently.
To give clear instructions of how to complete a procedure or task, we use:
[2]
Preface
Time for action – heading
1.
2.
3.
Action 1
Action 2
Action 3
Instructions often need some extra explanation so that they make sense, so they are
followed with:
What just happened?
This heading explains the working of tasks or instructions that you have just completed.
You will also find some other learning aids in the book, including:
Pop quiz – heading
These are short multiple-choice questions intended to help you test your own
understanding.
Have a go hero
These practical challenges give you ideas for experimenting with what you have learned.
You will also find a number of styles of text that distinguish between different kinds of
information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions,
pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The
previous XML defines a TestNG suite using the tag name suite. The name of the suite
is mentioned using the name attribute (in this case First Suite)."
A block of code is set as follows:
[3]
Preface
New terms and important words are shown in bold. Words that you see on the screen,
in menus or dialog boxes, for example, appear in the text like this: "Select the project
and then right-click on it to select New | File."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or may have disliked. Reader feedback is important for us to
develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to feedback@packtpub.com,
and mention the book title via the subject of your message.
[4]
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
Getting Started
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.
[6]
Chapter 1
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.
[7]
Getting Started
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.
[8]
Chapter 1
Time for action – installing TestNG onto Eclipse
Let's start with the installation process of TestNG onto Eclipse:
1.
2.
Open your Eclipse application.
3.
Click on the Add… button next to the Work with text box.
Go to Help | Install New Software.
[9]
Getting Started
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.
[ 10 ]
Chapter 1
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.
[ 11 ]
Getting Started
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.
[ 12 ]
Chapter 1
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.
2.
3.
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:
[ 13 ]
Getting Started
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.
[ 14 ]
Chapter 1
6.
7.
Click on the Libraries tab and click on the Add Library... option.
8.
Click on Finish on your next window. This will add the TestNG library to your
Eclipse project.
Select TestNG on the Add Library window as shown in the following screenshot
and click on Next:
[ 15 ]
Getting Started
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.
2.
Go to File | New | Other. This will open a new Add wizard window in Eclipse.
3.
On the next window click on the Browse button and select the Java project where
you need to add your class.
Select TestNG class from the Add wizard window and click on Next.
[ 16 ]
Chapter 1
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
[ 17 ]
Getting Started
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.
[ 18 ]
Chapter 1
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.
[ 19 ]
Getting Started
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.
[ 20 ]
Chapter 1
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.
[ 21 ]
2
Understanding testng.xml
In the previous chapter we had learned about TestNG, its features, how to
set it up and run it through Eclipse. In this chapter we will learn about testng.
xml, the main configuration file of TestNG used to define suites, tests, and
configure TestNG.
In this chapter we'll cover the following topics:
About testng.xml
Creating a test suite
Running testng.xml
Creating multiple tests in suite
Adding classes, packages, and method to tests
Including and excluding classes, packages, and methods in tests
About testng.xml
testng.xml is a configuration file for TestNG. It is used to define test suites and tests
in TestNG. It is also used to pass parameters to test methods, which we will discuss under
the Parameterization of test section in Chapter 3, Annotations.
testng.xml provides different options to include packages, classes, and independent test
methods in our test suite. It also allows us to configure multiple tests in a single test suite
and run them in a multithreaded environment.
Understanding testng.xml
TestNG allows you to do the following:
Create tests with packages
Create tests using classes
Create tests using test methods
Include/exclude a particular package, class, or test method
Use of regular expression while using the include/exclude feature
Store parameter values for passing to test methods at runtime
Configure multithreaded execution options
In the following sections and chapters we will be discussing more about these features.
Creating a test suite
Let's now create our first TestNG test suite using testng.xml. We will create a simple test
suite with only one test method.
Time for action – creating a test suite
Perform the following steps for creating a test suite:
1.
2.
3.
Go to the Eclipse project that we created in the previous chapter.
Select the project and then right-click on it and select New | File.
Select the project in the File window.
[ 24 ]
Chapter 2
4.
5.
Enter text testng.xml in the File name section, and click on Finish.
Eclipse will add the new file to your project and will open the file in the editor,
as shown in the following screenshot:
Note that the previous screen may look different in your Eclipse
depending upon the plugins that are installed in it.
6.
Add the following snippet to the newly created tesntg.xml file and save it.
The preceding XML defines a TestNG suite using the tag name suite. The name of the suite
is mentioned using the name attribute (in this case First Suite).
It contains a test, declared using the XML tag test and the name of the test is given using
the name attribute. The test contains a class (test.FirstTest) to be considered for test
execution which is configured using the classes and class tags as mentioned in the XML
file. We will discuss these in more detail going forward.
Let's go ahead and learn how to run the previously created testng.xml file.
[ 25 ]
Understanding testng.xml
Running testng.xml
In the earlier section we had created a testng.xml file but haven't yet verified it by running
it. In this section we will learn how to run the testng.xml configuration file. There are
multiple ways of running the testng.xml file as a TestNG suite.
Using command prompt
You can execute the testng.xml file through the command prompt. This also allows the
use of multiple testng.xml files to execute simultaneously through TestNG. Before running
a testng.xml suite through the command prompt, we need to compile our project code.
However, compilation of project code using Java is out of the scope of this book and is not
covered. Hence, we will use the class files compiled by Eclipse. The code compiled by Eclipse
can be found under a folder named bin inside your Test Java project folder.
Downloading the example code
You can download the example code files for all Packt books you have
purchased from your account at http://www.packtpub.com. If you
purchased this book elsewhere, you can visit http://www.packtpub.
com/support and register to have the files e-mailed directly to you.
Time for action – running testng.xml through the command
prompt
Perform the following steps for running testng.xml through the command prompt:
1.
2.
3.
Open the command prompt on your system.
Go to the Test Java project folder where the new testng.xml is created.
Type the following line.
java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG testng.xml
In the preceding command we are adding the TestNG JAR and the project compiled
code to the Java classpath by using the -cp option of Java.
Here /opt/testng-6.8.jar is the path to the testng JAR where you had
downloaded it and it may be different for your system. Also, bin is the folder
containing the compiled code of the Java project. This can be found under the
Eclipse project, which is under consideration. We will talk about compiling code
and running tests in later chapters of this book.
Here org.testng.TestNG consists of the main method that Java will use to
execute the testng.xml file, which is passed as an argument at the command line.
[ 26 ]
Chapter 2
4.
Run the previous command line by pressing the Enter key. This will execute the
test suite mentioned in the testng.xml file using TestNG. After execution an
HTML report is generated by TestNG in a folder named test-output under
the same directory where you had run the command. The following is the HTML
test report generated by TestNG:
Following is the console output:
What just happened?
We have successfully created our first testng.xml test suite and executed it using the
command prompt. In case you would like to execute multiple testng.xml files, you
can use the previous method by passing the other XML files as added arguments to
the command line. The following is a sample command:
java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG testng.xml testng1.
xml
TestNG will execute all the tests declared under these testng XML files.
[ 27 ]
Understanding testng.xml
TestNG also allows executing a particular test from the testng.xml file. To execute a
particular test from the testng XML file, use the option -testnames at the command
line with comma-separated names of tests that need to be executed. The following is a
sample command:
java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG -testnames "Second
Test" testng.xml
The preceding command will execute a test with the name Second Test from testng.xml
if it exists in the defined suite.
Using Eclipse
As we had already learned about how to run the testng.xml file using the command
prompt, we will now learn how to run it using Eclipse. This is one of the methods which
will help us to verify our testng.xml file while modifying it.
Time for action – executing testng.xml using Eclipse
Perform the following steps for executing testng.xml using Eclipse:
1.
2.
3.
Open Eclipse and go to the project where we have created the testng.xml file.
Select the testng.xml file, right-click on it, and select Run As | TestNG suite.
Eclipse will execute the XML file as TestNG suite and you can see the following
report in Eclipse:
The preceding window may not be shown by default in Eclipse after
execution and you may have to click on the window to see the results.
You can also use the Run Configuration option provided by Eclipse to customize your TestNG
tests in Eclipse. Let's learn how to configure Eclipse to run testng XML files.
[ 28 ]
Chapter 2
Time for action – configuring Eclipse to run a particular TestNG
XML file
Perform the following steps to configure Eclipse to run a particular TestNG XML file:
1.
2.
On the top-bar menu of Eclipse, go to Run | Run Configurations.
3.
4.
On the configuration window give a name My Test to the configuration.
Select TestNG from the set of configurations and click on the New Launch
Configuration icon.
Go to the Project section, click on Browse and select your project on the
project window.
[ 29 ]
Understanding testng.xml
5.
Now go to the Suite section and click on Browse. Select the mytestng.xml
configuration.
6.
Click on Apply, and then click on Run. This will run the selected testng XML
configuration file.
What just happened?
We have successfully learned to configure and execute the testng XML file using
Eclipse. The configuration also provides the option to select multiple testng XML
files and pass extra arguments to configure execution. Arguments can be passed by
going to the Arguments tab and entering them in the Program Argument section.
Let's now learn to create multiple test sections inside a testng XML file.
[ 30 ]
Chapter 2
Creating multiple tests
Earlier we had created a simple testng.xml file with a single test in a suite. TestNG allows you
to define multiple test sections in a single suite. This helps you in segregating your tests and
creating different tests based on modules, features, type of test (integration or unit), and so on.
Time for action – testng XML with multiple tests
Let's create a testng XML file with multiple tests and run it:
1.
Open Eclipse and create a new project with the name MultiTest and with the
following structure:
2.
Open the FirstTestClass.java file and add the following code snippet onto it:
package test.firstpackage;
import org.testng.annotations.Test;
public class FirstTestClass {
@Test
public void firstTest(){
System.out.println("First test method");
}
}
The preceding class contains one test method, which is annotated by the @Test
annotation. We will be discussing this annotation in more detail in our next chapter.
The test method prints a message onto the console upon execution.
[ 31 ]
Understanding testng.xml
3.
Open the SecondTestClass.java file and add the following code snippet to it:
package test.firstpackage;
import org.testng.annotations.Test;
public class SecondTestClass {
@Test
public void secondTest(){
System.out.println("Second test method");
}
}
4.
Now open the testng.xml file and add the following snippet to it:
The XML file defines a suite with the name Suite. The suite contains two tests
with names FirstTest and SecondTest respectively. These tests are configured
to execute separate classes test.firstpackage.FirstTestClass and test.
firstpackage.SecondTestClass.
When the XML file is executed as a suite in TestNG, each class is executed by a
separate test section of a suite.
[ 32 ]
Chapter 2
5.
Now run the testng.xml file using Eclipse. Once executed you will see the
following output generated in Eclipse:
What just happened?
We have successfully created a testng XML configuration file with multiple test
sections and then ran it using TestNG. You can run these tests individually by the
different -testnames configuration supported by TestNG as explained in the
Time for action – running testng.xml through the command prompt section.
Adding classes, packages, and methods to test
Earlier we learned about creating test suites, suites with multiple tests, and running them
through TestNG. Now we will learn how to create and configure suites to execute only the
tests belonging to a particular class or a package or just a particular test method.
In earlier examples you may have noticed tests with a single class. In this section we will
learn how to add multiple classes to a test.
[ 33 ]
Understanding testng.xml
Sample project
Before going ahead with creation of test suite with classes, packages, and test methods,
we will need a sample project in place for defining test suites for test execution. Let's
create a sample project first:
1. Open Eclipse and create a new project with three packages, each package
containing two classes, as mentioned in the following screenshot. Also, add
TestNG library to the project as mentioned in Chapter 1, Getting Started.
2. Add the following two test methods to each class with the following code:
@Test
public void firstTest(){
System.out.println("First test method");
}
@Test
public void secondTest(){
System.out.println("Second test method");
}
These methods print messages First test method and Second test method
to the console when executed.
3. Save the project.
Now the project is created for writing our test suites.
Creating a test with classes
In this section we will learn how to create and configure TestNG test suite using classes.
We will use the sample project created earlier and use it to write an example.
[ 34 ]
Chapter 2
Time for action – creating a test with classes
Perform the following steps to create a test with classes:
1.
2.
Open the sample project that we created earlier.
Add new file TestNG configuration XML by name class-testng.xml to the project
with following content.
The preceding testng XML suite defines a test with three classes (one from each
package). To add a class to your test suite just use a combination of classes and
class tag as shown. Use the class tag with the attribute name having a value of
the class name along with the package name (for example, test.firstpackage.
FirstTestClass) to add a test class to your test.
3.
Now run the preceding testng XML file as a TestNG suite through Eclipse.
You will see the following results in Eclipse:
[ 35 ]
Understanding testng.xml
As you can see in the previous screenshot, TestNG executes all the test methods
present inside the test class added to the test suite, and excludes all the other test
classes present in the project.
What just happened?
We have successfully created and executed a TestNG test suite by adding few test classes
to the suite. We can use multiple class tags as and when required under the classes tag
section to add multiple test classes to the tests. Now let's go ahead and create a test with
only packages.
Creating a test using packages
In this section we will learn how to create and configure TestNG test suite using project
packages. A package may contain one or many classes in it. Using this configuration we
can include all the classes under a package or its subpackages to the test suite.
We will use the sample project created earlier and use it to write an example.
Time for action – creating a test with packages
Perform the following steps to create a test with packages:
1.
2.
Let's use the same sample project created earlier.
Add new file TestNG configuration XML by name package-testng.xml to the
project with following content:
The preceding testng XML suite defines a test with two packages (test.
firstpackage and test.secondpackage, respectively) as you can see. To add a
package to your test suite just use a combination of the packages and package tag
as shown in the previous code. Use the package tag with the attribute name having
a value of the package name (for example, test.firstpackage) under the tag
packages to add packages to your tests.
[ 36 ]
Chapter 2
3.
Now run the previous testng XML file as a TestNG suite. You will see the following
results in Eclipse:
As you can see in the previous screenshot TestNG executes all the test classes that are using
TestNG annotations in them under the added packages of the test suite and excludes all the
other test classes present in other packages of the project.
What just happened?
We have successfully created and executed a TestNG test suite by adding test packages
to the suite. TestNG executes all the test methods inside the test classes present in the
packages. In case you want to add all the subpackages under a particular package, you
can use .* at the end of the package name (as shown in the following code snippet).
[ 37 ]
Understanding testng.xml
This will execute all the subpackages present under the package test.
Now let's go ahead and create a test by configuring it to execute only a particular test method.
Creating a test with methods
In this section we will learn how to create and configure the TestNG test suite by adding
specific test methods from test classes. Using this configuration, we can add specific test
methods from the test classes to the test suite for including them as part of the test execution.
We will use the sample project created earlier and use it to write an example.
Time for action – creating a test with methods
Perform the following steps to create a test with methods:
1.
2.
We will use the same sample project created earlier for defining a test suite.
Add a new file TestNG configuration XML by name method-testng.xml to the
project with following content:
The preceding testng XML suite defines a class that needs to be considered for
test execution and the test method that needs to be included for execution. To
add methods to your test suite we have to use the tags methods and include/
exclude under them to add or remove particular methods from a test class.
[ 38 ]
Chapter 2
3.
Now run the previous testng XML file as a TestNG suite. You will see the following
results in Eclipse:
What just happened?
We have successfully created a TestNG suite, considering only a particular method from a
test class, and executed it. In case you would like to add more methods, you can use multiple
include tags mentioning the name of the method that you want to include in the test
execution. Let's go ahead and create a test suite with all the combinations: package, class,
and test method in a single test.
Creating a test with packages, classes, and methods
In this section we will learn how to create and configure the TestNG test suite by including
packages, classes, and test methods.
We will use the sample project created earlier and use it to write an example.
[ 39 ]
Understanding testng.xml
Time for action – creating a test suite with package, class, and
test method
Perform the following steps to create a test suite with package, class, and test method:
1.
2.
We will use the same sample project created earlier for defining a test suite.
Add a new file TestNG configuration XML by name combine-testng.xml to
the project with following content:
The preceding testng XML suite defines a test with a package (test.
firstpackage), a particular class (test.secondpackage.FirstTestClass),
and a particular test method (firstTest under the class test.thirdpackage.
FirstTestClass) as part of the test suite.
[ 40 ]
Chapter 2
3.
Now run the previous testng XML file as a TestNG suite. You will see the following
results in Eclipse:
What just happened?
We have successfully created a TestNG suite by adding a particular package, class, and test
method to the test, and then executed it. This gives us the flexibility of creating a test with
different packages, classes, and test methods depending upon the test requirement.
[ 41 ]
Understanding testng.xml
Including and excluding
TestNG provides the flexibility to include or exclude tests while defining a test suite. This
helps in defining a test suite with a particular set of tests. While defining the testng XML
configuration file, we can use the include and exclude tags to include or exclude tests.
Let's create a few test suites to include and exclude particular tests and execute them.
Include/exclude packages
You can use the TestNG feature of including and excluding to include and exclude certain
test packages from a set of tests. Let's create a few test suites by including and excluding
test package in a test.
Time for action – test suite to include a particular package
Perform the following steps to create a test suite and include a particular package:
1.
2.
Let's take the sample project created earlier for our tests.
Create a testng XML file with name include-package-testng.xml in the
project. Add the following code to it:
The preceding test suite defines a test, which includes all subpackages of the test
package (defined by using regular expression test.*) and includes only a particular
package from all the packages for test execution. This is done by using the include
tag with the name attribute value as the package name that needs to be included
(that is, test.secondpackage). This informs TestNG to include classes belonging
to the included package for test execution.
[ 42 ]
Chapter 2
3.
Execute the previous XML file as a TestNG suite and check the results.
The following report will be shown on the Eclipse report window:
As you can see the results, TestNG has executed test methods from all the classes present
under the included package test.secondpackage and skipped the other test methods.
Time for action – test suite to exclude a particular package
Perform the following steps to create a test suite and exclude a particular package:
1.
2.
Let's take the sample project created earlier for our tests.
Create a testng XML file with name exclude-package-testng.xml
in the project. Add the following code to it:
[ 43 ]
Understanding testng.xml
The preceding test suite defines a test by including all subpackages of the test
package (defined by using the regular expression test.*) and excluding only a
particular package from all the packages for test execution. This is done by using
the exclude tag with the name attribute value as the package name (that is, test.
secondpackage) that needs to be excluded. This informs TestNG to exclude classes
belonging to the package from test execution.
3.
Execute the previous XML file as a TestNG suite and check the results. The following
report will be shown on the Eclipse report window:
As you can see from the results, TestNG has executed test methods from all the classes
present under all the packages under the test package excluding those that belong to
test.secondpackage.
What just happened?
We have successfully created test suites by including and excluding packages from the
test execution. This helps us in creating tests by including or excluding particular packages.
[ 44 ]
Chapter 2
Include/exclude methods
The include/exclude feature can also be used for including and excluding test
methods. It even supports pattern matching options to include/exclude methods
using regular expressions.
Let's first create a simple test suite by including and excluding some test methods,
and then we will create a test suite by using regular expressions for including and excluding.
Including a test method for test suite is the same as creating a test suite with test methods.
This was already covered earlier so I will go forward and tell you how to exclude a particular
test method from a test suite.
Time for action – test suite to exclude a particular method
Perform the following steps to create a test suite and exclude a particular method:
1.
2.
We will use the same project created earlier.
Add a new file TestNG configuration XML named exclude-method-testng.xml
to the project with the following content:
The preceding testng XML suite defines a class that needs to be considered for test
execution and the test method that needs to be excluded from execution. To exclude
a method from your test suite we have to use the tags methods and exclude
under them to exclude a particular method from a test class.
[ 45 ]
Understanding testng.xml
3.
Now run the previous testng XML file as a TestNG suite. You will see the following
results in Eclipse:
As you can see in the test report, TestNG excluded the said test method from test
execution and executed the rest of the test methods from the respective test class.
Using regular expressions to include/exclude
The include and exclude features of TestNG support the use of regular expressions
for including and excluding particular test methods based on certain search names.
Let's go ahead and create a test suite using regular expressions.
Prerequisite – creating a sample project
Before we go forward with writing a test suite using a regular expression we need a
sample project. So, let's first create a sample project for our test:
1. Create a new Java project in Eclipse with the following structure.
[ 46 ]
Chapter 2
2. Add the following code to the RegularExpClass file under the package test.
regularexppackage:
package test.regularexppackage;
import org.testng.annotations.Test;
public class RegularExpClass {
@Test
public void includeTestFirst(){
System.out.println("First include test method");
}
@Test
public void includeTestSecond(){
System.out.println("Second include test method");
}
@Test
public void excludeTestFirst(){
System.out.println("First exclude test method");
}
@Test
public void excludeTestSecond(){
System.out.println("Second exclude test method");
}
@Test
public void includeMethod(){
System.out.println("Include method");
}
@Test
public void excludeMethod(){
System.out.println("Exclude method");
}
}
The preceding code contains multiple test methods with different names, and we will use
these methods to learn about usage of regular expressions in the include and exclude
tags in TestNG.
[ 47 ]
Understanding testng.xml
Time for action – using regular expressions for test
Perform the following steps for using regular expressions for test:
1.
2.
We will use the sample project created earlier for regular expressions.
Add a new file TestNG configuration XML named regexp-testng.xml to the
project with the following content:
The preceding testng XML suite is configured to consider only those test methods
from a particular class whose name contains the word Test in it. The regular
expression is considered by use of .* before and after the text.
3.
Now run the previous testng XML file as a TestNG suite. You will see the following
results in Eclipse:
[ 48 ]
Chapter 2
As you can see in the test report TestNG has executed only those test methods whose
name contains the word Test in it. You can use the regular expression (.*) at the beginning
or end of a text to perform an ends-with and starts-with search, respectively. This regular
expression can also be used with the exclude tag to exclude particular test methods from
test execution.
What just happened?
We have successfully created a TestNG suite to include and exclude particular test methods
from a class. Also we have learned about how to use regular expressions and use a namebased search to include and exclude a test method in a test based on the test method name.
Have a go hero
Having gone through the chapter, feel free to attempt the following:
Write a testng XML configuration file to exclude all the methods that start with a
particular text
Create a testng XML configuration file to include all the subpackages in a package
Pop quiz – TestNG XML
Q1. Can we define a multiple test inside a test suite inside a testng XML?
1. Yes
2. No
Q2. Which of the following options should be used to execute a particular test from a
testng XML test suite containing a multiple tests section in it?
1. -test
2. -testnames
3. -testopts
Q3. Which of the following regular expressions should be used in TestNG for a regular search?
1. * (Star)
2. .* (Dot Star)
3. x ( X)
[ 49 ]
Understanding testng.xml
Summary
In this chapter, we learned about testng XML configuration files of TestNG to
define different tests using classes, packages, and test methods. We also learned
about how to include/exclude packages and test methods from a particular test.
We looked at how to use regular expressions to add particular packages and test
methods to the methods based on matching names.
In the next chapter we will learn about different annotations provided by TestNG
and how to use them.
[ 50 ]
3
Annotations
In the previous chapter we had learned about the TestNG XML configuration
file, the different features it provides, and the different ways to create a TestNG
test suite.
In this chapter we will learn about TestNG annotations and the different
features supported through them. These are the base of TestNG and most
of the features are supported through their use of TestNG.
In this chapter we'll cover the following topics:
Annotations in TestNG
Before and After annotations
Test annotation
Disabling a test
Exception test
Time test
Parameterization of test
Passing parameters to the test methods
Using DataProvider for parameterized tests
Annotations
Annotations in TestNG
Annotation is a feature introduced in Java 5 and is used to add metadata (data about data)
to Java source code. This will allow you to add information to an existing data object in your
source code. It can be applied for classes, methods, variables, and parameters. Annotations
may affect the way different programs or tools use your source code. There are certain
predefined set of annotations defined in Java. For example, @Override, @Deprecated,
@SupressWarnings, and so on, but Java allows users to define their own annotations too.
TestNg makes use of the same feature provided by Java to define its own annotations and
build an execution framework by using it. The following is a table containing information
about all the annotations provided by TestNG and a brief description of them:
Annotation
Description
@BeforeSuite or
@AfterSuite
The annotated method will be executed before and after
any tests declared inside a TestNG suite.
@BeforeTest or
@AfterTest
The annotated methods will be executed before and after
each test section declared inside a TestNG suite.
@BeforeGroups or
@AfterGroups
These annotations are associated with the groups feature
in TestNG.
BeforeGroups annotated method will run before any
of the test method of the specified group is executed.
AfterGroups annotated method will run after any of
the test method of the specified group gets executed.
For this method to be executed, the user has to mention
the list of groups this method belongs to using groups
attribute with the said annotation. You can specify more
than multiple groups if required.
@BeforeClass or
@AfterClass
BeforeClass annotated method is executed before
any of the test method of a test class.
AfterClass annotated method is executed after the
execution of every test methods of a test class are executed.
@BeforeMethod or
@AfterMethod
@DataProvider
These annotated methods are executed before/after the
execution of each test method.
Marks a method as a data providing method for a test
method. The said method has to return an Object
double array (Object[ ][ ]) as data.
[ 52 ]
Chapter 3
Annotation
@Factory
Description
Marks a annotated method as a factory that returns an
array of class objects (Object[ ]). These class objects
will then be used as test classes by TestNG. This is used to
run a set of test cases with different values.
@Listeners
Applied on a test class. Defines an array of test listeners
classes extending org.testng.ITestNGListener.
Helps in tracking the execution status and logging purpose.
@Parameters
This annotation is used to pass parameters to a test
method. These parameter values are provided using the
testng.xml configuration file at runtime.
@Test
Marks a class or a method as a test method. If used at class
level, all the public methods of a class will be considered as
a test method.
Before and After annotations
Before and After annotations are mainly used to execute a certain set of code before
and after the execution of test methods. These are used to basically set up some variables
or configuration before the start of a test execution and then to cleanup any of these things
after the test execution ends.
TestNG provides five different kinds of Before and After annotation options, each of which
can be used depending upon the test requirements. The following are the different before
and after options provided by TestNG:
@BeforeSuite/@AfterSuite
@BeforeTest/@AfterTest
@BeforeGroups/@AfterGroups
@BeforeClass/@AfterClass
@BeforeMethod/@AfterMethod
Let's try out an example containing all the preceding annotated methods and learn about
how and when they are executed.
[ 53 ]
Annotations
Time for action – running the Before and After annotations
1.
Perform the following steps to run the Before and After annotations: Open Eclipse
and create a Java Project with following structure. Please make sure that TestNG
library is added to the build path of the project as mentioned in Chapter 1,
Getting Started.
2.
Add the following code to the TestClass.java file shown in the
previous screenshot:
package test.beforeafter;
import
import
import
import
import
import
import
import
import
import
import
org.testng.annotations.AfterClass;
org.testng.annotations.AfterGroups;
org.testng.annotations.AfterMethod;
org.testng.annotations.AfterSuite;
org.testng.annotations.AfterTest;
org.testng.annotations.BeforeClass;
org.testng.annotations.BeforeGroups;
org.testng.annotations.BeforeMethod;
org.testng.annotations.BeforeSuite;
org.testng.annotations.BeforeTest;
org.testng.annotations.Test;
public class TestClass {
/**
* Before suite method which is executed before
* starting of any of the test in the suite.
*/
@BeforeSuite
public void beforeSuite(){
System.out.println("Before Suite method");
}
/**
[ 54 ]
Chapter 3
* After suite method which gets executed after
* execution of all the tests in a suite.
*/
@AfterSuite
public void afterSuite(){
System.out.println("After Suite method");
}
/**
* Before Test method which gets executed before the first
* test-method mentioned in each test inside the 'test'
* tag in test suite.
/
@BeforeTest
public void beforeTest(){
System.out.println("Before Test method");
}
/**
* After Test method which gets executed after
* the last test-method
*/
@AfterTest
public void afterTest(){
System.out.println("After Test method");
}
/**
* Before Class method which gets executed before
* any of the test-methods inside a class.
*/
@BeforeClass
public void beforeClass(){
System.out.println("Before Class method");
}
/**
* After Class method which gets executed after
* all of the test-methods inside a class gets executed.
*/
@AfterClass
[ 55 ]
Annotations
public void afterClass(){
System.out.println("After Class method");
}
/**
* Before group method gets executed before executing any of
* the tests belonging to the group as mentioned in the 'groups'
* attribute.
* The following method gets executed before execution of the
* test-method belonging to group "testOne".
*/
@BeforeGroups(groups={"testOne"})
public void beforeGroupOne(){
System.out.println("Before Group Test One method");
}
/**
* After group method gets executed after executing all the
* tests belonging to the group as mentioned in the 'groups'
* attribute.
* The following method gets executed after execution of the
* test-methods belonging to group "testOne".
*/
@AfterGroups(groups={"testOne"})
public void afterGroupOne(){
System.out.println("After Group Test One method");
}
/**
* Before group method gets executed before executing any of the
tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the
* test-method belonging to group "testTwo".
*/
@BeforeGroups(groups={"testTwo"})
public void beforeGroupTwo(){
System.out.println("Before Group Test two method");
}
/**
* After group method gets executed after executing all the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the
* test-methods belonging to group "testTwo".
*/
@AfterGroups(groups={"testTwo"})
public void afterGroupTwo(){
[ 56 ]
Chapter 3
System.out.println("After Group Test two method");
}
/**
* Before method which gets executed before each test-method.
*/
@BeforeMethod
public void beforeMethod(){
System.out.println("Before Method");
}
/**
* After method which gets executed after each test-method.
*/
@AfterMethod
public void afterMethod(){
System.out.println("After Method");
}
/**
* Test-method which belongs to group "testOne".
*/
@Test(groups={"testOne"})
public void testOneMethod(){
System.out.println("Test one method");
}
/**
* Test-method which belongs to group "testTwo".
*/
@Test(groups={"testTwo"})
public void testTwoMethod(){
System.out.println("Test two method");
}
}
As you can see there are multiple Before and After annotations defined in the
preceding test class. Preceding each one, you will also see a small detail about
each of the annotated methods along with the details of when they are executed.
3.
Create a new testng.xml file to the project and add the following code onto it:
[ 57 ]
Annotations
The preceding testng.xml file contains two tests containing the same test
class but with different test methods.
4.
Execute the preceding testng.xml file as a TestNG suite. You will be shown
the following results in the Console window:
You can see the sequence in which the Before and After methods are executed.
The BeforeGroups and AfterGroups of the respective test method group are
called before and after the respective group test method is executed.
[ 58 ]
Chapter 3
What just happened?
We have successfully created a test class with all kinds of Before and After annotations and
executed it using a testng.xml. We had learned from the previous example the sequence
in which each of the respective before and after test methods are executed.
The current example only contains Before and After annotations that are present in the same
class. Lets learn the execution flow when a class containing a Before and After annotation is
extended by another class having another set of Before and After annotations.
Time for action – Before and After annotation when extended
1.
Open eclipse and create a Java project with a package structure as mentioned
below. Make sure that you have added TestNG library to the build path.
2.
Add the following code to the BaseClass.java file:
package test.beforeafter;
import
import
import
import
org.testng.annotations.AfterClass;
org.testng.annotations.AfterMethod;
org.testng.annotations.BeforeClass;
org.testng.annotations.BeforeMethod;
public class BaseClass {
@BeforeClass
public void beforeBaseClass(){
System.out.println("Parent Before Class method");
}
@AfterClass
public void afterBaseClass(){
System.out.println("Parent After Class method");
}
@BeforeMethod
[ 59 ]
Annotations
public void beforeBaseMethod(){
System.out.println("Parent Before method");
}
@AfterMethod
public void afterBaseMethod(){
System.out.println("Parent After method");
}
}
The preceding class contains BeforeClass, AfterClass, BeforeMethod,
and AfterMethod annotated methods. Each of these methods prints a text
to the console when executed.
3.
Add the following code to the TestClass.java file:
package test.beforeafter;
import
import
import
import
import
org.testng.annotations.AfterClass;
org.testng.annotations.AfterMethod;
org.testng.annotations.BeforeClass;
org.testng.annotations.BeforeMethod;
org.testng.annotations.Test;
public class TestClass extends BaseClass{
@BeforeClass
public void beforeChildClass(){
System.out.println("Child Before Class method");
}
@AfterClass
public void afterChildClass(){
System.out.println("Child After Class method");
}
@BeforeMethod
public void beforeChildMethod(){
System.out.println("Child Before method");
}
@AfterMethod
public void afterChildMethod(){
System.out.println("Child After method");
}
[ 60 ]
Chapter 3
@Test
public void testMethod(){
System.out.println("Test method under TestClass");
}
}
The preceding test class extends the BaseClass file created earlier and also
contains certain methods having BeforeClass/AfterClass and BeforeMethod/
AfterMethod annotations. It also contains the test method denoted by the Test
annotation. All the methods print a sample text to the console when executed.
4.
Add a testng.xml file to the project and add the following code to it:
The preceding testng.xml file defines a single test inside a suite with only one
class TestClass considered for test.
5.
Execute the previously created testng.xml file as a TestNG suite. You will see
the following output in the Eclipse's Console window:
[ 61 ]
Annotations
What just happened?
We have successfully seen an example of a test class which contains a Before/After
annotation. We also executed a test class, where the base class that the test class extends,
also contains similar Before/After annotated methods. As you can see, the report output
of TestNG executes the parent class before annotated methods and then the child before
annotated methods. After annotated methods, the child class method is executed and
then the parent class.
This helps us to have a common before annotated methods across all test classes and have
specific Before/After annotated methods for each test class where ever required.
Test annotation
One of the basic annotations of TestNG is the Test annotation. This annotation marks a
method or a class as part of the TestNG test. If applied at class level this annotation will mark
all the public methods present inside the class as test methods for TestNG test. It supports
lot of attributes which you can use along with the annotation, which will enable you to use
the different features provided by TestNG. The following is a list of attributes supported by
the Test annotation:
Supported attributes
alwaysRun
Description
dataProvider
The name of the data provider, which will provide data for
data-driven testing to this method.
dataProviderClass
The class where TestNG should look for the data-provider method
mentioned in the dataProvider attribute. By default its the
current class or its base classes.
dependsOnGroups
Specifies the list of groups this method depends on.
Takes a true or false value. If set to true this method will always
run even if its depending method fails.
dependsOnMethods
Specifies the list of methods this method depends on.
description
The description of this method.
enabled
Sets whether the said method or the methods inside the said class
should be enabled for execution or not. By default its value is true.
expectedExceptions
This attribute is used for exception testing. This attribute specifies
the list of exceptions this method is expected to throw. In case a
different exception is thrown.
groups
List of groups the said method or class belongs to.
timeOut
This attribute is used for a time out test and specifies the time
(in millisecs) this method should take to execute.
[ 62 ]
Chapter 3
We will learn about these attributes and how to use them in future sections in this chapter
or in future chapters. As we have already seen sample tests using the Test annotation on
methods, we will skip it and learn on how we can use the Test annotation on a class.
Time for action – using test annotation on class
1.
Open Eclipse and create a sample Java project as shown in the following screenshot
with TestNG library added to its build path:
2.
Add a new test class with the name TestClass under the test package and add
the following code to it:
package test;
import org.testng.annotations.Test;
@Test
public class TestClass {
public void testMethodOne(){
System.out.println("Test method one.");
}
public void testMethodTwo(){
System.out.println("Test method two.");
}
private void testMethodThree(){
System.out.println("Test method three.");
}
}
The preceding class contains three methods out of which two are public
methods and one is a private method. The class has been annotated with
the Test annotation.
[ 63 ]
Annotations
3.
Select the preceding test class in Eclipse and run it as TestNG test. You will see
the following results in the TestNG Results window in Eclipse:
What just happened?
We have successfully run a class which is being annotated by a Test annotation of TestNG.
As you can see from the results, only two methods out of the three methods of the class
were executed by TestNG. If a class is annotated by the Test annotation, TestNG will
consider only the methods with public access modifiers as test methods. All the methods
with other access modifiers will be neglected by TestNG.
Disabling a test
There may be some scenarios where you may have to disable a particular test or a set of
tests from getting executed. For example, consider a scenario where a serious bug exists
in a feature due to certain tests belonging to certain scenarios that cannot be executed.
As the issue has already been identified we may need to disable the said test scenarios
from being executed.
Disabling a test can be achieved in TestNG by setting the enable attribute of the Test
annotation to false. This will disable the said test method from being executed as part
of the test suite. If this attribute is set for the Test annotation at class level, all the public
methods inside the class will be disabled.
Lets go ahead and create a sample project to see how this feature works.
[ 64 ]
Chapter 3
Time for action – disabling a test method
1.
Create a new class inside the package test with name the DisableTestClass
inside the same Java project created earlier.
2.
Add the following code to the newly created class:
package test;
import org.testng.annotations.Test;
public class DisableTestClass {
@Test(enabled=true)
public void testMethodOne(){
System.out.println("Test method one.");
}
@Test(enabled=false)
public void testMethodTwo(){
System.out.println("Test method two.");
}
@Test
public void testMethodThree(){
System.out.println("Test method three.");
}
}
The preceding class contains three test methods out of which, two contain the
attribute enabled with the values true and false respectively.
3.
Select and run the previous class as TestNG test in Eclipse. You will see following
results in the Results window of TestNG in Eclipse:
[ 65 ]
Annotations
What just happened?
We have successfully created test methods with a Test annotation and used the attribute
enabled along with it. As you can see in the previous results, only two methods were
executed by TestNG. The method with attribute enabled value as false was ignored from
test execution. By default the attribute value of enabled is true, hence you can see the test
method with name testMethodThree was executed by TestNG even when the attribute
value was not specified.
Exception test
While writing unit tests there can be certain scenarios where we need to verify that an
exception is being thrown by the program during execution. TestNG provides a feature to
test such scenarios by allowing the user to specify the type of exceptions that are expected
to be thrown by a test method during execution. It supports multiple values being provided
for verification. If the exception thrown by the test is not part of the user entered list, the
test method will be marked as failed.
Let's create a sample test and learn how exception test works in TestNG.
Time for action – writing an exception test
1.
Create a new Java project with the following structure in Eclipse:
2.
Create a new class with name ExceptionTest and add the following code to it:
package test.exception;
import java.io.IOException;
import org.testng.annotations.Test;
public class ExceptionTest {
@Test(expectedExceptions={IOException.class})
public void exceptionTestOne() throws Exception{
throw new IOException();
}
[ 66 ]
Chapter 3
@Test(expectedExceptions={IOException.class,
NullPointerException.class})
public void exceptionTestTwo() throws Exception{
throw new Exception();
}
}
The preceding class contains two test methods, each throwing one particular
kind of exception, exceptionTestOne throws IOException where as
exceptionTestTwo throws Exception. The expected exception to validate
while running these tests is mentioned using the expectedExceptions
attribute value while using the Test annotation.
3.
Select and run the preceding class as TestNG test in Eclipse. You will see following
results in the Results window of TestNG in Eclipse:
What just happened?
We have successfully created an exception test and ran it. As you can see from the test
results, exceptionTestTwo was marked as failed by TestNG during execution. The test
failed because the exception thrown by the said method does not match the exception
list provided in the expectedExceptions list. The value to this list takes the expected
exceptions to be passed as class as shown in the code.
TestNG also supports multiple expected exceptions to be provided for verification while
executing a particular test, this is shown in the preceding class for exceptionTestTwo test
method. You can also verify a test based on the exception message that was thrown by the
test. Let's learn how to write a exception test based on the exception message thrown.
[ 67 ]
Annotations
Time for action – writing a exception test verifying message
1.
Create a new class with the name ExceptionMessageTest inside the Java project
created in the earlier section.
2.
Add the following code to it:
package test.exception;
import java.io.IOException;
import org.testng.annotations.Test;
public class ExceptionMessageTest {
/**
* Verifies the exception message based on the exact error
message thrown.
*/
@Test(expectedExceptions={IOException.class},
expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestOne() throws Exception{
throw new IOException("Pass Message test");
}
/**
* Verifies the exception message using the regular exception.
* This test verifies that the exception message contains a
text "Message" in it.
*/
@Test(expectedExceptions={IOException.class},
expectedExceptionsMessageRegExp=".* Message .*")
public void exceptionMsgTestTwo() throws Exception{
throw new IOException("Pass Message test");
}
/**
* Verifies the exception message based on the exact error
message thrown.
* This is to show that TestNG fails a test when the exception
message does not match.
*/
@Test(expectedExceptions={IOException.class},
expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestThree() throws Exception{
throw new IOException("Fail Message test");
}
}
[ 68 ]
Chapter 3
The preceding class contains three test methods each throwing the same
exception but with different error messages. Verification for each test is done
based on the exception error message thrown by them using the attribute
expectedExceptionsMessageRegExp while using the Test annotation.
3.
Select and run the preceding class as TestNG test in Eclipse. You will see following
results in the Results window of TestNG in Eclipse:
What just happened?
We successfully created a sample program to verify a test based on the exception message
thrown. We executed and verified the previous test based on the exception message thrown
by each of them. The attribute expectedExceptionsMessageRegExp can only be used
with the use of expectedExceptions attribute. Regular expression can also be used to
verify the error message, this can be done using .*. Depending upon the position of the
regular expression we can use it to do pattern matching such as starts-with, contains, and
ends-with while verifying the exception message.
Time test
While running tests there can be cases where certain tests get stuck or may take much more
time than expected. In such a case you may need to mark the said test case as fail and then
continue. TestNG allows user to configure a time period to wait for a test to completely
execute. This can be configured in two ways:
At suite level: This will be applicable for all the tests in the said TestNG test suite
At each test method level: This will be applicable for the said test method and will
override the time period if configured at the suite level
Let's go ahead and create a sample project to see how this feature works.
[ 69 ]
Annotations
Time for action – time test at suite level
1.
Open Eclipse and create a sample Java project with the structure shown in the
following screenshot:
2.
Add a sample test class with name TimeSuite and add the following code to it:
package test.timetest;
import org.testng.annotations.Test;
public class TimeSuite {
@Test
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}
@Test
public void timeTestTwo() throws InterruptedException{
Thread.sleep(400);
System.out.println("Time test method two");
}
}
The preceding test class contains two test methods which print a message onto to
the console on successful execution. Both also contain the Thread.sleep method
which pause the test execution depending upon the argument passed for the time
specified in milliseconds.
3.
Add a testng.xml file to the project and put the following code to it:
[ 70 ]
Chapter 3
The preceding testng.xml contains a suite with a single test considering a test
class for test execution. You'll notice that the suite tag contains a new attribute
named time-out which is set with a value 500. This attribute applies a time-out
period for test methods for the whole suite. That means if any test method in
the said suite takes more than the specified time period (in this case 500
milliseconds) to complete execution it will be marked as failed.
4.
Run the preceding testng.xml file as TestNG suite in Eclipse. You will see the
following test results in the Results window of TestNG in Eclipse:
What just happened?
As you can see for the test result, TestNG executed the said tests and failed the first test
as the test took more time to execute than the time mentioned in the time-out section. This
feature is useful while doing time testing and to recover from lock conditions in multithreaded
execution. Let's now go ahead and learn to set the timeout at a test method level.
Time for action – time test at test method level
1.
Add a new test class to the project created in the earlier section under the
timetest package with the name TimeMethod:
2.
Add the following code to it:
package test.timetest;
import org.testng.annotations.Test;
[ 71 ]
Annotations
public class TimeMethod {
@Test(timeOut=500)
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}
@Test
public void timeTestTwo() throws InterruptedException{
Thread.sleep(400);
System.out.println("Time test method two");
}
}
The preceding test class contains two test methods which print a message onto to
the console on successful execution. Both also contain Thread.sleep method
which pauses the test execution depending upon the argument passed for the time
specified in milliseconds. A time-out value 500 at test level is specified for test
method timeTestOne using the attribute timeOut while using Test annotation
as shown in the preceding code.
3.
Select the respective test class and execute it as TestNG test using Eclipse. You will
see the following test results in the TestNG Results window in Eclipse:
[ 72 ]
Chapter 3
What just happened?
As you can see from the test result, TestNG executed the said tests and failed the first test.
The test failed because the test took more time to execute than the time mentioned in the
timeOut attribute of the Test annotation. This helps in specifying a predefined execution
time limit for a specific method. The timeout value mentioned at a test method level always
takes precedence over the the time-out specified at test suite level.
Parameterization of test
One of the important features of TestNG is parameterization. This feature allows user to pass
parameter values to test methods as arguments. This is supported by using the Parameters
and DataProvider annotations. There are mainly two ways through which we can provide
parameter values to test-methods:
Through testng XML configuration file
Through DataProviders
Parameterization through testng.xml
If you need to pass some simple values such as String types to the test methods at
runtime, you can use this approach of sending parameter values through TestNG XML
configuration files. You have to use the Parameters annotation for passing parameter
values to the test method.
Let's write a simple example of passing parameters to test methods through the XML
configuration file.
Time for action – parameterization through testng.xml
1.
Open Eclipse and create simple Java project with the following package structure.
Make sure that you have added TestNG library to the project build path.
[ 73 ]
Annotations
2.
Add a new Java class file with the name ParameterTest and copy the following
code to it:
package test.parameter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterTest {
/**
* Following method takes one parameter as input. Value of the
* said parameter is defined at suite level.
*/
@Parameters({ "suite-param" })
@Test
public void prameterTestOne(String param) {
System.out.println("Test one suite param is: " + param);
}
/**
* Following method takes one parameter as input. Value of the
* said parameter is defined at test level.
*/
@Parameters({ "test-two-param" })
@Test
public void prameterTestTwo(String param) {
System.out.println("Test two param is: " + param);
}
/**
* Following method takes two parameters as input. Value of the
* test parameter is defined at test level. The suite level
* parameter is overridden at the test level.
*/
@Parameters({ "suite-param", "test-three-param" })
@Test
public void prameterTestThree(String param,
String paramTwo) {
System.out.println("Test three suite param is: " + param);
System.out.println("Test three param is: " + paramTwo);
}
}
[ 74 ]
Chapter 3
The preceding class contains three test methods, each of them require a different
set of parameter values. The Parameters annotation is mentioned for each of the
tests with the name of the parameter that needs to be passed to the test method
at the time of the test execution. The value of these parameters needs to be
mentioned in the testng XML file that will be defined for suite definition.
3.
Create a testng. xml configuration file with name param-testng.xml
and copy the following code to it:
[ 75 ]
Annotations
The preceding XML file contains three tests in it, each explains a different way of
passing the parameters to the test methods. The parameter is declared in testng
XML file using the parameter tag. The name attribute of the tag defines name of
the parameter whereas the value attribute defines the value of the said parameter.
The tag can be used at suite level as well as at test level, as you can see from the
preceding XML file.
4.
Run the preceding testng.xml as TestNG test suite. You will see the following
test results on the Console window:
What just happened?
We created a test class with multiple methods that accepts parameters from TestNG. The
parameter values are set at both suite and test level in the testng XML file. Any parameter
value defined at the test level will override the value of a parameter, with same name, if
defined at suite level. You can see this in test three for test method prameterTestThree.
TestNG also provides an option to provide optional values for a parameter, this value will be
used if parameter value is not found in the defined file.
Time for action – providing optional values
Perform the following steps to provide optional values:
1.
Create a new class file with the name OptionalTest inside the Java project
created earlier.
2.
Copy the following code to the said Java file and save it.
package test.parameter;
import org.testng.annotations.Optional;
[ 76 ]
Chapter 3
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class OptionalTest {
@Parameters({"optional-value"})
@Test
public void optionTest(@Optional("optional value")
String value){
System.out.println("This is: "+value);
}
}
The preceding class file contains a single test method that takes one parameter
as input. The said test method on execution prints the parameter value that is
passed onto the console using the System.out.println method. The Parameter
value is passed to the test method using the parameter named optional-value
from the XML file. An optional value for the said parameter is defined using the
Optional annotation against the said parameter.
3.
Create a new testng XML file with name optional-testng.xml and copy the
following code to it:
The preceding XML file has two tests defined in it. No parameter is defined in the
first test where as the second test declares a parameter named optional-value
in it. Both contain the same test class for test execution.
[ 77 ]
Annotations
4.
Select the above testng XML file and run it as a TestNG suite. You will see the
following results in the Console window of Eclipse:
What just happened?
We have successfully created a test using Optional annotation of TestNG. As you can see
from the previous test results, TestNG has passed the optional value to the test method during
first test execution. This happened because TestNG was unable to find a parameter named
optional-value in the XML file from the first test. During the second test it found the
parameter value in the XML and passed the said value to the test method during execution.
The parameter annotation can be used for any of the Before/After, Factory, and Test
annotated methods. It can be used to initialize variables and use them in a class, test,
or may be for the whole test execution.
DataProvider
One of the important features provided by TestNG is the DataProvider feature. It helps
the user to write data-driven tests, that means same test method can be run multiple
times with different datasets. DataProvider is the second way of passing parameters
to test methods. It helps in providing complex parameters to the test methods as it is
not possible to do this from XML.
To use the DataProvider feature in your tests you have to declare a method annotated by
DataProvider and then use the said method in the test method using the dataProvider
attribute in the Test annotation.
Lets write a simple example and learn how to use the DataProvider feature in our tests.
[ 78 ]
Chapter 3
Time for action – using Test annotation on Class
1.
Open Eclipse and create a Java project with the structure shown in the
following screenshot:
2.
Create a new Java class with the name SameClassDataProvider and copy
the following code to it:
package test.dataprovider;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class SameClassDataProvider {
@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
return new Object[][] { { "data one" }, { "data two" } };
}
@Test(dataProvider = "data-provider")
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
}
The preceding test class contains a test method which takes one argument as
input and prints it to console when executed. A DataProvider method is also
available in the same class by using the DataProvider annotation of TestNG.
The name of the said DataProvider method is mentioned using the name attribute
of the DataProvider annotation. The DataProvider returns a double Object class
array with two sets of data, data one and data two.
The DataProvider to provide parameter values to a test method is defined by
giving the name of the data provider using the dataProvider attribute while
using the Test annotation.
[ 79 ]
Annotations
3.
Add a new testng xml file to said project with the name simple-testng.xml
and add the following code to it:
The preceding testng XML file defines a simple test suite with the said test
class created earlier.
4.
Select the testng xml file in eclipse and run it as a TestNG suite. You will see
following test result in the Console window:
What just happened?
As you can see from the above test result the respective test method in the class was
executed two times. The execution of the test method is dependent upon the number
of datsets passed by the DataProvider method, in this case as two different sets of
data were returned by the DataProvider, the test method was executed two times.
It is mandatory for a DataProvider method to return the data in the form of double
array of Object class (Object [][]). The first array represents a data set where as the
second array contains the parameter values.
In the current example the DataProvider method was written in the same class. TestNG
by default looks for the DataProvider method in the same class or in any of the base
classes. But if you want to put your DataProvider method in another class, you can do
so by making it a static method and providing the name of the class containing it to TestNG.
Lets take a look at this and learn how it works.
[ 80 ]
Chapter 3
Time for action – DataProvider in different class
1.
Open Eclipse and add two new classes with the names DataProviderClass
and TestClass to the Java project created earlier.
2.
Add the following code to TestClass:
package test.dataprovider;
import org.testng.annotations.Test;
public class TestClass {
@Test(dataProvider = "data-provider",dataProviderClass=DataProvi
derClass.class)
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
}
The preceding test class contains a test method which takes one argument
as input and prints it onto the console when executed. The DataProvider to
provide parameter values to a test method is defined by giving the name of the
DataProvider using the DataProvider attribute while using Test annotation.
As the DataProvider method is in a different class, the class name to refer for getting
the DataProvider is provided to TestNG using the dataProviderClass attribute as
seen in the preceding code.
3.
Add the following code to DataProviderClass:
package test.dataprovider;
import org.testng.annotations.DataProvider;
public class DataProviderClass {
@DataProvider(name="data-provider")
public static Object[][] dataProviderMethod(){
return new Object[][] { { "data one" }, { "data two" } };
}
}
The preceding class only contains the DataProvider method to provide data to a test
method. The method returns two sets of data when called.
[ 81 ]
Annotations
4.
Add a new testng XML file to said project with the name different-classtestng.xml and add the following code to it:
The preceding testng XML file defines a simple test suite with a single test class.
5.
Select the testng XML file in Eclipse and run it as a TestNG suite. You will see the
following test result in the Console window:
What just happened?
As you can see from the above test results the test method was executed two times
depending upon the data passed to it by DataProvider method. In this scenario the
DataProvider method was in a different class. In such a case the DataProvider has to be
declared static so that it can be used by a test method in a different class for providing data.
Have a go hero
Having gone through the chapter, feel free to attempt the following:
Write an Exception test to verify that the exception message thrown starts
with a specific text
Write a test method which accepts two parameters out of which one of
them is optional
[ 82 ]
Chapter 3
Pop quiz – annotations
Q1. How many different type of Before and After annotations are provided by TestNG?
1. 3
2. 4
3. 5
Q2. Using which attribute with the Test annotation you can disable a test method?
1. disableTest
2. enableTest
3. enabled
4. disabled
Q3. We can provide multiple exceptions while verifying a exception in a test.
1. True
2. False
Q4. The time for performing time test is provided in?
1. seconds
2. milliseconds
3. minutes
4. hours
Q5. Which annotation has to be used to provide a parameter to a test method?
1. Parameterization
2. Parameter
3. Parameters
Q6. What kind of return value does a DataProvider method have to return in TestNG?
1. Object
2. Object[]
3. Object[][]
4. List