TestNG test scripts can be executed in multiple ways depending on the setup and the project structure. Below are the commonly used methods:
1. Running TestNG in a Programming Editor
In IDEs like IntelliJ IDEA, Eclipse, or NetBeans, TestNG test classes can be executed directly once the TestNG plugin is installed.
Run at Class Level:
You can run:
An individual test method, or
The entire test class by right-clicking the file and selecting:
Run [ClassName] as TestNG Test
Run via testng.xml:
You can also right-click the testng.xml file and select:
Run testng.xml
This will execute all tests defined within the XML suite configuration.
Running TestNG via Command Line
To run the TestNG test in the maven workspace through a command line, the surefire plugin is needed.\
What is the Surefire Plugin?
The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application.
Official Reference: Surefire Plugin Documentation
Run All Tests Using Maven Command
To execute all the tests in your Maven project (TestNG or JUnit):
This command automatically picks up any test classes or test suites defined in the project.
Run a Specific testng.xml Suite
We can execute the particular Testng.xml file through the suiteXmlFiles element of the surefire configuration in the Mavensurfire plugin.
We can include or exclude the test classes using different configurations. The plugin needs to be mentioned in the pom.xml
Execute with Maven Command:
This command will clean the previous builds and execute the tests defined in the specified testng.xml file.
Additional Features of Surefire Plugin
The Surefire plugin supports many useful configurations, such as:
Passing environment/system variables
Including or excluding specific test classes
Running tests in parallel
Setting test failure policies
Rerunning failed tests
These features can be controlled via configuration properties in the pom.xml.