TestNG Documentation
TestNG Documentation
7.9.0
Table Of Contents
1. Welcome to TestNG
1.1. Requirements
1.2. Mailing-lists
1.3. Locations of the projects
1.4. Bug reports
1.5. License
2. Download
2.1. Current Release Version
2.1.1. Maven
2.1.2. Gradle
2.1.3. Snapshots
2.1.4. Eclipse plug-in
2.1.5. Build TestNG from source code
2.1.6. Build the TestNG Eclipse Plugin from source code
3. TestNG Documentation
3.1. Introduction
3.2. Annotations
3.3.
testng.xml
3.4. Running TestNG
3.4.1. Command Line Parameters
3.4.2. System properties
3.4.3. Running tests from within a test jar
3.5. Test methods, Test classes and Test groups
3.5.1. Test methods
3.5.2. Test groups
Method groups
3.5.3. Groups of groups
3.5.4. Exclusion groups
3.5.5. Partial groups
3.6. Parameters
3.6.1. Parameters from testng.xml
3.6.2. Parameters with DataProviders
3.6.3. Retries and data providers
3.6.4. Controlling ThreadPool Usage
3.6.5. Parameters from System Properties
Parameters in reports
3.7. Dependencies
3.7.1. Dependencies with annotations
3.7.2. Dependencies in XML
3.8. Factories
3.9. Class level annotations
3.10. Ignoring tests
3.11. Parallelism and time-outs
3.11.1. Parallel suites
3.11.2. Parallel tests, classes and methods
3.12. Rerunning failed tests
3.13. JUnit tests
3.14. BeanShell and advanced group selection
3.14.1. Annotation Transformers
3.14.2. Via xml suite file
3.14.3. Via command line arguments
3.14.4. Via your code
3.15. Method Interceptors
3.16. Interceptors for Data Providers
3.17. TestNG Listeners
3.17.1. Listener list
3.17.2. Specifying listeners with testng.xml or in Java
3.17.3. Specifying listeners with ServiceLoader
3.17.4. Ordering listeners in TestNG
3.18. Dependency injection
3.18.1. Native dependency injection
3.18.2. Guice dependency injection
3.19. Listening to TestNG lifecycle events
3.20. Listening to method invocations
3.21. Listening to configuration invocations
3.22. Listening to class level invocations
3.23. Listening to data provider invocations
3.24. Listening to Suite level invocations
3.25. Overriding test methods
3.26. Overriding configuration methods
3.27. Altering suites (or) tests
4. Test results
4.1. Success, failure and assert
4.2. Logging and results
4.3. Logging Listeners
4.4. Logging Reporters
4.5. JUnit Reports
4.6. Reporter API
4.7. XML Reports
4.8. TestNG Exit Codes
5. YAML
6. Dry Run for your tests
7. JVM Arguments in TestNG
8. Logging framework integration in TestNG
9. Migrating from JUnit
9.1. Using Eclipse
9.2. Asserts
9.3. Running JUnit Tests
9.4. Related reading
10. Javadocs
11. Running TestNG
11.1. Eclipse plug-in
11.2. TestNG IDEA Plug-in
11.3. Using TestNG with Gradle
11.3.1. Specifying your Gradle build file
11.4. Using TestNG with Maven
11.4.1. Specifying your pom.xml
11.4.2. Sample reports
11.5. TestNG Ant Task
11.6. Running TestNG programmatically
12. MORE TESTNG READING
12.1. Articles and presentations
12.1.1. English
12.1.2. French
12.1.3. German
12.2. CONTRIBUTORS
12.3. SPECIAL THANKS
12.4. TESTIMONIES
13. The TestNG TextBook: Next Generation Java Testing
13.1. Book Description
13.2. About the Author
14. ReportNG - A reporting tool
15. GitHub Repository
15.1. TestNG Core
15.2. TestNG Documentation
15.3. TestNG Eclipse plugin
15.4. ReportNG - A reporting tool
1. Welcome to TestNG
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:
Annotations.
Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc…).
Test that your code is multithread safe.
Flexible test configuration.
Support for data-driven testing (with @DataProvider).
Support for parameters.
Powerful execution model (no more TestSuite).
Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc…).
Embeds BeanShell for further flexibility.
Default JDK functions for runtime and logging (no dependencies).
Dependent methods for application server testing.
TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc…
I started TestNG out of frustration for some JUnit deficiencies which I have documented on my weblog here and here Reading these entries might give you a better idea of the goal I am trying to achieve with TestNG. You can also check out a quick
overview of the main features
and an
article
describing a very concrete example where the combined use of several TestNG’s features provides for a very intuitive and maintainable testing design.
Here is a very simple test:
package example1;
import org.testng.annotations.*;
public class SimpleTest {
@BeforeClass
public void setUp() {
// code that will be invoked when this test is instantiated
@Test(groups = {"fast"})
public void aFastTest() {
System.out.println("Fast test");
@Test(groups = {"slow"})
public void aSlowTest() {
System.out.println("Slow test");
The method
setUp()
will be invoked after the test class has been built and before any test method is run. In this example, we will be running the group fast, so
aFastTest()
will be invoked while
aSlowTest()
will be skipped.
Things to note:
No need to extend a class or implement an interface.
Even though the example above uses the JUnit conventions, our methods can be called any name you like, it’s the annotations that tell TestNG what they are.
A test method can belong to one or several groups.
Once you have compiled your test class into the build directory, you can invoke your test with the command line, an ant task (shown below) or an XML file:
Use ant to invoke it:
c:> ant
Buildfile: build.xml
test:
[testng] Fast test
[testng] ===============================================
[testng] Suite for Command line test
[testng] Total tests run: 1, Failures: 0, Skips: 0
[testng] ===============================================
BUILD SUCCESSFUL
Total time: 4 seconds
Then you can browse the result of your tests:
start test-output\index.html (on Windows)
1.1. Requirements
TestNG Upto v7.5: JDK 8.
TestNG v7.6.0 and above: JDK 11 or higher.
1.2. Mailing-lists
The users mailing-list can be found on
Google Groups
If you are interested in working on TestNG itself, join the
developer mailing-list
1.3. Locations of the projects
If you are interested in contributing to TestNG or one of the IDE plug-ins, you will find them in the following locations:
TestNG
Eclipse plug-in
IDEA IntelliJ plug-in
Visual Studio Code plugin-in
1.4. Bug reports
If you think you found a bug, here is how to report it:
Create a small project that will allow us to reproduce this bug. In most cases, one or two Java source files and a testng.xml file should be sufficient. Then you can either zip it and email it to the
testng-dev mailing-list
or make it available on an open source hosting site, such as
github
and email testng-dev so we know about it. Please make sure that this project is self contained so that we can build it right away (remove the dependencies on external or proprietary frameworks, etc…).
If the bug you observed is on the Eclipse plug-in, make sure your sample project contains the
.project
and
.classpath
files.
File a bug.
For more information, you can either
download TestNG
, read the manual or browse the links at the top.
1.5. License
Apache 2.0
2. Download
2.1. Current Release Version
2.1.1. Maven
2.1.2. Gradle
dependencies {
testCompile 'org.testng:testng:7.9.0'
2.1.3. Snapshots
TestNG automatically uploads
snapshots to Sonatype
which you can access by adding the following repository:
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
2.1.4. Eclipse plug-in
See
the related documentation
2.1.5. Build TestNG from source code
TestNG is also hosted on GitHub, where you can download the source and build the distribution yourself:
$ git clone https://github.com/testng-team/testng.git
$ cd testng
$ ./gradlew build
You will then find the jar file in the
testng/build/libs
directory
Some useful tasks:
./gradlew tasks
- to see what all tasks are available
./gradlew test
- to kick off the tests (incremental build)
./gradlew clean test
- if you want to have a clean build.
./gradlew autostyleApply
- Applies code formatting steps to sourcecode in-place.
./gradlew autostyleCheck
- Checks that sourcecode satisfies formatting steps.
./gradlew check
- Runs the below checks:
"Applies code formatting steps to sourcecode in-place." and
"Checks that sourcecode satisfies formatting steps.".
2.1.6. Build the TestNG Eclipse Plugin from source code
TestNG Eclipse Plugin is
hosted on GitHub
, you can download the source code and
build by ourselves
3. TestNG Documentation
3.1. Introduction
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
Writing a test is typically a three-step process:
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
You can find a quick example on the
Welcome page
The concepts used in this documentation are as follows:
A suite is represented by one XML file. It can contain one or more tests and is defined by the
tag.
A test is represented by
A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the
A test method is a Java method annotated by
@Test
in your source.
A TestNG test can be configured by
@BeforeXXX
and
@AfterXXX
annotations which allows to perform some Java logic before and after a certain point, these points being either of the items listed above.
The rest of this manual will explain the following:
A list of all the annotations with a brief explanation. This will give you an idea of the various functionalities offered by TestNG but you will probably want to consult the section dedicated to each of these annotations to learn the details.
A description of the testng.xml file, its syntax and what you can specify in it.
A detailed list of the various features and how to use them with a combination of annotations and testng.xml.
3.2. Annotations
Type
Annotations
Description
Annotation
@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
@BeforeGroups
@AfterGroups
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
Configuration information for a TestNG class:
@BeforeSuite
: The annotated method will be run before all tests in this suite have run.
@AfterSuite
: The annotated method will be run after all tests in this suite have run.
@BeforeTest
: The annotated method will be run before any test method belonging to the classes inside the
@AfterTest
: The annotated method will be run after all the test methods belonging to the classes inside the
@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.
@BeforeClass
: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass
: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod
: The annotated method will be run before each test method.
@AfterMethod
: The annotated method will be run after each test method.
Behaviour of annotations in superclass of a TestNG class
The annotations above will also be honored (inherited) when placed on a superclass of a TestNG class. This is useful for example to centralize test setup for multiple test classes in a common superclass.
In that case, TestNG guarantees that the "@Before" methods are executed in inheritance order (highest superclass first, then going down the inheritance chain), and the "@After" methods in reverse order (going up the inheritance chain).
Annotation Attributes
alwaysRun
For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.
For after methods (afterSuite, afterClass, …): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
dependsOnGroups
The list of groups this method depends on.
dependsOnMethods
The list of methods this method depends on.
enabled
Whether methods on this class/method are enabled.
groups
The list of groups this class/method belongs to.
inheritGroups
If true, this method will belong to groups specified in the @Test annotation at the class level.
onlyForGroups
Only for @BeforeMethod and @AfterMethod. If specified, then this setup/teardown method will only be invoked if the corresponding test method belongs to one of the listed groups.
Annotation
@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.
Annotation Attributes
name
The name of this data provider. If it’s not supplied, the name of this data provider will automatically be set to the name of the method.
parallel
If set to true, tests generated using this data provider are run in parallel. Default value is false.
Annotation
@Factory
Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return
Object[]
Annotation
@Listeners
Defines listeners on a test class.
Annotation Attributes
value
An array of classes that extend org.testng.ITestNGListener.
Annotation
@Parameters
Describes how to pass parameters to a @Test method.
Annotation Attributes
value
The list of variables used to fill the parameters of this method.
Annotation
@Test
Marks a class or a method as part of the test.
Annotation Attributes
alwaysRun
If set to true, this test method will always be run even if it depends on a method that failed.
dataProvider
The name of the data provider for this test method.
dataProviderClass
The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.
dependsOnGroups
The list of groups this method depends on.
dependsOnMethods
The list of methods this method depends on.
description
The description for this method.
enabled
Whether methods on this class/method are enabled.
expectedExceptions
The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
groups
The list of groups this class/method belongs to.
invocationCount
The number of times this method should be invoked.
invocationTimeOut
The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.
priority
The priority for this test method. Lower priorities will be scheduled first.
successPercentage
The percentage of success expected from this method
singleThreaded
If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with
parallel="methods"
. This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called sequential (now deprecated).
timeOut
The maximum number of milliseconds this test should take.
threadPoolSize
The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount.
NOTE: this attribute is ignored if invocationCount is not specified
3.3.
testng.xml
You can invoke TestNG in several different ways:
With a
testng.xml
file
With
ant
From the command line
This section describes the format of
testng.xml
(you will find documentation on ant and the command line below).
The current DTD for testng.xml can be found on the main Web site:
testng-1.0.dtd
Here is an example testng.xml file:
You can specify package names instead of class names:
In this example, TestNG will look at all the classes in the package
test.sample
and will retain only classes that have TestNG annotations.
You can also specify groups and methods to be included and excluded:
You can also define new groups inside
testng.xml
and specify additional details in attributes, such as whether to run the tests in parallel, how many threads to use, whether you are running JUnit tests, etc…
By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictable order, set the
preserve-order
attribute to false.
Please see the DTD for a complete list of the features, or read on.
3.4. Running TestNG
TestNG can be invoked in different ways:
Command line
ant
Eclipse
IntelliJ’s IDEA
This section only explains how to invoke TestNG from the command line.
Please click on one of the links above if you are interested in one of the other ways.
Assuming that you have TestNG in your class path, the simplest way to invoke TestNG is as follows:
java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]
You need to specify at least one XML file describing the TestNG suite you are trying to run.
Additionally, the following command-line switches are available:
3.4.1. Command Line Parameters
Option
Argument
Documentation
-configfailurepolicy
skip
continue
Whether TestNG should continue to execute the remaining tests in the suite or skip them if an @Before* method fails. Default behavior is
skip
-d
A directory
The directory where the reports will be generated (defaults to
test-output
).
-dataproviderthreadcount
The default number of threads to use for data providers when running tests in parallel.
This sets the default maximum number of threads to use for data providers when running tests in parallel. It will only take effect if the parallel mode has been selected (for example, with the
-parallel
option). This can be overridden in the suite definition.
-excludegroups
A comma-separated list of groups.
The list of groups you want to be excluded from this run.
-groups
A comma-separated list of groups.
The list of groups you want to run (e.g. "windows,linux,regression").
-listener
A comma-separated list of Java classes that can be found on your classpath.
Lets you specify your own test listeners. The classes need to implement
org.testng.ITestListener
-usedefaultlisteners
true
false
Whether to use the default listeners
-methods
A comma separated list of fully qualified class name and method. For example
com.example.Foo.f1,com.example.Bar.f2
Lets you specify individual methods to run.
-methodselectors
A comma-separated list of Java classes and method priorities that define method selectors.
Lets you specify method selectors on the command line. For example:
com.example.Selector1:3,com.example.Selector2:2
-parallel
methods,tests,classes
If specified, sets the default mechanism used to determine how to use parallel threads when running tests. If not set, default mechanism is not to use parallel threads at all. This can be overridden in the suite definition.
-reporter
The extended configuration for a custom report listener.
Similar to the
-listener
option, except that it allows the configuration of JavaBeans-style properties on the reporter instance.
Example:
-reporter com.test.MyReporter:methodFilter=
insert
,enableFiltering=true
You can have as many occurrences of this option, one for each reporter that needs to be added.
-sourcedir
A semicolon separated list of directories.
The directories where your javadoc annotated test sources are. This option is only necessary if you are using javadoc type annotations. (e.g.
src/test
or
src/test/org/testng/eclipse-plugin;src/test/org/testng/testng
).
-suitename
The default name to use for a test suite.
This specifies the suite name for a test suite defined on the command line. This option is ignored if the
suite.xml
file or the source code specifies a different suite name. It is possible to create a suite name with spaces in it if you surround it with double-quotes
"like this"
-testclass
A comma-separated list of classes that can be found in your classpath.
A list of class files separated by commas (e.g.
org.foo.Test1,org.foo.test2
).
-testjar
A jar file.
Specifies a jar file that contains test classes. If a
testng.xml
file is found at the root of that jar file, it will be used, otherwise, all the test classes found in this jar file will be considered test classes.
-testname
The default name to use for a test.
This specifies the name for a test defined on the command line. This option is ignored if the suite.xml file or the source code specifies a different test name. It is possible to create a test name with spaces in it if you surround it with double-quotes
"like this"
-testnames
A comma separated list of test names.
Only tests defined in a
-testrunfactory
A Java classes that can be found on your classpath.
Lets you specify your own test runners. The class needs to implement
org.testng.ITestRunnerFactory
-threadcount
The default number of threads to use when running tests in parallel.
This sets the default maximum number of threads to use for running tests in parallel. It will only take effect if the parallel mode has been selected (for example, with the -parallel option). This can be overridden in the suite definition.
-xmlpathinjar
The path of the XML file inside the jar file.
This attribute should contain the path to a valid XML file inside the test jar (e.g.
resources/testng.xml
). The default is
testng.xml
, which means a file called
testng.xml
at the root of the jar file. This option will be ignored unless
-testjar
is specified.
-shareThreadPoolForDataProviders
true
false
Indicates if TestNG should use a global shared thread-pool (at suite level) for running data driven tests. TestNG will consider the value set for the configuration parameter
-dataproviderthreadcount
as the size of the thread pool.
-useGlobalThreadPool
true
false
Indicates if TestNG should use a global shared thread-pool (at suite level) for running regular and data driven tests. TestNG will consider the value set for the configuration parameter
-threadcount
as the size of the thread pool.
-log
(or)
-verbose
a valid log level
Level of verbosity to be used when logging messages.
-junit
true
false
Should TestNG run in JUnit mode.
-mixed
true
false
Mixed mode - autodetect the type of current test and run it with appropriate runner.
-objectfactory
A string that represents a fully qualified class name.
Fully qualified class name that implements
org.testng.ITestObjectFactory
which can be used to create test class and listener instances.
-ignoreMissedTestNames
true
false
Ignore missed test names given by
-testnames
and continue to run existing tests, if any.
-skipfailedinvocationcounts
true
false
Should TestNG skip failed invocation counts for data driven tests and tests driven by invocation counts.
-testRunFactory
A string that represents a fully qualified class name.
Fully qualified class name that implements
org.testng.ITestRunnerFactory
which can be used to create custom test runners for running tests.
-suitethreadpoolsize
An integer value that represents the thread pool size. When not specified, defaults to
Size of the thread pool to use to run suites.
-randomizesuites
true
false
Whether to run suites in same order as specified in XML or not.
-alwaysrunlisteners
true
false
Should method invocation listeners be run even for skipped methods.
-dependencyinjectorfactory
A string that represents a fully qualified class name.
Fully qualified class name that implements org.testng.IInjectorFactory which can be used to handle with dependency injection.
`-failwheneverythingskipped'
true
false
Should TestNG fail execution if all tests were skipped and nothing was run.
-spilistenerstoskip
A comma separated string that represents a fully qualified class name(s).
Comma separated fully qualified class names of listeners that should be skipped from being wired in via Service Loaders.
-overrideincludedmethods
true
false
Should TestNG exclude explicitly included test methods if they belong to any excluded groups as defined in the suite xml file.
-includeAllDataDrivenTestsWhenSkipping
true
false
Should TestNG report all iterations of a data driven test as individual skips, in-case of upstream failures.
-propagateDataProviderFailureAsTestFailure
true
false
Should TestNG consider failures in Data Providers as test failures.
-generateResultsPerSuite
true
false
Should TestNG generate results on a per suite basis by creating a sub directory for each suite and dumping results into it.
-shareThreadPoolForDataProviders
true
false
Should TestNG use a global Shared ThreadPool (At suite level) for running data providers.
-useGlobalThreadPool
true
false
Should TestNG use a global Shared ThreadPool (At suite level) for running regular and data driven tests.
This documentation can be obtained by invoking TestNG without any arguments.
You can also put the command line switches in a text file, say
c:\command.txt
, and tell TestNG to use that file to retrieve its parameters:
C:> more c:\command.txt
-d test-output testng.xml
C:> java org.testng.TestNG @c:\command.txt
Additionally, TestNG can be passed properties on the command line of the Java Virtual Machine, for example
java -Dtestng.test.classpath="c:/build;c:/java/classes;" org.testng.TestNG testng.xml
Here are the properties that TestNG understands:
3.4.2. System properties
Property
Type
Documentation
testng.test.classpath
A semicolon separated series of directories that contain your test classes.
If this property is set, TestNG will use it to look for your test classes instead of the class path. This is convenient if you are using the package tag in your XML file and you have a lot of classes in your classpath, most of them not being test classes.
Example:
java org.testng.TestNG -groups windows,linux -testclass org.test.MyTest
The ant task and
testng.xml
allow you to launch TestNG with more parameters (methods to include, specifying parameters, etc…), so you should consider using the command line only when you are trying to learn about TestNG and you want to get up and running quickly.
The command line flags that specify what tests should be run will be ignored if you also specify a testng.xml file, with the exception of
-groups
and
-excludegroups
, which will override all the group inclusions/exclusions found in
testng.xml
3.4.3. Running tests from within a test jar
TestNG can be provided with a jar that contains your test classes and you can execute the tests from within it.
Let’s see an example (We are going to use Maven for this example.)
We will be using
maven-assembly-plugin
and
maven-jar-plugin
to demonstrate how to do this.
The project’s directory structure will look like below:
├── pom.xml
└── src
└── test
├── java
│ └── org
│ └── testng
│ ├── FirstTestCase.java
│ └── SecondTestCase.java
└── resources
├── suites
│ ├── suite1.xml
│ └── suite2.xml
└── test-jar-with-dependencies.xml
The relevant
section and the
section will look like below:
The contents of
src/test/resources/test-jar-with-dependencies.xml
will look like below:
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd
Now let’s build the jars using the command
mvn clean package
Now in order to run the tests, use the below command:
java -jar target/uber-testjar-demo-1.0-SNAPSHOT-test-jar-with-dependencies.jar -testjar target/uber-testjar-demo-1.0-SNAPSHOT-tests.jar
This command causes TestNG to look for test classes in the jar and it executes all of them.
Here:
target/uber-testjar-demo-1.0-SNAPSHOT-test-jar-with-dependencies.jar
- Represents the uber/fat jar which contains all the dependencies inside it.
target/uber-testjar-demo-1.0-SNAPSHOT-tests.jar
- Contains all the tests that we created.
-testjar
- This argument informs TestNG that it should look for test classes inside the jar and NOT in the current CLASSPATH.
If you would like to execute a specific suite file that exists in the jar, then use the below command:
java -jar target/uber-testjar-demo-1.0-SNAPSHOT-test-jar-with-dependencies.jar -testjar target/uber-testjar-demo-1.0-SNAPSHOT-tests.jar -xmlpathinjar suites/suite2.xml
Here we are specifying the path to the suite file using the command line argument
-xmlpathinjar
For more details, refer to
this stackoverflow post
3.5. Test methods, Test classes and Test groups
3.5.1. Test methods
Test methods are annotated with @Test. Methods annotated with @Test that happen to return a value will be ignored, unless you set allow-return-values to true in your testng.xml:
or
3.5.2. Test groups
TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods 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 and can be found either under the
or
tag. Groups specified in the
tag apply to all the
tags underneath. Note that groups are accumulative in these tags: if you specify group "a" in
and "b" in
, then both "a" and "b" will be included.
For example, it is quite common to have at least two categories of tests:
Check-in tests.
These tests should be run before you submit new code. They should typically be fast and just make sure no basic functionality was broken.
Functional tests.
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.
Typically, check-in tests are a subset of functional tests. TestNG allows you to specify this in a very intuitive way with test groups. For example, you could structure your test by saying that your entire test class belongs to the "functest" group, and additionally that a couple of methods belong to the group "checkintest":
public class Test1 {
@Test(groups = { "functest", "checkintest" })
public void testMethod1() {
@Test(groups = {"functest", "checkintest"} )
public void testMethod2() {
@Test(groups = { "functest" })
public void testMethod3() {
Invoking TestNG with
will run all the test methods in that classes, while invoking it with
checkintest
will only run
testMethod1()
and
testMethod2()
Here is another example, using regular expressions this time. Assume that some of your test methods should not be run on Linux, your test would look like:
@Test
public class Test1 {
@Test(groups = { "windows.checkintest" })
public void testWindowsOnly() {
@Test(groups = {"linux.checkintest"} )
public void testLinuxOnly() {
@Test(groups = { "windows.functest" } )
public void testWindowsToo() {
You could use the following
testng.xml
to launch only the Windows methods:
Note: TestNG uses
regular expressions
, and not
wildmats
. Be aware of the difference (for example,
"anything"
is matched by
".
— dot star — and not
).
Method groups
You can also exclude or include individual methods:
This can come in handy to deactivate a single method without having to recompile anything, but I don’t recommend using this technique too much since it makes your testing framework likely to break if you start refactoring your Java code (the regular expressions used in the tags might not match your methods anymore).
3.5.3. 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". "functest" itself will contain the groups "windows" and "linux" while "checkintest will only contain "windows". Here is how you would define this in your property file:
3.5.4. Exclusion groups
TestNG allows you to include groups as well as exclude them.
For example, it is quite usual to have tests that temporarily break because of a recent change, and you don’t have time to fix the breakage yet. 4 However, you do want to have clean runs of your functional tests, so you need to deactivate these tests but keep in mind they will need to be reactivated.
A simple way to solve this problem is to create a group called "broken" and make these test methods belong to it. For example, in the above example, I know that
testMethod2()
is now broken so I want to disable it:
@Test(groups = {"checkintest", "broken"} )
public void testMethod2() {
All I need to do now is to exclude this group from the run:
This way, I will get a clean test run while keeping track of what tests are broken and need to be fixed later.
you can also disable tests on an individual basis by using the "enabled" property available on both
@Test
and
@Before
@After
annotations.
3.5.5. Partial groups
You can define groups at the class level and then add groups at the method level:
@Test(groups = { "checkin-test" })
public class All {
@Test(groups = { "func-test" })
public void method1() {
//test code goes here
public void method2() {
//more code goes here
In this class,
method2()
is part of the group "checkin-test", which is defined at the class level, while
method1()
belongs to both "checkin-test" and "func-test".
3.6. Parameters
TestNG allows you to pass an arbitrary number of parameters to each of your tests using the `@Parameters` annotation.
There are three ways to set these parameters
The testng.xml file
Programmatically
Java system properties
3.6.1. Parameters from testng.xml
If you are using simple values for your parameters, you can specify them in your
testng.xml
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
In this code, we specify that the parameter
firstName
of your Java method should receive the value of the XML parameter called
first-name
. This XML parameter is defined in
testng.xml
The same technique can be used for
@Before
@After
and
@Factory
annotations:
@Parameters({ "datasource", "jdbc-driver" })
@BeforeMethod
public void beforeTest(String ds, String driver) {
m_dataSource = buildDataSource(); // look up the value of datasource
m_jdbcDriver = driver;
This time, the two Java parameter
ds
and
driver
will receive the value given to the properties
datasource
and
jdbc-driver
respectively.
Parameters can be declared optional with the
org.testng.annotations.Optional
annotation:
@Parameters("db")
@Test
public void testNonExistentParameter(@Optional("mysql") String db) {
//more code
If no parameter named "db" is found in your
testng.xml
file, your test method will receive the default value specified inside the
@Optional
annotation: "mysql".
The
@Parameters
annotation can be placed at the following locations:
On any method that already has a
@Test
@Before
@After
or
@Factory
annotation.
On at most one constructor of your test class. In this case, TestNG will invoke this particular constructor with the parameters initialized to the values specified in
testng.xml
whenever it needs to instantiate your test class. This feature can be used to initialize fields inside your classes to values that will then be used by your test methods.
Important:
The XML parameters are mapped to the Java parameters in the same order as they are found in the annotation, and TestNG will issue an error if the numbers don’t match.
Parameters are scoped. In testng.xml, you can declare them either under:
tag or
tag or
tag or
tag.
The order of precedence (lowest to highest) in terms of resolving values for parameters with same names is
If two parameters have the same name, it’s the one defined in
that has precedence. This is convenient if you need to specify a parameter applicable to all your tests and override its value only for certain test method.
3.6.2. Parameters with DataProviders
Specifying parameters in testng.xml might not be sufficient if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc…). In this case, you can use a Data Provider to supply the values you need to test. A Data Provider is a method on your class that returns an array of array of objects. This method is annotated with
@DataProvider
//This method will provide data to any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", 36 },
{ "Anne", 37},
};
//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
will print
Cedric 36
Anne 37
@Test
method specifies its Data Provider with the
dataProvider
attribute. This name must correspond to a method on the same class annotated with
@DataProvider(name="…")
with a matching name.
By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the
dataProviderClass
attribute:
public class StaticProvider {
@DataProvider(name = "create")
public static Object[][] createData() {
return new Object[][] {
new Object[] { 42 }
};
public class MyTest {
@Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
public void test(Integer n) {
// ...
The data provider supports injection too. TestNG will use the test context for the injection. The Data Provider method can return one of the following types:
An array of array of objects (
Object[][]
) where the first dimension’s size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. This is the case illustrated by the example above.
An
Iterator
@Test(dataProvider = "dp")
public void test1(String s) {
@Test(dataProvider = "dp")
public void test2(String s) {
and will therefore display:
test1
test2
Data providers can run in parallel with the attribute parallel:
@DataProvider(parallel = true)
public Object[][] getTestData() {
// ...
Each of the parallel data providers running from an XML file runs with a thread pool which has a size of
10
by default. You can modify this value in the
tag of your XML file:
If you want to run a few specific data providers in a different thread pool, you need to run them from a different XML file.
3.6.3. Retries and data providers
TestNG allows you to retry a data provider incase it has encountered any issues when calling it the first time.
This is similar to how regular test methods can be retried as explained in
this section
To be able to retry a data provider, the following needs to be done.
First we would need to implement the interface
org.testng.IRetryDataProvider
Next you would need to tie this implementation to the data provider annotation using the attribute
retryUsing
of the
@DataProvider
annotation.
With that we can now retry a failed data provider.
Here’s a sample retry implementation:
import org.testng.IDataProviderMethod;
import org.testng.IRetryDataProvider;
import java.util.concurrent.atomic.AtomicInteger;
public class RetryDataProvider implements IRetryDataProvider {
private final AtomicInteger counter = new AtomicInteger(1);
@Override
public boolean retry(IDataProviderMethod dataProvider) {
boolean status = counter.getAndIncrement() <= 2;
String clazz = dataProvider.getMethod().getDeclaringClass().getName();
String dataProviderMethodName = dataProvider.getMethod().getName();
String methodName = clazz + "." + dataProviderMethodName + "()";
System.err.println("Retry the data provider method " + methodName + " ? " + status);
return status;
Here’s how a test class that consumes this retry mechanism can look like:
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class RetriableDataProviderSample {
private boolean throwException = true;
@Test(dataProvider = "test-data")
public void sampleTestMethod(int input) {
System.err.println("Input value = " + input);
@DataProvider(retryUsing = RetryDataProvider.class, name = "test-data")
public Object[][] testDataSupplier() {
if (throwException) {
throwException = false;
System.err.println("Simulating a problem when invoking the data provider");
throw new IllegalStateException("Simulating a failure in data provider");
return new Object[][]{
{1}, {2}
};
And when you run this sample, the output would look something like below:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Simulating a problem when invoking the data provider
Retry the data provider method org.testng.demo.RetriableDataProviderSample.testDataSupplier() ? true
Input value = 1
Input value = 2
===============================================
Default Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
3.6.4. Controlling ThreadPool Usage
Starting from TestNG
7.9.0
, there are some additional ways in which the thread-pools that run the parallel tests can be controlled. For these new features to be consumed, update your suite file to use the
testng-1.1.dtd
(as seen below) so that your IDE can provide you with autocompletion:
share-thread-pool-for-data-providers
- When this attribute is set to true at the suite level, TestNG will start using a shared thread pool for all the data driven tests in a given
. The size of the thread pool is determined using the attribute
data-provider-thread-count
. This attribute has a default value of
false
use-global-thread-pool
- When this attribute is set to true at the suite level, TestNG will start using a common thread pool for running both your regular test methods and data driven test methods. The size of the thread pool is determined using the attribute thread-count. This attribute has a default value of
false
3.6.5. Parameters from System Properties
TestNG can be passed parameters on the command line of the Java Virtual Machine using system properties (
-D
). Parameters passed in this way are not required to be pre-defined in
testng.xml
, but will override any parameters defined there.
java -Dfirst-name=Cedrick -Dlast-name="von Braun" org.testng.TestNG testng.xml
The Java system property variable is a string with no spaces that represents the name of the property. The value variable is a string that represents the value of the property. If the value is a string with spaces, then enclose it in quotation marks.
In TestNG 6.x parameters defined in testng.xml could not be overwritten by system properties
Parameters in reports
Parameters used to invoke your test methods are shown in the HTML reports generated by TestNG. Here is an example:
3.7. Dependencies
Sometimes, you need your test methods to be invoked in a certain order. Here are a few examples:
To make sure a certain number of test methods have completed and succeeded before running more test methods.
To initialize your tests while wanting this initialization methods to be test methods as well (methods tagged with
@Before
@After
will not be part of the final report).
TestNG allows you to specify dependencies either with annotations or in XML.
3.7.1. Dependencies with annotations
You can use the attributes
dependsOnMethods
or
dependsOnGroups
, found on the
@Test
annotation.
There are two kinds of dependencies:
Hard dependencies
. All the methods you depend on must have run and succeeded for you to run. If at least one failure occurred in your dependencies, you will not be invoked and marked as a SKIP in the report.
Soft dependencies
. You will always be run after the methods you depend on, even if some of them have failed. This is useful when you just want to make sure that your test methods are run in a certain order but their success doesn’t really depend on the success of others. A soft dependency is obtained by adding
"alwaysRun=true"
in your
@Test
annotation.
Here is an example of a hard dependency:
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
In this example,
method1()
is declared as depending on method
serverStartedOk()
, which guarantees that
serverStartedOk()
will always be invoked first.
You can also have methods that depend on entire groups:
@Test(groups = { "init" })
public void serverStartedOk() {}
@Test(groups = { "init" })
public void initEnvironment() {}
@Test(dependsOnGroups = { "init.*" })
public void method1() {}
In this example,
method1()
is declared as depending on any group matching the regular expression
"init.*"
, which guarantees that the methods
serverStartedOk()
and
initEnvironment()
will always be invoked before
method1()
as stated before, the order of invocation for methods that belong in the same group is not guaranteed to be the same across test runs.
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.
Both
dependsOnGroups
and
dependsOnMethods
accept regular expressions as parameters. For
dependsOnMethods
, if you are depending on a method which happens to have several overloaded versions, all the overloaded methods will be invoked. If you only want to invoke one of the overloaded methods, you should use
dependsOnGroups
For a more advanced example of dependent methods, please refer to
this article
, which uses inheritance to provide an elegant solution to the problem of multiple dependencies.
By default, dependent methods are grouped by class.
For example, if method
b()
depends on method
a()
and you have several instances of the class that contains these methods (because of a factory of a data provider), then the invocation order will be as follows:
a(1)
a(2)
b(2)
b(2)
TestNG will not run
b()
until all the instances have invoked their
a()
method.
This behavior might not be desirable in certain scenarios, such as for example testing a sign in and sign out of a web browser for various countries. In such a case, you would like the following ordering:
signIn("us")
signOut("us")
signIn("uk")
signOut("uk")
For this ordering, you can use the XML attribute
group-by-instances
. This attribute is valid either on
or
or
3.7.2. Dependencies in XML
Alternatively, you can specify your group dependencies in the
testng.xml
file. You use the
tag to achieve this:
The
attribute contains a space-separated list of groups.
3.8. Factories
Factories allow you to create tests dynamically. For example, imagine you want to create a test method that will access a page on a website several times, and you want to invoke it with different values:
public class TestWebServer {
@Test(parameters = { "number-of-times" })
public void accessPage(int numberOfTimes) {
while (numberOfTimes-- > 0) {
// access the web page
This can become quickly impossible to manage, so instead, you should use a factory:
public class WebTestFactory {
@Factory
public Object[] createInstances() {
Object[] result = new Object[10];
for (int i = 0; i < 10; i++) {
result[i] = new WebTest(i * 10);
return result;
and the new test class is now:
public class WebTest {
private int m_numberOfTimes;
public WebTest(int numberOfTimes) {
m_numberOfTimes = numberOfTimes;
@Test
public void testServer() {
for (int i = 0; i < m_numberOfTimes; i++) {
// access the web page
Your
testng.xml
only needs to reference the class that contains the factory method, since the test instances themselves will be created at runtime:
Or, if building a test suite instance programmatically, you can add the factory in the same manner as for tests:
TestNG testNG = new TestNG();
testNG.setTestClasses(WebTestFactory.class);
testNG.run();
The factory method can receive parameters just like
@Test
and
@Before
@After
and it must return
Object[]
. The objects returned can be of any class (not necessarily the same class as the factory class) and they don’t even need to contain TestNG annotations (in which case they will be ignored by TestNG).
Factories can also be used with data providers, and you can leverage this functionality by putting the
@Factory
annotation either on a regular method or on a constructor. Here is an example of a constructor factory:
@Factory(dataProvider = "dp")
public FactoryDataProviderSampleTest(int n) {
super(n);
@DataProvider
static public Object[][] dp() {
return new Object[][] {
new Object[] { 41 },
new Object[] { 42 },
};
The example will make TestNG create two test classes, on with the constructor invoked with the value
41
and the other with
42
Optionally, you can specify indices for the data provider when using it with
@Factory
annotation.
When indices are specified, only the elements at the specified indices are passed as parameters for the factory annotated constructor.
@Factory(dataProvider = "dp", indices = {1, 3})
public ExampleTestCase(String text) {
this.i = i;
@DataProvider(name = "dp")
public static Object[] getData() {
return new Object[]{
"Java", "Kotlin", "Golang", "Rust"
};
In the above example, the values Kotlin (2nd element) and Rust (4th element) are passed as parameters to the
@Factory
annotated constructor and totally 2
ExampleTestCase
instances are created.
3.9. Class level annotations
The
@Test
annotation can be put on a class instead of a test method:
@Test
public class Test1 {
public void test1() {
public void test2() {
The effect of a class level @Test annotation is to make all the public methods of this class to become test methods even if they are not annotated. You can still repeat the
@Test
annotation on a method if you want to add certain attributes.
For example:
@Test
public class Test1 {
public void test1() {
@Test(groups = "g1")
public void test2() {
will make both
test1()
and
test2()
test methods but on top of that,
test2()
now belongs to the group "g1".
3.10. Ignoring tests
TestNG lets you ignore all the @Test methods :
In a class (or)
In a particular package (or)
In a package and all of its child packages
using the new annotation
@Ignore
When used at the method level
@Ignore
annotation is functionally equivalent to
@Test(enabled=false)
Here’s a sample that shows how to ignore all tests within a class.
import org.testng.annotations.Ignore;
import org.testng.annotations.Test;
@Ignore
public class TestcaseSample {
@Test
public void testMethod1() {
@Test
public void testMethod2() {
The
@Ignore
annotation has a higher priority than individual
@Test
method annotations. When
@Ignore
is placed on a class, all the tests in that class will be disabled.
To ignore all tests in a particular package, you just need to create
package-info.java
and add the
@Ignore
annotation to it. Here’s a sample :
@Ignore
package com.testng.master;
import org.testng.annotations.Ignore;
This causes all the
@Test
methods to be ignored in the package
com.testng.master
and all of its sub-packages.
3.11. Parallelism and time-outs
You can instruct TestNG to run your tests in separate threads in various ways.
3.11.1. Parallel suites
This is useful if you are running several suite files (e.g.
java org.testng.TestNG testng1.xml testng2.xml
) and you want each of these suites to be run in a separate thread. You can use the following command line flag to specify the size of a thread pool:
java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml
The corresponding ant task name is
suitethreadpoolsize
3.11.2. Parallel tests, classes and methods
The parallel attribute on the
tag can take one of following values:
parallel="methods"
: TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
parallel="tests"
: TestNG will run all the methods in the same
tag in the same thread, but each
tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same
and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
parallel="classes"
: TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
parallel="instances"
: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
Additionally, the attribute `thread-count` allows you to specify how many threads should be allocated for this execution.
the
@Test
attribute
timeOut
works in both parallel and non-parallel mode.
You can also specify that a
@Test
method should be invoked from different threads. You can use the attribute
threadPoolSize
to achieve this result:
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {
In this example, the function
testServer()
will be invoked ten times from three different threads. Additionally, a
time-out
of ten seconds guarantees that none of the threads will block on this thread forever.
3.12. Rerunning failed tests
Every time tests fail in a suite, TestNG creates a file called
testng-failed.xml
in the output directory.This XML file contains the necessary information to rerun only these methods that failed, allowing you to quickly reproduce the failures without having to run the entirety of your tests.Therefore, a typical session would look like this:
java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs testng.xml
java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs test-outputs\testng-failed.xml
Note that
testng-failed.xml
will contain all the necessary dependent methods so that you are guaranteed to run the methods that failed without any SKIP failures.
Sometimes, you might want TestNG to automatically retry a test whenever it fails. In those situations, you can use a retry analyzer.
When you bind a retry analyzer to a test, TestNG automatically invokes the retry analyzer to determine if TestNG can retry a test case again in an attempt to see if the test that just fails now passes. Here is how you use a retry analyzer:
Build an implementation of the interface
org.testng.IRetryAnalyzer
Bind this implementation to the
@Test
annotation for e.g.,
@Test(retryAnalyzer = LocalRetry.class)
Following is a sample implementation of the retry analyzer that retries a test for a maximum of three times.
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class MyRetry implements IRetryAnalyzer {
private int retryCount = 0;
private static final int maxRetryCount = 3;
@Override
public boolean retry(ITestResult result) {
if (retryCount < maxRetryCount) {
retryCount++;
return true;
return false;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestclassSample {
@Test(retryAnalyzer = MyRetry.class)
public void test2() {
Assert.fail();
3.13. JUnit tests
TestNG can run JUnit 3 and JUnit 4 tests. All you need to do is put the JUnit jar file on the classpath, specify your JUnit test classes in the
testng.classNames
property and set the
testng.junit
property to true:
The behavior of TestNG in this case is similar to JUnit depending on the JUnit version found on the class path:
JUnit 3:
All methods starting with test* in your classes will be run
If there is a method
setUp()
on your test class, it will be invoked before every test method
If there is a method
tearDown()
on your test class, it will be invoked before after every test method
If your test class contains a method
suite()
, all the tests returned by this method will be invoked
JUnit 4:
TestNG will use the
org.junit.runner.JUnitCore
runner to run your tests
3.14. BeanShell and advanced group selection
If the
and
tags in
testng.xml
are not enough for your needs, you can use a
BeanShell
expression to decide whether a certain test method should be included in a test run or not. You specify this expression just under the
tag:
When a