TestNG: A Comprehensive Guide

April 05, 2026 · 27 min read · Testing Guide

HeadSpin Platform
Automated & amp; manual testing made easygoing through data science insights.
Differentiating capabilities:
  • Extensive end-to-end automation of QA process
  • Comparative analysis of app performance against equal
  • Uninterrupted monitoring of app execution using synthetic data for higher availability of apps
  • Easy-to-use developer friendly platform
cloudtest go
Affordable Real Device Testing for Emerging Teams
cloudtest go
Affordable Real Device Testing for Digital Enterprises
cloudtest go
The Ultimate Solution for a Powerful Blend of Functional & amp; Performance Testing!
cyol
TEM
New
Centralized peregrine tryout executing in cloud
cyol
Enhance Your Accessibility Testing With HeadSpin
cyol
Automate camera-based examination

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

retail

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

TestNG: A Comprehensive GuideTestNG: A Comprehensive Guide

TestNG: A Comprehensive Guide

Updated on
September 10, 2025
Updated on
September 10, 2025
 by 
Dheera KrishnanDheera Krishnan
Dheera Krishnan

TestNG is a robust examination framework that helps you write comprehensive tests with simplicity. It offers many features including annotations, argument injectant, and built-in support for group tests. In this guide, we & # x27; ll look at some of the key features of TestNG and how to use them to write better exam.

What is TestNG?

TestNG testing framework is one that proffer various advantages over other frameworks, such as JUnit and NUnit. TestNG is designed to be more flexible and extensible, and it provides features that make it easier to write and run exam. In this guide, we & # x27; ll look at some of the critical elements of TestNG and how to use them to write best tests.

Cedric Beust create TestNG in 2004. JUnit inspired him, and he wanted to create a more pliant and powerful testing framework. TestNG is an open-source project and is released under the Apache License 2.0.

TestNG is designed to be more flexible and extensible, and it provides feature that make it easier to publish and run tests.

Also check:

Why use TestNG?

TestNG is a powerful testing framework that offers various vantage over other frameworks, such as JUnit and NUnit. Some of the key benefits of TestNG include:

  • Flexibility and extensibility:TestNG is more flexible than former model, and it provides features that get it easier to It allows you run parallel execution of tests
  • Ease of use:TestNG is plan to be more user-friendly, as it defines addiction between one method on another (so if a failure occurs in the middle).
  • Comprehensive coverage:TestNG help you write better, more extensive tests. In TestNG, we can group our method together into grouping with specific precedency for each purpose - for example: high/medium level checking versus low-level ones like logging exception or regress true conditions.

Why was TestNG developed?

JUnit is a popular testing framework oftentimes expend in conjugation with TestNG. Although JUNit is a far aged fabric than TestNG, sure shortcomings of JUnit get TestNG arrive into the marketplace. In this section, we & # x27; ll appear at some of the key differences between TestNG and JUnit:

Annotations:TestNG uses annotating to identify tests, set priorities, and configure other aspects of how the tests should be run. JUnit also uses annotation, but the annotations have different names and function different purposes. For example, the @ Test notation in TestNG is used to place a test method, while the @ Test annotation in JUnit is used to configure how the test should be run.

Assertions:Assertions are used to verify that a specific condition is true. Both TestNG and JUnit support assertions, though TestNG volunteer additional assertion capabilities.

Parallel testing:TestNG indorse parallel testing, which allows you to run multiple tests simultaneously. JUnit does not indorse parallel testing.

Reporting:TestNG provides flexible coverage features to facilitate debug tests or provide information to stakeholder. JUnit also provides some reporting feature, but they are not as robust as the 1 in TestNG.

Data-driven examination:TestNG countenance you to easily parameterize your exam, which is helpful for data-driven testing. JUnit does not have built-in support for data-driven testing, but some third-party libraries can be used to accomplish this.

Also read:

How To Install TestNG in eclipse and Configure TestNG

Advantages of TestNG

TestNG has become one of the nigh wide used quiz frameworks for Java because it simplifies the test conception and execution process while offering features that go beyond traditional frameworks like JUnit. Some of the key advantages include:

  • Rich set of annotations: TestNG provide flexible annotations such as@ BeforeSuite, @ AfterTest, and @ DataProvider, which give precise control over the order and flow of test execution.
  • Parallel test execution: Running exam in parallel help teams save time and reduce build cycles, a critical factor for large test cortege.
  • Grouping and prioritization: With TestNG, testers can group tests into categories like smoke, regression, or functional, and assign execution priorities to ensure that the most important scenarios run first.
  • Data-driven testing: The built-in@ DataProviderget it simple to fulfill the same exam with multiple set of datum without duplicating code.
  • Detailed coverage: TestNG generates HTML and XML reports by default, making it easier for team to analyse results and identify issues quickly.
  • Integration support: It desegregate seamlessly with tools such as Maven, Gradle, Jenkins, Eclipse, and IntelliJ IDEA, fitting course into modern CI/CD grapevine.

Together, these features get TestNG a versatile pick for handling complex testing needs in both small and enterprise-scale projects.

Good Practices for Using TestNG

While TestNG volunteer potent features, using it effectively ask postdate certain best practices to ensure test remain maintainable, scalable, and reliable:

  • Keep exam autonomous: Write test methods that can run in isolation. Avoid excessive reliance on dependsOnMethods unless a true habituation exists.
  • Organize trial with groups: Define legitimate groups such as smoking, regression, or performance to simplify targeted examination execution.
  • Use datum providers wisely: Replace repetitive code with@ DataProviderto handle multiple data sets efficiently and hold tests clean.
  • Leverage parallel execution carefully: Ensure that your codification is thread-safe before enable parallel runs to prevent inconsistent results.
  • Adopt meaningful naming and priorities: Use descriptive method name and employ execution priorities only when necessary to maintain clarity.
  • Customize reporting: Extend or configure TestNG reports and listeners to enamor screenshots, logs, or metrics relevant to your project.
  • Integrate with flesh and CI instrument: Always relate TestNG with Maven, Gradle, or Jenkins to automate execution and reporting as part of your CI/CD workflow.

By applying these practices, teams can maximize the value of TestNG, keep their test suites stable, and accelerate the overall testing procedure.

This subdivision covers all the details regarding how to install and configure TestNG on your eclipse IDE. To make sure your tests are designed to run expeditiously, follow the steps below to set up TestNG on your machine:

Install TestNG

TestNG is typically added to a Java undertaking via Maven/Gradle or Eclipse Marketplace.

Add TestNG to your project

To add TestNG to your projection, you can add the following dependency to your pom.xml file:

& lt; dependency & gt; & lt; groupId & gt; org.testng & lt; /groupId & gt; & lt; artifactId & gt; testng & lt; /artifactId & gt; & lt; version & gt; 6.12.3 & lt; /version & gt; & lt; /dependency & gt;

Configure TestNG in your project

To run examination with TestNG, you need to configure it in your projection. You can do this by adding the following XML to your pom.xml file:

& lt; build & gt; & lt; plugins & gt; & lt; plugin & gt; & lt; groupId & gt; org.apache.maven.plugins & lt; /groupId & gt; & lt; artifactId & gt; maven-surefire-plugin & lt; /artifactId & gt; & lt; version > 2.19 & lt; /version & gt; & lt; configuration & gt; & lt; suiteXmlFiles & gt; & lt; suiteXmlFile/ & gt; & lt; /suiteXmlFiles > & lt; /configuration & gt; & lt; /plugin & gt; & lt; /plugins & gt; & lt; /build & gt;

Run TestNG tests

Once you have configured TestNG in your projection, you can run your tests by using the following command:


mvn test

This will run all of the tests in your project. You can also use the following command to run a specific examination:

mvn -Dtest=MyTest test

Key Features of TestNG Testing Framework

Some of the key features of TestNG include:

Annotations: TestNG uses annotations to place exam, set priorities, and configure other aspects of how the tests should be run.

Parameter injectant: TestNG allows you to shoot parameters into your tests, which can be used to configure the tryout or provide datum for the exam.

Built-in support for grouping exam: With TestNG, you can group tryout, which can be helpful if you want to run a subset of exam.

Elastic coverage:TestNG provide pliable reportage features, which can help debug examination or provide info to stakeholder.

Parallel essay in TestNG:Parallel testing in TestNG is well-supported, which can help you preserve time when running tests.

Data-driven examination:TestNG allows you to easy parameterize your examination, which can be helpful for data-driven testing.

TestNG Eclipse plugin:The TestNG Eclipse plugin makes it easy to create and run TestNG tests from within Eclipse.

Also check out:

How to use TestNG Framework

Now that we & # x27; ve taken a look at some of the key features of TestNG let & # x27; s see how we can use it to write best exam. This subdivision will appear at how to use annotations, parameter, and groups in TestNG.

Annotations

Annotation is a powerful lineament of testing that allows you to specify how your test should be run. Annotations can be used to configure your test, set up dependencies, and group your tests. You can get your tests more modular and easier to maintain by using annotations.

TestNG uses notation to name tryout, set priorities, and configure other prospect of how the trial should be run. This section will look at some of the most commonly use TestNG annotation.

@ BeforeMethod

The @ BeforeMethod annotation denotes a method that should be run before each test. This can be utilise to set up the environs for the test or perform any other actions that need to be done before the test is run.

public vacancy setup () {// do something}

@ AfterMethod

The @ AfterMethod annotation denote a method that should be run after each test. This can be used to houseclean up the environment after the test is run or perform any early action that need to be done after the test is complete.

public nihility tearDown () {// do something}

@Test 

The @ Test annotation is used to denote a test method. This method will be fulfil when the examination is run.

public void myTest () {// do something}

Attributes Used With Annotations In TestNG Framework

The following are some of the attributes that can be utilize with annotating in TestNG:

  • Priority: This attribute is used to set the priority of a test. The high the priority, the sooner the trial will be run.
  • timeout: This attribute is used to set the amount of time (in milliseconds) that should be permit for the examination to run. If the examination lead long than this, it will be abort.
  • dependsOnMethods:This attribute specifies which methods the current method depends on. If one of the dependent method fails, the current process will not be run.
  • groups:This attribute specifies which groups the current method belongs to. This can be helpful if you desire to run a subset of tests.
  • dataProvider:This attribute specifies a data provider for the test. This can be used to provide data for the test from various origin.

Parameters

TestNG allows you to shoot parameters into your tests, which can be habituate to configure the trial or provide datum for the test. In this subdivision, we & # x27; ll look at using argument in TestNG.

Configuring parameters in TestNG

You can configure parameters in TestNG by utilize the & lt; parameter & gt; tag in your testng.xml file. For example, if you wanted to shoot a parameter name & quot; foo & quot; with the value & quot; bar, & quot; you would use the following XML:

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

& lt; exam & gt; & lt; parameters & gt; & lt; parameter name= '' foo '' value= '' bar '' / & gt; & lt; /parameters & gt; & lt; stratum & gt; // your test category here & lt; /classes & gt; & lt; /test & gt;

Injecting Parameters into Tests

Parameter injection is another valuable feature of TestNG that allows you to inject parameter into your tests. This can be used to provide data to your tests or to configure how your tests are run. Parameter shot is a potent feature that can make your test more flexible and easier to maintain.

Once you have configure parameters in TestNG, you can shoot them into your test by using the @ Parameters annotation. For example, if you wanted to inject a parameter named & quot; foo & quot; into a test method, you would use the next annotation:

public nothingness myTest (String foo) {// do something with the foo argument}

Grouping Tests

TestNG likewise has built-in support for grouping tests. This permit you to group your trial and run them as a unit. This can be useful for mastermind your tests or running a subset of your examination. TestNG also allows you to exclude certain groups of tryout from being run.

This subdivision will look at how to use grouping in TestNG.

Configuring Groups in TestNG

You can configure groups in TestNG by using the & lt; grouping & gt; tag in your testng.xml file. For example, if you wanted to create a grouping named & quot; smoke-tests, & quot; you would use the following XML:

& lt; trial & gt; & lt; groups & gt; & lt; group name= '' smoke-tests '' / & gt; & lt; /groups & gt; & lt; classes & gt; // your examination class here & lt; /classes & gt; & lt; /test & gt;

Running Tests in Groups

Once you have configured groups in TestNG, you can run tests in those groups by habituate the -groups command-line selection. For representative, if you wanted to run all tests in the & quot; smoke-tests & quot; group, you would use the next command:

java org.testng.TestNG -groups smoke-tests testng.xml

You can also use the groups property of the & lt; stratum & gt; tag to stipulate which groups a test class belongs to. For exemplar, if you require to make sure that a test family belong to the & quot; smoke-tests & quot; group, you would use the following XML:

& lt; test & gt; & lt; classes & gt; & lt; class name= '' MyTestClass '' groups= '' smoke-tests '' / & gt; & lt; /classes & gt; & lt; /test & gt;

Create a tryout suite with TestNG

To make a test suite with TestNG, you must create a testng.xml file. This file contains the configuration for your exam suite. Once you have made the file, you can run your tests expend the java org.testng.TestNG command-line option. For example, if you want to run all the tests in your test entourage, you would use the following command:

java org.testng.TestNG testng.xml

You can also use the -suitename choice to specify the name of your examination retinue. This can be utile if you have multiple test suites and you want to run them all at once. For example, if you have a test suite named & quot; smoke-tests & quot; and another name & quot; regression-tests, & quot; you could run both suites by using the next command:

java org.testng.TestNG -suitename SmokeTests, RegressionTests testng.xml

TestNG likewise allows you to limit how your tests will be run. This can be useful if you have dependencies between your tests. For example, if you experience a test that needs to run before another test, you can specify that by using the dependsOnMethods attribute of the @ Test annotation. For example:

public void myFirstTest () {// do something} @ Test (dependsOnMethods= '' myFirstTest '') public void mySecondTest () {// do something else}

How to run tests with TestNG

To run your tests, you can use the java org.testng.TestNG command-line option. This will run all of your trial in the order limit in your testng.xml file. You can too use the -Dtestng.xml option to select a different XML file. For instance, if you need to run all the tests in your & quot; smoke-tests & quot; suite, you would use the undermentioned dictation:

java org.testng.TestNG -Dtestng.xml=smoke-tests.xml testng.xml

You can also use the -suitename option to specify the name of your trial suite. This can be utile if you hold multiple tryout suites and you want to run them all at once. For illustration, if you have a exam suite call & quot; smoke-tests & quot; and another called & quot; regression-tests, & quot; you could run both suites by use the following bidding:

java org.testng.TestNG -suitename SmokeTests, RegressionTests testng.xml

If you want to run a specific exam or radical of tests, you can use the -test names or -groups of options. For example, if you wanted to run the & quot; myFirstTest & quot; and & quot; mySecondTest & quot; tests, you would use the following command:

java org.testng.TestNG -testnames myFirstTest, mySecondTest testng.xml

If you want to run all the tryout in a specific group, you can use the -groupsof option. For model, if you wanted to run all the test in the & quot; smoke-tests & quot; grouping, you would use the next bid:

java org.testng.TestNG -groupsof smoke-tests testng.xml

You can also use the groups attribute of the & lt; class & gt; tag to specify which radical a test class belongs to. For example, if you desire to make sure that a test class belonged to the & quot; smoke-tests & quot; grouping, you would use the following XML:

& lt; test & gt; & lt; classes & gt; & lt; class name= '' MyTestClass '' groups= '' smoke-tests '' / & gt; & lt; /classes & gt; & lt; /test & gt;

Creating a exam entourage with TestNG is leisurely. You need to make a testng.xml file and fix the configuration for your exam. Once you have completed the file, you can run your examination apply the java org.testng.TestNG command-line selection. TestNG besides allows you to specify the order in which your tests will be run and how they will be grouped. This do it easy to create comprehensive trial suites that can be efficiently run and maintained.

Using group and dependencies in TestNG

TestNG allows you to set how your tryout will be run. This can be utilitarian if you have colony between your test. For example, if you have a test that needs to run before another trial, you can specify that by apply the dependsOnMethods attribute of the @ Test annotation. For model:

public vacancy myFirstTest () {// do something} @ Test (dependsOnMethods= '' myFirstTest '') public void mySecondTest () {// do something else}

This countenance you to easily make comprehensive test suites that can efficiently run and maintain. TestNG also will enable you to grouping your examination. This can be useful if you need to run a specific set of tests. For example, if you have a suite of Smoke Tests and Regression Tests, you can group your tests accordingly and run them separately.

To radical your test, you can use the group attribute of the & lt; class & gt; tag. For example:

& lt; exam & gt; & lt; classes & gt; & lt; class name= '' MyTestClass '' groups= '' smoke-tests '' / & gt; & lt; /classes & gt; & lt; /test & gt;

This will ensure that only the tests in the & quot; smoke-tests & quot; group are run. Using a comma-separated inclination, you can also specify that a test class belongs to multiple groups. For example:

& lt; test & gt; & lt; stratum & gt; & lt; stratum name= '' MyTestClass '' groups= '' smoke-tests, regression-tests '' / & gt; & lt; /classes & gt; & lt; /test & gt;

This will ensure that the tests in both the & quot; smoke-tests & quot; and & quot; regression-tests & quot; groups are run. TestNG also allows you to shut tests from a group. This can be utilitarian if you want to run all the examination in a group except for a few.

To exclude tests from a group, you can use the excludeMethods attribute of the @ Test annotation. For exemplar:

public nullity myFirstTest () {// do something} @ Test (excludeMethods= '' myFirstTest '') public void mySecondTest () {// do something else}

This will create sure that only the & quot; mySecondTest & quot; is run. You can also use the excludeGroups attribute of the & lt; class & gt; tag to shut a radical of tryout from running. For example:

& lt; test & gt; & lt; classes & gt; & lt; class name= '' MyTestClass '' excludeGroups= '' smoke-tests '' / & gt; & lt; /classes & gt; & lt; /test & gt;

This will ensure that all the tests in the & quot; smoke-tests & quot; group are except from running. TestNG also permit you to specify how your tests will be run. This can be useful if you have dependencies between your tests.

For example, if you hold a tryout that take to run before another test, you can specify that by using the dependsOnMethods attribute of the @ Test annotation. For example:

public void myFirstTest () {// do something} @ Test (dependsOnMethods= '' myFirstTest '') public void mySecondTest () {// do something else}

You can besides use the dependsOnGroups impute to specify a grouping of tests that need to be run before another group. For example:

public void myFirstTest () {// do something} @ Test (dependsOnGroups= '' myFirstTest '') public void mySecondTest () {// do something else}

This will check that the & quot; myFirstTest & quot; group is run before the & quot; mySecondTest & quot; group. You can also use the sequential attribute of the & lt; class & gt; tag to specify that the tests in a class need to be run in a specific order. For exemplar:

& lt; trial & gt; & lt; classes & gt; & lt; class name= '' MyTestClass '' sequential= '' true '' / & gt; & lt; /classes & gt; & lt; /test & gt;

This will ensure that the tests in the & quot; MyTestClass & quot; are run in the order they are defined. TestNG also allow you to specify the turn of times a trial should be run. This can be useful if you desire to make certain that a test is stable before releasing it.

For example, if you want to run a test 10 times, you can use the invocationCount attribute of the @ Test annotation. For example:

public void myFirstTest () {// do something} @ Test (invocationCount=10) public nihility mySecondTest () {// do something else}

This will make sure that the & quot; mySecondTest & quot; is run ten times. You can besides use the threadPoolSize property to specify how many threads should be used to run your tests. For instance:

& lt; test & gt; & lt; classes & gt; & lt; form name= '' MyTestClass '' threadPoolSize= '' 5 '' / & gt; & lt; /classes & gt; & lt; /test & gt;

This will ensure that the exam in the & quot; MyTestClass & quot; are run using five yarn. TestNG also allows you to specify the amount of time a test should take. This can be utilitarian if you want to ensure that a test is conduct the expected sum of clip.

For example, if you want a test to finish within 10 bit, you can use the invocationTimeOut attribute of the @ Test annotating. For instance:

public void myFirstTest () {// do something} @ Test (invocationTimeOut=10000) public void mySecondTest () {// do something else}

This will ensure that the & quot; mySecondTest & quot; is given 10 seconds to stop before it is considered a failure. TestNG also allow you to specify the quantity of time a examination should await for another test to dispatch. This can be utilitarian if you feature tests dependent on each other.

For example, if you want a test to expect for another exam to finish before it start, you can use the dependsOnMethods attribute of the @ Test annotation. For example:

public void myFirstTest () {// do something} @ Test (dependsOnMethods= '' myFirstTest '', alwaysRun=true) public void mySecondTest () {// do something else}

This will ensure that the & quot; mySecondTest & quot; is always run after the & quot; myFirstTest & quot; trial has end. TestNG also allows you to delimit the measure of time a test should wait for another test to finish. This can be useful if you have tests dependent on each former.

For example, if you require a trial to wait for another examination to terminate before it starts, you can use the dependsOnGroups attribute of the @ Test annotation. For example:

public nullity myFirstTest () {// do something} @ Test (dependsOnGroups= '' myFirstTest '') public void mySecondTest () {// do something else}

This will assure that the & quot; mySecondTest & quot; is run after the & quot; myFirstTest & quot; group has cease. You can too use the sequential property of the & lt; category & gt; tag to specify that the tests in a category motive to be run in a specific order. For example:

& lt; test & gt; & lt; classes & gt; & lt; class name= '' MyTestClass '' sequential= '' true '' / & gt; & lt; /classes & gt; & lt; /test & gt;

How to debug test with TestNG

Debugging tests with TestNG is a little different than debugging exam with JUnit. You can use the fail () method to stop a test and publish the plenty trace with JUnit. With TestNG, you can use the fail (String message) method to block a test and print the substance. For example:

public vacuum myFirstTest () {// do something} @ Test (invocationCount=10) public void mySecondTest () {// do something else}

The & quot; mySecondTest & quot; will run ten times, but the & quot; myFirstTest & quot; will only run formerly. To debug the & quot; myFirstTest, & quot; you can use the fail (String substance) method to stop the test and print the message. For representative:

public void myFirstTest () {fail (`` myFirstTest '');} @ Test (invocationCount=10) public void mySecondTest () {// do something else}

This will ensure that the & quot; myFirstTest & quot; is only run erstwhile and that the & quot; mySecondTest & quot; is ne'er run. You can also use the skipFailedInvocations attribute of the & lt; class & gt; tag to specify that tests should be hop-skip if they fail. For example:

& lt; test & gt; & lt; classes & gt; & lt; class name= '' MyTestClass '' skipFailedInvocations= '' true '' / & gt; & lt; /classes & gt; & lt; /test & gt;

This will check that the & quot; mySecondTest & quot; is just run if the & quot; myFirstTest & quot; passes. If the & quot; myFirstTest & quot; fails, the & quot; mySecondTest & quot; will be skipped. You can also use the skipFailedInvocations dimension of the & lt; class & gt; tag to stipulate that tests should be omitted if they fail. For model:

& lt; test & gt; & lt; classes & gt; & lt; form name= '' MyTestClass '' skipFailedInvocations= '' true '' / & gt; & lt; /classes & gt; & lt; /test & gt;

Important points to retrieve while habituate TestNG

Here are some things to keep in mind while using TestNG for getting the good out of the model:

  • Use the annotation @ DataProvider to make test data for your tests. This annotation will help you to pen more comprehensive and robust tests.
  • Use the annotation @ Parameters to inject parameters into your trial. This annotation can inject values from a constellation file or the command line into your tests.
  • Use the groups attribute of the & lt; stratum & gt; tag to specify that a tryout class should be run in a specific order. This can be helpful when you have tryout that are dependent on each early.
  • Use the dependsOnMethods attribute of the @ Test annotating to specify that a exam should wait for another trial to finish before it starts. This can be useful when you have test dependent on each other.
  • Use the sequential attribute of the & lt; class & gt; tag to specify that the tests in a class need to be run in a specific order. This can be helpful when you have tests dependent on each early.
  • Use the skipFailedInvocations attribute of the & lt; class & gt; tag to delimit that tests should be skipped if they fail. This can be helpful when you have tests dependent on each other.
  • Use the annotation @ Factory to create trial instances dynamically. This notation can be utilise to develop tests that are not know at compile time.
  • Use the annotation @ Listeners to add listeners to your tests. This notation can be used to add custom behavior to your trial.
  • Use the annotation @ Test (invocationCount=n) to run a trial multiple clip. This can be useful for tests that need to be run multiple times.

The Bottom Line

TestNG is a framework that has sure advantages over JUnit. It allows us to create easy-to understand HTML reports which are generated automatically, making it easygoing for developers with circumscribed technological knowledge of program languages like Java or C # as well nonprofessional ones who may be just starting out in this battlefield. It is perfect for all the developers and testers who still want control over what they & # x27; re doing when testing their apps on different devices from across various variation. If you haven ’ t tried TestNG yet, you should as it is one of the better testing Frameworks out there.

TestNG Frequently Asked Question (FAQs)

Q: What is TestNG?

A:TestNG is a powerful testing fabric that aid you write better, more comprehensive tests. It offers many features that make it easier to write and run tests, including annotating, parameter shot, and built-in support for grouping tests.

Q: What are the critical differences between TestNG and Selenium?

A:TestNG is a racy testing framework that help you write better, more comprehensive tests. It offers many features that make it easy to write and run examination, include annotations, parameter shot, and built-in support for grouping tests. Selenium is a web automation tool that can automate web browser interaction.

Q: How can TestNG be used to write better tryout?

A:TestNG can be utilize to write better test by render features such as annotations, parameter injection, and built-in support for grouping tryout. These features can help you indite more comprehensive and practical exam.

Q: What is the difference between TestNG and JUnit?

A:TestNG and JUnit are both testing model that can be expend to write and run tests. However, there are some critical differences between these two frameworks. TestNG uses annotations to name tests, set anteriority, and configure other aspects of how the tests should be run. JUnit also uses annotations, but the notation experience different names and serve different use.

Q: Which method can be tested by TestNG?

A:TestNG can test public, private, and protected methods. It can also be used to test inactive and non-static method.

Q: What is the postcondition in TestNG?

A:A postcondition is a status that must be met after a test has run. In TestNG, you can use the assertPostcondition () method to control that a postcondition has been completed.

Q: What is the sequence of execution of the annotations in TestNG?

A: The episode of execution of the annotations in TestNG is as follows:

1) BeforeSuite

2) BeforeTest

3) BeforeClass

4) BeforeMethod

5) Test

6) AfterMethod

7) AfterClass

8) AfterTest

9) AfterSuite

Dheera Krishnan

Dheera Krishnan is a Software Engineer and Customer Success professional at HeadSpin specialise in software testing, mobile performance, and lineament technology. She impart hands-on expertise in automation, DevOps testing, and peregrine validation to help teams ameliorate testing strategies and deliver unlined digital experiences.

LinkedIn
Author & # x27; s Profile

Piali Mazumdar

Lead, Content Marketing, HeadSpin Inc.

Piali is a dynamic and results-driven Content Marketing Specialist with 8+ geezerhood of experience in crafting engaging narratives and marketing collateral across divers industries. She excels in collaborating with cross-functional teams to develop innovative substance strategy and deliver compelling, unquestionable, and impactful content that resonates with target audiences and enhances brand authenticity.

LinkedIn

TestNG: A Comprehensive Guide

4 Parts

regression intelligence blog
-

Regression Intelligence hard-nosed guide for modern users (Part 3)

Coming Soon
Regression Intelligence practical guide for advanced users
-

Regression Intelligence hardheaded guide for advanced users (Part 4)

Coming Soon

Discover how HeadSpin can empower your occupation with superior examine capabilities

Our Platform enables you to:
accelerate time-to-market
Accelerate time-to-market, gaining a competitory edge
faster development cycles
Boost developer/QA productivity with quicker development cycles
automated buil-over-build regression testing
Automate build-over-build regression testing for consistent solvent
gain better visibility into functional & performance issues
Gain better visibility into functional and execution issues
reduce mean time
Reduce mean time to identify/resolve during examination, QA, and production
evaluate audio, video & qoe
Evaluate sound, video, and content character of experience (QoE) effortlessly
The trusted choice for global endeavour
Adobe
Hargreaves Lansdown
Truecaller
Crazylabs
Nedbank
Numeracle
Veryon
Close

Discover how HeadSpin can empower your business with superior prove potentiality

Our Platform enables you to:
accelerate time-to-market
Accelerate time-to-market, benefit a competitory edge
faster development cycles
Boost developer/QA productiveness with faster development cycles
automated buil-over-build regression testing
Automate build-over-build regression prove for consistent results
gain better visibility into functional & performance issues
Gain best visibility into functional and performance issue
reduce mean time
Reduce mean time to identify/resolve during examination, QA, and production
evaluate audio, video & qoe
Evaluate audio, video, and content quality of experience (QoE) effortlessly
The trusted pick for global enterprises
Close

Discover how HeadSpin can invest your job with superior examine capabilities

Our Platform enables you to:
accelerate time-to-market
Accelerate time-to-market, gaining a militant edge
faster development cycles
Boost developer/QA productiveness with quicker development cycles
automated buil-over-build regression testing
Automate build-over-build regression examine for consistent results
gain better visibility into functional & performance issues
Gain better visibility into functional and performance subject
reduce mean time
Reduce mean time to identify/resolve during test, QA, and production
evaluate audio, video & qoe
Evaluate audio, video, and content quality of experience (QoE) effortlessly
The trusted choice for global enterprises
Close

Connet Now

Wipro LogoVMLYR Logo
Close
Book a Meeting
Products
footer down arrow
Solutions
footer down arrow
Industries
footer down arrow
Features
footer down arrow
Support
footer down arrow
Resource Center
footer down arrow
Why Choose HeadSpin?
footer down arrow
Copyright © 2026 HeadSpin, Inc. All Rights Reserved.

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free