Testng Tutorial
Testng Tutorial
TESTNGTUTORIAL
tutorialspoint.com
i
TestNG Tutorial
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that
make it more powerful and easier to use.
TestNG is designed to cover all categories of tests: Unit, functional, end-to-end, integration, etc., and it
requires JDK 5 or higher.
This tutorial will give you great understanding on TestNG framework needed to test an enterprise level
application to deliver it with robustness and reliability.
Audience
This tutorial is designed for Software Professionals, who are willing to learn TestNG Framework in simple
and easy steps. This tutorial will give you great understanding on TestNG Framework concepts, and after
completing this tutorial, you will be at intermediate level of expertise from where you can take yourself to
higher level of expertise.
Prerequisites
Before proceeding with this tutorial, you should have a basic understanding of Java programming
language, text editor and execution of programs, etc., because you are going to use TestNG to handle all
levels of Java project testing (Unit, functional, end-to-end, integration, etc.,), so it will be good if you
have knowledge of software development and software testing processes.
the content and graphics on this tutorial are the property of tutorialspoint.com. Any content from
tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form
without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws.
This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the
accuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com site
or this tutorial content contains some errors, please contact us at [email protected]
TUTORIALS POINT
Simply Easy Learning
Table of Content
TestNG Tutorial ........................................................................ 2
Audience .................................................................................. 2
Prerequisites ............................................................................ 2
Copyright & Disclaimer Notice .................................................. 2
TestNG - Overview ................................................................... 5
What is TestNG? ....................................................................................... 5
TestNG Features ....................................................................................... 6
TestNG - Environment.............................................................. 7
System Requirement ................................................................................. 7
Step 1 - verify Java installation in your machine ........................................ 7
Step 2: Set JAVA environment .................................................................. 8
Step 3: Download TestNG archive ............................................................ 8
Step 4: Set TestNG environment ............................................................... 9
Step 5: Set CLASSPATH variable ............................................................. 9
Step 6: Test TestNG Setup........................................................................ 9
Step 7: Verify the Result .......................................................................... 10
CHAPTER
TestNG - Overview
esting is the process of checking the functionality of the application whether it is working as per
requirements and to ensure that at developer level, unit testing comes into picture. Unit testing is the testing of
single entity (class or method). Unit testing is very essential to every software company to give a quality product to
their customers.
JUnit has driven developers to understand the usefulness of tests, especially of unit tests when compared to any
other testing framework. Leveraging a rather simple, pragmatic, and strict architecture, JUnit has been able to
"infect" great number of developers. Features of JUnit can be seen in Junit Features.
Some of the short comings of JUnit are:
Initially designed to enable unit testing only, now used for all kinds of testing.
Intrusive (forces you to extend classes and name your methods a certain way).
The management of different suites of tests in complex projects can be very tricky..
What is TestNG?
Definition of TestNG as per its documentation is:
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it
more powerful and easier to use.
TestNG is an open source automated testing framework; where NG of TestNG means Next Generation. TestNG is
similar to JUnit (especially JUnit 4), but its not a JUnit extension. Its inspired by JUnit. It is designed to be better
than JUnit, especially when testing integrated classes. The creator of TestNG isCedric Beust.
TestNG eliminates most of the limitations of the older framework and gives the developer the ability to write more
flexible and powerful tests. As it heavily borrows from Java Annotations (introduced with JDK 5.0) to define tests, it
can also show you how to use this new feature of the Java language in a real production environment.
TUTORIALS POINT
Simply Easy Learning
TestNG Features
Annotations.
Supports testing integrated classes (e.g., by default, no need to create a new test class instance for every
test method).
Introduces test groups. Once you have compiled your tests, you can just ask TestNG to run all the "frontend" tests, or "fast", "slow", "database", etc...
Supports Dependent test methods, parallel testing, load testing, partial failure.
TUTORIALS POINT
Simply Easy Learning
CHAPTER
TestNG - Environment
estNG is a framework for Java, so the very first requirement is to have JDK installed in your machine.
System Requirement
JDK
1.5 or above.
Memory
no minimum requirement.
Disk Space
no minimum requirement.
Operating System
no minimum requirement.
Task
Command
Windows
Linux
$ java -version
Mac
Open Terminal
Output
Windows
Linux
TUTORIALS POINT
Simply Easy Learning
Mac
If you do not have Java installed, install the Java Software Development Kit (SDK)
fromhttp://www.oracle.com/technetwork/java/javase/downloads/index.html. We are assuming Java 1.7.0_25 as
installed version for this tutorial.
Output
Windows
Linux
export JAVA_HOME=/usr/local/java-current
Mac
export JAVA_HOME=/Library/Java/Home
Output
Windows
Append the string; C:\Program Files\Java\jdk1.7.0_25\bin to the end of the system variable,
Path.
Linux
export PATH=$PATH:$JAVA_HOME/bin/
Mac
not required
Archive name
Windows
testng-6.8.jar
Linux
testng-6.8.jar
Mac
testng-6.8.jar
TUTORIALS POINT
Simply Easy Learning
Output
Windows
Linux
export TESTNG_HOME=/usr/local/TESTNG
Mac
export TESTNG_HOME=/Library/TESTNG
Output
Windows
Linux
export CLASSPATH=$CLASSPATH:$TESTNG_HOME/testng-6.8.jar:
Mac
export CLASSPATH=$CLASSPATH:$TESTNG_HOME/testng-6.8.jar:
import org.testng.annotations.Test;
importstatic org.testng.Assert.assertEquals;
publicclassTestNGSimpleTest{
@Test
publicvoid testAdd(){
String str ="TestNG is working fine";
assertEquals("TestNG is working fine", str);
}
}
TestNG can be invoked in several different ways:
With ant
Let us invoke using the testng.xml file. Create an xml file with name testng.xml in C:\ > TestNG_WORKSPACE to
execute Test case(s)
TUTORIALS POINT
Simply Easy Learning
TUTORIALS POINT
Simply Easy Learning
CHAPTER
Write the business logic of your test and insert TestNG annotations in your code..
Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a testng.xml file
or in build.xml..
Run TestNG.
Here, we will see one complete example of TestNG testing using POJO class, Business logic class and a test xml
which will be run by TestNG.
Create EmployeeDetails.java in C:\ > TestNG_WORKSPACE which is a POJO class.
publicclassEmployeeDetails{
privateString name;
privatedouble monthlySalary;
privateint age;
/**
* @return the name
*/
publicString getName(){
return name;
}
/**
* @param name the name to set
*/
publicvoid setName(String name){
this.name = name;
}
/**
* @return the monthlySalary
*/
publicdouble getMonthlySalary(){
return monthlySalary;
}
/**
TUTORIALS POINT
Simply Easy Learning
Now, let's create a TestNG class called TestEmployeeDetails.java in C:\ > TestNG_WORKSPACE.A TestNG
class is a Java class that contains at least one TestNG annotation. This class contains test cases to be tested. A
TUTORIALS POINT
Simply Easy Learning
TestNG test can be configured by @BeforeXXX and @AfterXXX annotations (we will see this in the chapter TestNG
- Execution Procedure) which allows to perform some Java logic before and after a certain point.
import org.testng.Assert;
import org.testng.annotations.Test;
publicclassTestEmployeeDetails{
EmpBusinessLogic empBusinessLogic =newEmpBusinessLogic();
EmployeeDetails employee =newEmployeeDetails();
@Test
publicvoid testCalculateAppriasal(){
employee.setName("Rajeev");
employee.setAge(25);
employee.setMonthlySalary(8000);
double appraisal = empBusinessLogic
.calculateAppraisal(employee);
Assert.assertEquals(500, appraisal,0.0,"500");
}
// test to check yearly salary
@Test
publicvoid testCalculateYearlySalary(){
employee.setName("Rajeev");
employee.setAge(25);
employee.setMonthlySalary(8000);
double salary = empBusinessLogic
.calculateYearlySalary(employee);
Assert.assertEquals(96000, salary,0.0,"8000");
}
}
TestEmployeeDetails class is used for testing the methods of EmpBusinessLogic class. It
Before you can run the tests, however, you must configure TestNG using a special XML file, conventionally named
testng.xml. The syntax for this file is very simple, and its contents as below. Create this file in C:\ >
TestNG_WORKSPACE:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<classes>
<classname="TestEmployeeDetails"/>
</classes>
</test>
</suite>
Details of the above file are as below:
A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
Tag <test> represents one test and can contain one or more TestNG classes.
TUTORIALS POINT
Simply Easy Learning
<class> tag represents a TestNG class is a Java class that contains at least one TestNG annotation. It can
contain one or more test methods.
TUTORIALS POINT
Simply Easy Learning
CHAPTER
he traditional way to indicate test methods in JUnit 3 is by prefixing their name with test. This is a very
effective method for tagging certain methods in a class as having a special meaning, but the naming doesnt scale
very well (what if we want to add more tags for different frameworks?) and is rather inflexible (what if we want to
pass additional parameters to the testing framework?).
Annotations were formally added to the Java language in JDK 5 and TestNG made the choice to use annotations to
annotate test classes.
Here is the list of annotations that TestNG supports:
Annotation
Description
@BeforeSuite
The annotated method will be run only once before all tests in this suite have run.
@AfterSuite
The annotated method will be run only once after all tests in this suite have run.
@BeforeClass
The annotated method will be run only once before the first test method in the current class
is invoked.
@AfterClass
The annotated method will be run only once after all the test methods in the current class
have been run.
@BeforeTest
The annotated method will be run before any test method belonging to the classes inside the
<test> tag is run.
@AfterTest
The annotated method will be run after all the test methods belonging to the classes inside
the <test> tag have run.
@BeforeGroups
The list of groups that this configuration method will run before. This method is guaranteed
to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups
The list of groups that this configuration method will run after. This method is guaranteed to
run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeMethod The annotated method will be run before each test method.
@AfterMethod
@DataProvider
Marks a method as supplying data for a test method. The annotated method must return an
Object[ ][ ] where each Object[ ] can be assigned the parameter list of the test method. The
@Test method that wants to receive data from this DataProvider needs to use a
dataProvider name equals to the name of this annotation.
TUTORIALS POINT
Simply Easy Learning
@Factory
Marks a method as a factory that returns objects that will be used by TestNG as Test
classes. The method must return Object[ ].
@Listeners
@Parameters
@Test
TestNG identifies the methods it is interested in by looking up annotations. Hence, method names are not
restricted to any pattern or format.
We can pass additional parameters to annotations.
Annotations are strongly typed, so the compiler will flag any mistakes right away.
Test classes no longer need to extend anything (such as TestCase, for JUnit 3).
TUTORIALS POINT
Simply Easy Learning
CHAPTER
histutorial explains the execution procedure of methods in TestNG which means that which method is
called first and which one after that. Here is the execution procedure of the TestNG test API methods with the
example.
Create a java class file name TestngAnnotation.java in C:\ > TestNG_WORKSPACE to test annotation.
import
import
import
import
import
import
import
import
import
org.testng.annotations.Test;
org.testng.annotations.BeforeMethod;
org.testng.annotations.AfterMethod;
org.testng.annotations.BeforeClass;
org.testng.annotations.AfterClass;
org.testng.annotations.BeforeTest;
org.testng.annotations.AfterTest;
org.testng.annotations.BeforeSuite;
org.testng.annotations.AfterSuite;
publicclassTestngAnnotation{
// test case 1
@Test
publicvoid testCase1(){
System.out.println("in test case 1");
}
// test case 2
@Test
publicvoid testCase2(){
System.out.println("in test case 2");
}
@BeforeMethod
publicvoid beforeMethod(){
System.out.println("in beforeMethod");
}
@AfterMethod
publicvoid afterMethod(){
System.out.println("in afterMethod");
}
@BeforeClass
publicvoid beforeClass(){
TUTORIALS POINT
Simply Easy Learning
System.out.println("in beforeClass");
}
@AfterClass
publicvoid afterClass(){
System.out.println("in afterClass");
}
@BeforeTest
publicvoid beforeTest(){
System.out.println("in beforeTest");
}
@AfterTest
publicvoid afterTest(){
System.out.println("in afterTest");
}
@BeforeSuite
publicvoid beforeSuite(){
System.out.println("in beforeSuite");
}
@AfterSuite
publicvoid afterSuite(){
System.out.println("in afterSuite");
}
}
Next, let's create the file testng.xml in C:\ > TestNG_WORKSPACE to execute annotations.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<classes>
<classname="TestngAnnotation"/>
</classes>
</test>
</suite>
Compile the Test case class using javac.
C:\TestNG_WORKSPACE>javac TestngAnnotation.java
Now, run the testng.xml, which will run test case defined in provided Test Case class.
C:\TestNG_WORKSPACE>java org.testng.TestNG testng.xml
Verify the output.
in
in
in
in
in
in
beforeSuite
beforeTest
beforeClass
beforeMethod
test case 1
afterMethod
TUTORIALS POINT
Simply Easy Learning
in
in
in
in
in
in
beforeMethod
test case 2
afterMethod
afterClass
afterTest
afterSuite
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
See the above output and this is how the TestNG execution procedure is:
Even the methods beforeTest(), beforeClass(), afterClass() and afterTest() methods are executed only once.
beforeMethod() method executes for each test case but before executing the test case.
afterMethod() method executes for each test case but after the execution of test case.
TUTORIALS POINT
Simply Easy Learning
CHAPTER
he test cases are executed using TestNG class. This class is the main entry point for running tests in the
TestNG framework. Users can create their own TestNG object and invoke it in many different ways:
On an existing testng.xml
You can also define which groups to include or exclude, assign parameters, etc. The command line parameters are:
-sourcedir src1;src2: ; separated list of source directories (used only when javadoc annotations are used)
-target
-groups
-testrunfactory
-listener
We will create the TestNG object an existing testng.xml in our example below.
Create a Class
Create a java class to be tested say MessageUtil.java in C:\ > TestNG_WORKSPACE
/*
* This class prints the given message on console.
*/
TUTORIALS POINT
Simply Easy Learning
publicclassMessageUtil{
privateString message;
//Constructor
//@param message to be printed
publicMessageUtil(String message){
this.message = message;
}
// prints the message
publicString printMessage(){
System.out.println(message);
return message;
}
}
Implement the test condition and check the condition using assertEquals API of TestNG.
Create testng.xml
Next, let's create testng.xml file in C:\ > TestNG_WORKSPACE to execute Test case(s). This file captures your
entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which
you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests
or split several runtime configurations (e.g., testng-database.xml would run only tests that exercise your database).
<?xml version="1.0" encoding="UTF-8"?>
<suitename="Sample test Suite">
<testname="Sample test">
<classes>
<classname="SampleTest"/>
</classes>
TUTORIALS POINT
Simply Easy Learning
</test>
</suite>
Compile the Test case using javac.
C:\TestNG_WORKSPACE>javac MessageUtil.java SampleTest.java
Now, run the testng.xml, which will run test case defined in <test> tag.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output.
Hello World
===============================================
Sample test Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
TUTORIALS POINT
Simply Easy Learning
CHAPTER
Test suiteis a collection of test cases that are intended to test a behavior or set of behaviors of software
program. In TestNG, we cannot define a suite in testing source code, but it is represented by one XML file as suite
is the feature of execution. This also allows flexible configuration of the tests to be run. A suite can contain one or
more tests and is defined by the <suite> tag.
<suite> is a root tag of your testng.xml. It describes a test suite, which in turn is made of several <test> sections.
Table below lists all the legal attributes <suite> accepts.
Attribute
Description
name
verbose
parallel
thread-count
annotations
time-out
The default timeout that will be used on all the test methods found in this test.
In this chapter, we will show you an example having two Test1 & Test2 test classes to run together using Test Suite.
Create a Class
Create a java class to be tested say MessageUtil.java in C:\ > JUNIT_WORKSPACE
/*
* This class prints the given message on console.
*/
publicclassMessageUtil{
privateString message;
TUTORIALS POINT
Simply Easy Learning
// Constructor
// @param message to be printed
publicMessageUtil(String message){
this.message = message;
}
// prints the message
publicString printMessage(){
System.out.println(message);
return message;
}
// add "Hi!" to the message
publicString salutationMessage(){
message ="Hi!"+ message;
System.out.println(message);
return message;
}
}
TUTORIALS POINT
Simply Easy Learning
<suitename="Suite1">
<testname="exampletest1">
<classes>
<classname="Test1"/>
</classes>
</test>
<testname="exampletest2">
<classes>
<classname="Test2"/>
</classes>
</test>
</suite>
Suite1 includes exampletest1 and exampletest2.
Compile all java classes using javac.
C:\TestNG_WORKSPACE>javac MessageUtil.java Test1.java Test2.java
Now, run the testng.xml, which will run test case defined in provided Test Case class.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output.
Inside testPrintMessage()
Manisha
Inside testSalutationMessage()
Hi!Manisha
===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================
You can also check the test-output folder; under the Suite1 folder you can see two html's created exampletest1.html
and exampletest2.html which would look like as below:
TUTORIALS POINT
Simply Easy Learning
TUTORIALS POINT
Simply Easy Learning
CHAPTER
ometimes, it happens that our code is not ready and test case written to test that method/code will fail if run.
In such cases, annotation @Test(enabled = false) helps to disable this test case.
A test method is annotated with @Test(enabled = false), then the test case that is not ready to test is bypassed.
Now, let's see @Test(enabled = false) in action.
Create a Class
/*
* This class prints the given message on console.
*/
publicclassMessageUtil{
privateString message;
//Constructor
//@param message to be printed
publicMessageUtil(String message){
this.message = message;
}
// prints the message
publicString printMessage(){
System.out.println(message);
return message;
}
// add "Hi!" to the message
publicString salutationMessage(){
message ="Hi!"+ message;
System.out.println(message);
return message;
}
}
TUTORIALS POINT
Simply Easy Learning
Create testng.xml
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM
"http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<classes>
<classname="IgnoreTest"/>
</classes>
</test>
</suite>
Compile the MessageUtil, Test case classes using javac.
C:\TestNG_WORKSPACE>javac MessageUtil.java IgnoreTest.java
Now, run the testng.xml, which will not run testPrintMessage() test case defined in provided Test Case class.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output. testPrintMessage() test case is not tested.
Inside testSalutationMessage()
TUTORIALS POINT
Simply Easy Learning
Hi!Manisha
===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================
You can also Ignore a group of tests which will be discussed in the next chapter
TUTORIALS POINT
Simply Easy Learning
CHAPTER
he group test is a new innovative feature in TestNG, it doesnt exist in JUnit framework, it permits you
dispatch methods into proper portions and preform sophisticated groupings of test methods. Not only can you
declare those methods that belong to groups, but you can also specify groups that contain other groups. Then,
TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding
another set. This gives you maximum flexibility in how you partition your tests and doesn't require you to recompile
anything if you want to run two different sets of tests back to back.
Groups are specified in your testng.xml file using the <groups> tag. It can be found either under the <test> or
<suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.
Now, let's see an example of how to group test.
Create a Class
/*
* This class prints the given message on console.
*/
publicclassMessageUtil{
privateString message;
// Constructor
// @param message to be printed
publicMessageUtil(String message){
this.message = message;
}
// prints the message
publicString printMessage(){
System.out.println(message);
return message;
}
// add "tutorialspoint" to the message
publicString salutationMessage(){
message ="tutorialspoint"+ message;
System.out.println(message);
return message;
TUTORIALS POINT
Simply Easy Learning
}
// add "www." to the message
publicString exitMessage(){
message ="www."+ message;
System.out.println(message);
return message;
}
}
Check-in tests (checkintest): These tests should be run before you submit new code. They should
typically be fast and just make sure no basic functionality is broken.
Functional tests (functest): These tests should cover all the functionalities of your software and be run at
least once a day, although ideally you would want to run them continuously.
Create the java class file name GroupTestExample.java in C:\ > TestNG_WORKSPACE
import org.testng.Assert;
import org.testng.annotations.Test;
publicclassGroupTestExample{
String message =".com";
MessageUtil messageUtil =newMessageUtil(message);
@Test(groups ={"functest","checkintest"})
publicvoid testPrintMessage(){
System.out.println("Inside testPrintMessage()");
message =".com";
Assert.assertEquals(message, messageUtil.printMessage());
}
@Test(groups ={"checkintest"})
publicvoid testSalutationMessage(){
System.out.println("Inside testSalutationMessage()");
message ="tutorialspoint"+".com";
Assert.assertEquals(message, messageUtil.salutationMessage());
}
@Test(groups ={"functest"})
publicvoid testingExitMessage(){
System.out.println("Inside testExitMessage()");
message ="www."+"tutorialspoint"+".com";
Assert.assertEquals(message, messageUtil.exitMessage());
}
}
TUTORIALS POINT
Simply Easy Learning
Create testng.xml
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s). Here, we would be executing only those
tests, which belong to the group functest.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<groups>
<run>
<includename="functest"/>
</run>
</groups>
<classes>
<classname="GroupTestExample"/>
</classes>
</test>
</suite>
Compile the MessageUtil, Test case classes using javac.
C:\TestNG_WORKSPACE>javac MessageUtil.java GroupTestExample.java
Now, run the testng.xml which will run only the method testPrintMessage() as it belongs to the groupfunctest.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output. Only the method testPrintMessage() is executed.
Inside testPrintMessage()
.com
Inside testExitMessage()
www..com
===============================================
Suite1
Total tests run: 2, Failures: 1, Skips: 0
===============================================
Groups of groups
Groups can also include other groups. These groups are called MetaGroups. For example, you might want to define
a group all that includes checkintest and functest. Let's modify our testng.xml file as below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<groups>
<definename="all">
<includename="functest"/>
<includename="checkintest"/>
</define>
<run>
<includename="all"/>
</run>
</groups>
TUTORIALS POINT
Simply Easy Learning
<classes>
<classname="GroupTestExample"/>
</classes>
</test>
</suite>
Executing the above testng.xml will execute all the three tests and will give you the below result:
Inside testPrintMessage()
.com
Inside testSalutationMessage()
tutorialspoint.com
Inside testExitMessage()
www.tutorialspoint.com
===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Exclusion groups
You can ignore a group by using the <exclude> tag as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<groups>
<definename="all">
<excludename="functest"/>
<includename="checkintest"/>
</define>
<run>
<includename="all"/>
</run>
</groups>
<classes>
<classname="GroupTestExample"/>
</classes>
</test>
</suite>
TUTORIALS POINT
Simply Easy Learning
CHAPTER
10
TestNG Exception Test
estNG provides a option of tracing the Exception handling of code. You can test whether a code throws
desired exception or not. The expectedExceptions parameter is used along with @Test annotation. Now, let's
see @Test(expectedExceptions) in action.
Create a Class
Create a java class to be tested say MessageUtil.java in C:\ > TestNG_WORKSPACE.
Add an error condition inside printMessage() method.
/*
* This class prints the given message on console.
*/
publicclassMessageUtil{
privateString message;
//Constructor
//@param message to be printed
publicMessageUtil(String message){
this.message = message;
}
// prints the message
publicvoid printMessage(){
System.out.println(message);
int a =0;
int b =1/a;
}
// add "Hi!" to the message
publicString salutationMessage(){
message ="Hi!"+ message;
System.out.println(message);
return message;
}
}
TUTORIALS POINT
Simply Easy Learning
TUTORIALS POINT
Simply Easy Learning
===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================
TUTORIALS POINT
Simply Easy Learning
CHAPTER
11
TestNG Dependency Test
ometimes, you may need to invoke methods in a Test case in a particular order or you want to share some
data and state between methods. This kind of dependency is supported by TestNG as it supports the declaration of
explicit dependencies between test methods.
TestNG allows you to specify dependencies either with:
TUTORIALS POINT
Simply Easy Learning
Add test methods testPrintMessage(), testSalutationMessage() and initEnvironmentTest() to your test class.
Add
attribute dependsOnMethods
oftestSalutationMessage() method.
"initEnvironmentTest"
} to
the
@Test
annotation
Create the java class file name DependencyTestUsingAnnotation.java in C:\ > TestNG_WORKSPACE
import org.testng.Assert;
import org.testng.annotations.Test;
publicclassDependencyTestUsingAnnotation{
String message ="Manisha";
MessageUtil messageUtil =newMessageUtil(message);
@Test
publicvoid testPrintMessage(){
System.out.println("Inside testPrintMessage()");
message ="Manisha";
Assert.assertEquals(message, messageUtil.printMessage());
}
@Test(dependsOnMethods ={"initEnvironmentTest"})
publicvoid testSalutationMessage(){
System.out.println("Inside testSalutationMessage()");
message ="Hi!"+"Manisha";
Assert.assertEquals(message, messageUtil.salutationMessage());
}
@Test
publicvoid initEnvironmentTest(){
System.out.println("This is initEnvironmentTest");
}
}
CREATE TESTNG.XML
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM
"http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<classes>
<classname="DependencyTestUsingAnnotation"/>
</classes>
</test>
</suite>
Compile the MessageUtil, Test case classes using javac
C:\TestNG_WORKSPACE>javac MessageUtil.java DependencyTestUsingAnnotation.java
Now, run the testng.xml, which will run the testSalutationMessage() method only after the execution
ofinitEnvironmentTest() method.
TUTORIALS POINT
Simply Easy Learning
CREATE A CLASS
Create a java class to be tested say MessageUtil.java in C:\ > TestNG_WORKSPACE
publicclassMessageUtil{
privateString message;
// Constructor
// @param message to be printed
publicMessageUtil(String message){
this.message = message;
}
// prints the message
publicString printMessage(){
System.out.println(message);
return message;
}
// add "Hi!" to the message
publicString salutationMessage(){
message ="Hi!"+ message;
System.out.println(message);
return message;
}
}
Add test methods testPrintMessage(), testSalutationMessage() and initEnvironmentTest() to your test class and
them to the group "init".
TUTORIALS POINT
Simply Easy Learning
import org.testng.Assert;
import org.testng.annotations.Test;
publicclassDependencyTestUsingAnnotation{
String message ="Manisha";
MessageUtil messageUtil =newMessageUtil(message);
@Test(groups ={"init"})
publicvoid testPrintMessage(){
System.out.println("Inside testPrintMessage()");
message ="Manisha";
Assert.assertEquals(message, messageUtil.printMessage());
}
@Test(dependsOnGroups ={"init.*"})
publicvoid testSalutationMessage(){
System.out.println("Inside testSalutationMessage()");
message ="Hi!"+"Manisha";
Assert.assertEquals(message, messageUtil.salutationMessage());
}
@Test(groups ={"init"})
publicvoid initEnvironmentTest(){
System.out.println("This is initEnvironmentTest");
}
}
In this example, testSalutationMessage() is declared as depending on any group matching the regular expression
"init.*", which guarantees that the methods testPrintMessage() and initEnvironmentTest() will always be invoked
before testSalutationMessage().
If a method depended upon fails and you have a hard dependency on it (alwaysRun=false, which is the default), the
methods that depend on it are not marked as FAIL but as SKIP. Skipped methods will be reported as such in the
final report (in a color that is neither red nor green in HTML), which is important since skipped methods are not
necessarily failures.
CREATE TESTNG.XML
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM
"http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<classes>
<classname="DependencyTestUsingAnnotation"/>
</classes>
</test>
</suite>
Compile the MessageUtil, Test case classes using javac
C:\TestNG_WORKSPACE>javac MessageUtil.java DependencyTestUsingAnnotation.java
Now, run the testng.xml, which will run the testSalutationMessage() method only after the execution
ofinitEnvironmentTest() method.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output.
TUTORIALS POINT
Simply Easy Learning
This is initEnvironmentTest
Inside testPrintMessage()
Manisha
Inside testSalutationMessage()
Hi!Manisha
===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================
dependsOnGroups Vs dependsOnMethods
On using groups, we are no longer exposed to refactoring problems. As long as we dont modify the
dependsOnGroups or groups attributes, our tests will keep running with the proper dependencies set up.
Whenever a new method needs to be added in the dependency graph, all we need to do is put it in the right
group and make sure it depends on the correct group. We dont need to modify any other method.
TUTORIALS POINT
Simply Easy Learning
CHAPTER
12
TestNG Parameterized Test
nother interesting feature available in TestNG is parametric testing. In most cases, you'll come across a
scenario where the business logic requires a hugely varying number of tests. Parameterized testsallow developers
to run the same test over and over again using different values.
TestNG lets you pass parameters directly to your test methods in two different ways:
With testng.xml
Add test method parameterTest() to your test class. This method takes a String as input parameter.
Add the annotation @Parameters("myName") to this method. The parameter would be passed a values from
testng.xml which we will see in the next step.
Create the java class file name ParameterizedTest1.java in C:\ > TestNG_WORKSPACE
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
publicclassParameterizedTest1{
@Test
@Parameters("myName")
publicvoid parameterTest(String myName){
System.out.println("Parameterized value is : "+ myName);
}
}
TUTORIALS POINT
Simply Easy Learning
CREATE TESTNG.XML
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM
"http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<parametername="myName"value="manisha"/>
<classes>
<classname="ParameterizedTest1"/>
</classes>
</test>
</suite>
We can also define the parameters at the <suite> level. Suppose we have defined myName at both <suite> and
<test> levels then, in such cases regular scoping rules apply. This means that any class inside <test> tag will see
the value of parameter defined in <test>, while the classes in the rest of the testng.xml file will see the value defined
in <suite>.
Compile the Test case class using javac.
C:\TestNG_WORKSPACE>javac ParameterizedTest1.java
Now, run the testng.xml, which will run the parameterTest method. TestNG will try to find a parameter
named myName first in the <test> tag, and then, if it cant find it, it searches in the <suit> tag that encloses it.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output.
Parameterized value is : manisha
===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================
TestNG will automatically try to convert the value specified in testng.xml to the type of your parameter. Here are the
types supported:
String
int/Integer
boolean/Boolean
byte/Byte
char/Character
double/Double
float/Float
long/Long
TUTORIALS POINT
Simply Easy Learning
short/Short
EXAMPLE 1
Here, the @DataProvider passes Integer and Boolean as parameter.
Define the method primeNumbers() which is defined as a Dataprovider using the annotation. This method
returns array of object array.
Add test method testPrimeNumberChecker() to your test class. This method takes a Integer and Boolean as
input parameters. This method validates if the parameter passed is a prime number.
Add the annotation @Test(dataProvider = "test1") to this method. The attribute dataProvider is mapped to
"test1".
Create the java class file name ParamTestWithDataProvider1.java in C:\ > TestNG_WORKSPACE
import
import
import
import
org.testng.Assert;
org.testng.annotations.BeforeMethod;
org.testng.annotations.DataProvider;
org.testng.annotations.Test;
publicclassParamTestWithDataProvider1{
privatePrimeNumberChecker primeNumberChecker;
TUTORIALS POINT
Simply Easy Learning
@BeforeMethod
publicvoid initialize(){
primeNumberChecker =newPrimeNumberChecker();
}
@DataProvider(name ="test1")
publicstaticObject[][] primeNumbers(){
returnnewObject[][]{{2,true},{6,false},{19,true},
{22,false},{23,true}};
}
// This test will run 4 times since we have 5 parameters defined
@Test(dataProvider ="test1")
publicvoid testPrimeNumberChecker(Integer inputNumber,
Boolean expectedResult){
System.out.println(inputNumber +" "+ expectedResult);
Assert.assertEquals(expectedResult,
primeNumberChecker.validate(inputNumber));
}
}
CREATE TESTNG.XML
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM
"http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<classes>
<classname="ParamTestWithDataProvider1"/>
</classes>
</test>
</suite>
Compile the Test case class using javac.
C:\TestNG_WORKSPACE>.javac ParamTestWithDataProvider1.java PrimeNumberChecker.java
Now, run the testng.xml.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output.
2 true
6 false
19 true
22 false
23 true
===============================================
Suite1
Total tests run: 5, Failures: 0, Skips: 0
===============================================
TUTORIALS POINT
Simply Easy Learning
EXAMPLE 2
Here, the @DataProvider passes Object as parameter.
Define the method primeNumbers() which is defined as a Dataprovider using the annotation. This method
returns array of object array.
Add test method testMethod() to your test class. This method takes object bean as parameter.
Add the annotation @Test(dataProvider = "test1") to this method. The attribute dataProvider is mapped to
"test1".
Create the java class file name ParamTestWithDataProvider2.java in C:\ > TestNG_WORKSPACE
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
publicclassParamTestWithDataProvider2{
@DataProvider(name ="test1")
publicstaticObject[][] primeNumbers(){
returnnewObject[][]{{newBean("hi I am the bean",111)}};
}
@Test(dataProvider ="test1")
publicvoid testMethod(Bean myBean){
System.out.println(myBean.getVal()+" "+ myBean.getI());
}
}
TUTORIALS POINT
Simply Easy Learning
CREATE TESTNG.XML
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM
"http://testng.org/testng-1.0.dtd" >
<suitename="Suite1">
<testname="test1">
<classes>
<classname="ParamTestWithDataProvider2"/>
</classes>
</test>
</suite>
Compile the Test case class using javac.
C:\TestNG_WORKSPACE>javac ParamTestWithDataProvider2.java Bean.java
Now, run the testng.xml.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output.
hi I am the bean 111
===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================
TUTORIALS POINT
Simply Easy Learning
CHAPTER
13
TestNG Run JUnit Tests
ow that you have understood TestNG and its various tests, you must be worried by now as to how to
refactor your existing JUnit code. There's no need to worry as TestNG provides a way to shift from JUnit to TestNG
at your own pace. You can execute your existing Junit test cases using TestNG.
TestNG can automatically recognize and run JUnit tests, so you can use TestNG as a runner for all your existing
tests and write new tests using TestNG. All you have to do is to put JUnit library on the TestNG classpath, so it can
find and use JUnit classes, change your test runner from JUnit to TestNG in Ant and then run TestNG in "mixed"
mode. This way you can have all your tests in the same project, even in the same package, and start using TestNG.
This approach also allows you to convert your existing JUnit tests to TestNG incrementally.
Let us see an example below and try out the above feature:
TUTORIALS POINT
Simply Easy Learning
To execute the JUnit test cases define property junit="true" as in the xml above. The JUnit test case class TestJunit
is defined in class name.
For JUnit 4, TestNG will use the org.junit.runner.JUnitCore runner to run your tests.
Compile all java classes using javac.
C:\TestNG_WORKSPACE>javac TestJunit.java
Now, run the testng.xml, which will run Junit test case as TestNG.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE:C:\TestNG_WORKSPACE\lib\junit4.11.jar" org.testng.TestNG testng.xml
Here, I've placed the junit-4.11.jar under C:\TestNG_WORKSPACE\lib\junit-4.11.jar.
Verify the output.
===============================================
Converted JUnit suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
TUTORIALS POINT
Simply Easy Learning
CHAPTER
14
TestNG Test Results
eporting is the most important part of any test execution, reason being it helps the user to understand the
result of the test execution, point of failure, and reasons for the failure. Logging, on the other hand, is important to
keep an eye on the execution flow or for debugging in case of any failures.
TestNG by default generates a different type of report for its test execution. This includes an HTML and an XML
report output. TestNG also allows its users to write their own reporter and use it with TestNG. There is also an
option to write your own loggers, which are notified at runtime by TestNG.
There are two main ways to generate a report with TestNG:
Listeners
: For
implementing
a
listener
class,
the
class
has
to
implement
theorg.testng.ITestListener interface. These classes are notified at runtime by TestNG when the test starts,
finishes, fails, skips, or passes.
Reporters : For implementing a reporting class, the class has to implement anorg.testng.IReporter interface.
These classes are called when the whole suite run ends. The object containing the information of the whole
test run is passed to this class when called.
The table below lists examples for different cases of reporting and logging:
Custom Logging
Custom Reporter
This example illustrates the default HTML and XML report generated by TestNG.
JUnit Reports
This example illustrates the about generating Junit reports from TestNG reports.
Custom Logging
We had earlier read about the different options that TestNG provides for logging and reporting. Now, let's learn how
to start using them. To start with, we will write a sample program in which we will use the ITestListener interface for
logging purposes.
TUTORIALS POINT
Simply Easy Learning
import org.testng.annotations.Test;
publicclassSampleTest{
@Test
publicvoid testMethodOne(){
Assert.assertTrue(true);
}
@Test
publicvoid testMethodTwo(){
Assert.assertTrue(false);
}
@Test(dependsOnMethods={"testMethodTwo"})
publicvoid testMethodThree(){
Assert.assertTrue(true);
}
}
The preceding test class contains three test methods out of which testMethodOne andtestMethodThree will pass
when executed, whereas testMethodTwo is made to fail by passing a falseBoolean value to the Assert.assertTrue
method, which is used for truth conditions in the tests.
TUTORIALS POINT
Simply Easy Learning
Create testng.xml
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?>
<suitename="Simple Logger Suite">
<listeners>
<listenerclass-name="CustomListener"/>
</listeners>
<testname="Simple Logger test">
<classes>
<classname="SampleTest"/>
</classes>
</test>
</suite>
Compile the SampleTest, CustomListener classes using javac
C:\TestNG_WORKSPACE>javac CustomListener.java SampleTest.java
Now, run the testng.xml.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output.
testMethodOne--Test method success
testMethodTwo--Test method failed
testMethodThree--Test method skipped
===============================================
Simple Logger Suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================
We created a custom logger class, which implements the ITestListener interface and attached itself to the TestNG
test suite as a listener. Methods of this listener class are invoked by TestNG when test started, at test fail, at test
success, and so on. Multiple listeners can be implemented and added to the test suite execution, TestNG will invoke
all the listeners that are attached to the test suite.
Logging listeners are mainly used when we need to see the continuous status of the test execution when the tests
are getting executed.
Custom Reporter
In this section, we will cover, with an example, the method of writing your custom reporter and attaching it to
TestNG. To write a custom reporter class, our extension class should implement the IReporter interface. Let's go
ahead and create an example with the custom reporter.
TUTORIALS POINT
Simply Easy Learning
import org.testng.annotations.Test;
publicclassSampleTest{
@Test
publicvoid testMethodOne(){
Assert.assertTrue(true);
}
@Test
publicvoid testMethodTwo(){
Assert.assertTrue(false);
}
@Test(dependsOnMethods={"testMethodTwo"})
publicvoid testMethodThree(){
Assert.assertTrue(true);
}
}
The preceding test class contains three test methods out of which testMethodOne andtestMethodThree will pass
when executed, whereas testMethodTwo is made to fail by passing a falseBoolean value to the Assert.assertTrue
method, which is used for truth conditions in the tests.
org.testng.IReporter;
org.testng.ISuite;
org.testng.ISuiteResult;
org.testng.ITestContext;
org.testng.xml.XmlSuite;
publicclassCustomReporterimplementsIReporter{
@Override
publicvoid generateReport(List xmlSuites,List suites,
String outputDirectory){
//Iterating over each suite included in the test
for(ISuite suite : suites){
//Following code gets the suite name
String suiteName = suite.getName();
//Getting the results for the said suite
Map suiteResults = suite.getResults();
for(ISuiteResult sr : suiteResults.values()){
ITestContext tc = sr.getTestContext();
System.out.println("Passed tests for suite '"+ suiteName +
"' is:"+ tc.getPassedTests().getAllResults().size());
System.out.println("Failed tests for suite '"+ suiteName +
"' is:"+
tc.getFailedTests().getAllResults().size());
System.out.println("Skipped tests for suite '"+ suiteName +
"' is:"+
tc.getSkippedTests().getAllResults().size());
}
}
}
}
TUTORIALS POINT
Simply Easy Learning
The preceding class implements the org.testng.IReporter interface. It implements the definition for the
method generateReport of the IReporter interface. The method takes three arguments :
the first being xmlSuite, which is the list suites mentioned in the testng XML being executed
The second one being suites which contains the suite information after the test execution; this object contains
all the information about the packages, classes, test methods, and their test execution results.
The third being the outputDirectory, which contains the information of the output folder path, where the reports
will be generated.
Create testng.xml
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?>
<suitename="Simple Reporter Suite">
<listeners>
<listenerclass-name="CustomReporter"/>
</listeners>
<testname="Simple Reporter test">
<classes>
<classname="SampleTest"/>
</classes>
</test>
</suite>
Compile the SampleTest, CustomReporter classes using javac
C:\TestNG_WORKSPACE>javac CustomReporter.java SampleTest.java
Now, run the testng.xml.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output..
===============================================
Simple Reporter Suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================
Passed tests for suite 'Simple Reporter Suite' is:1
Failed tests for suite 'Simple Reporter Suite' is:1
Skipped tests for suite 'Simple Reporter Suite' is:1
The preceding example shows a simple custom reporter, which prints the number of failed, passed, and skipped
tests on the console for each suite included in the said test execution. Reporter is mainly used to generate the final
report for the test execution. The extension can be used to generate XML, HTML, XLS, CSV, or text format files
depending upon the report requirement.
TUTORIALS POINT
Simply Easy Learning
default under the folder named testoutput and can be changed to any other folder by configuring it. These reports
consist of certain HTML and XML reports that are TestNG specific.
Create testng.xml
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?>
<suitename="Simple HTML-XML Suite">
<testname="Simple HTML-XML test">
<classes>
<classname="SampleTest"/>
</classes>
</test>
</suite>
Compile the SampleTest class using javac.
C:\TestNG_WORKSPACE>javac SampleTest.java
Now, run the testng.xml.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
Verify the output.
===============================================
Simple HTML-XML Suite
Total tests run: 3, Failures: 1, Skips: 1
TUTORIALS POINT
Simply Easy Learning
===============================================
Now, go to the C:\TestNG_WORKSPACE\test-output folder. Open the index.html on your default web browser. You
will see the following HTML report:
Now, open the file C:\TestNG_WORKSPACE\test-output\testing-results.xml in the default XML editor on your
system, and you will see the following results in the XML file:
TestNG by default generates multiple reports as part of its test execution. These reports mainly include TestNG
HTML report, TestNG emailable report, TestNG report XML, and JUnit report XML files. These files can be found
under the output report folder (in this case test-output). This default report generation can be disabled while
running the tests by setting the value of the property useDefaultListeners to false. This property can be set while
using the build tools like Ant or Maven
JUnit Reports
TUTORIALS POINT
Simply Easy Learning
JUnit is one of those unit frameworks, which were initially used by many Java applications as a Unit test framework.
By default, JUnit tests generate a simple report XML files for its test execution. These XML files can then be used to
generate any custom reports as per the testing requirement. We can also generate HTML reports using the XML
files. Ant has such a utility task which takes these JUnit XML files as input and generates an HTML report from it.
TestNG by default generates the JUnit XML reports for any test execution (in the test-output folder). We can use
these XML report files as input for generation of a JUnit HTML report. Let us see an example below:
Create testng.xml
Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).
<?xml version="1.0" encoding="UTF-8"?>
<suitename="Simple Suite">
<testname="Simple test">
<classes>
<classname="SampleTest"/>
</classes>
</test>
</suite>
Compile the SampleTest class using javac.
C:\TestNG_WORKSPACE>javac SampleTest.java
Now, run the testng.xml.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
TUTORIALS POINT
Simply Easy Learning
TUTORIALS POINT
Simply Easy Learning
Here, we have seen how to use the JUnit XML report generated by TestNG and generate HTML report using Ant.
There are two kinds of reports that can be generated using this method: frames and no-frames. If the report
generation is configured with frames, there will multiple files generated for each class and the main report will
connect to them through links. A no-frames report consists of a single file with all the results of the test execution.
This can be configured by providing the respective value to the format attribute of the report task in Ant.
TUTORIALS POINT
Simply Easy Learning
CHAPTER
15
TestNG Plug with ANT
n this example, we will demonstrate how to run TestNG using ANT. Let's follow the given steps:
Archive name
Windows
apache-ant-1.8.4-bin.zip
Linux
apache-ant-1.8.4-bin.tar.gz
Mac
apache-ant-1.8.4-bin.tar.gz
Output
Windows
Linux
export ANT_HOME=/usr/local/\apache-ant-1.8.4
Mac
export ANT_HOME=/Library/\apache-ant-1.8.4
Append Ant compiler location to System Path as follows for different OS:
OS
Output
Windows
Append the string ;%ANT_HOME\bin to the end of the system variable, Path.
TUTORIALS POINT
Simply Easy Learning
Linux
export PATH=$PATH:$ANT_HOME/bin/
Mac
not required
Archive name
Windows
testng-6.8.jar
Linux
testng-6.8.jar
Mac
testng-6.8.jar
/*
* This class prints the given message on console.
*/
publicclassMessageUtil{
privateString message;
//Constructor
//@param message to be printed
publicMessageUtil(String message){
this.message = message;
}
// prints the message
publicvoid printMessage(){
System.out.println(message);
return message;
}
// add "Hi!" to the message
publicString salutationMessage(){
message ="Hi!"+ message;
System.out.println(message);
return message;
}
}
Create TestMessageUtil class in C:\ > TestNG_WORKSPACE > TestNGWithAnt > src folder.
import org.testng.Assert;
import org.testng.annotations.Test;
TUTORIALS POINT
Simply Easy Learning
publicclassTestMessageUtil{
String message ="Manisha";
MessageUtil messageUtil =newMessageUtil(message);
@Test
publicvoid testPrintMessage(){
System.out.println("Inside testPrintMessage()");
Assert.assertEquals(message,messageUtil.printMessage());
}
@Test
publicvoid testSalutationMessage(){
System.out.println("Inside testSalutationMessage()");
message ="Hi!"+"Manisha";
Assert.assertEquals(message,messageUtil.salutationMessage());
}
}
Copy testng-6.8.jar in C:\ > TestNG_WORKSPACE > TestNGWithAnt > lib folder
TUTORIALS POINT
Simply Easy Learning
</target>
<targetname="compile"depends="clean">
<javacsrcdir="${srcdir}"destdir="${testdir}"
verbose="${full-compile}">
<classpathrefid="classpath.test"/>
</javac>
</target>
<targetname="test"depends="compile">
<testngoutputdir="${testdir}"classpathref="classpath.test">
<xmlfilesetdir="${srcdir}"includes="testng.xml"/>
</testng>
</target>
</project>
Run the following ant command.
C:\TestNG_WORKSPACE\TestNGWithAnt>ant
Verify the output.
test:
[testng]
[testng]
[testng]
[testng]
[testng]
[testng]
[testng]
[testng]
[testng]
[testng]
[testng]
[testng]
[testng]
[TestNG] Running:
C:\TestNG_WORKSPACE\TestNGWithAnt\src\testng.xml
Inside testPrintMessage()
Manisha
Inside testSalutationMessage()
Hi!Manisha
===============================================
Plug ANT test Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
BUILD SUCCESSFUL
Total time: 1 second
TUTORIALS POINT
Simply Easy Learning
CHAPTER
16
TestNG - Plug with Eclipse
Archive name
Windows
testng-6.8.jar
Linux
testng-6.8.jar
Mac
testng-6.8.jar
Open eclipse -> right click on project and click on property > Build Path > Configure Build Path and add the
testng-6.8.jar in the libraries using Add External Jar button.
TUTORIALS POINT
Simply Easy Learning
We assume that your eclipse has inbuilt TestNG plug-in; if it is not available, then please get the latest version
using the update site:
o
o
o
o
o
o
o
In your eclipse IDE, select Help / Software updates / Find and Install.
Search for new features to install.
New remote site.
For Eclipse 3.4 and above, enter http://beust.com/eclipse.
For Eclipse 3.3 and below, enter http://beust.com/eclipse1.
Make sure the check box next to URL is checked and click Next.
Eclipse will then guide you through the process.
Now, your eclipse is ready for the development of TestNG test cases.
/*
* This class prints the given message on console.
*/
publicclassMessageUtil{
privateString message;
TUTORIALS POINT
Simply Easy Learning
//Constructor
//@param message to be printed
publicMessageUtil(String message){
this.message = message;
}
// prints the message
publicString printMessage(){
System.out.println(message);
return message;
}
}
import org.testng.Assert;
import org.testng.annotations.Test;
publicclassTestNGExample{
String message ="Hello World";
MessageUtil messageUtil =newMessageUtil(message);
@Test
publicvoid testPrintMessage(){
Assert.assertEquals(message,messageUtil.printMessage());
}
}
Following should be the project structure:
Finally, verify the output of the program by right clicking on program and running as TestNG.
Verify the result.
TUTORIALS POINT
Simply Easy Learning
TUTORIALS POINT
Simply Easy Learning