Invoke Testng.xml from command line
Invoke Testng.xml from command line
To invoke testng.xml
from the command line, you can use the java
command along with the TestNG JAR file.
You can execute testng.xml file in different ways.
- Eclipse IDE
- IntelliJ IDEA
- Ant / Maven build tool
- Jenkins CI
- Command Line
- Batch File.
In IDE you can just right click on testng.xml file and click on Run as ‘TestNG’ will invoke your tests.
✅ Pre-requisites
-
JDK must be installed and environment variables (
JAVA_HOME
,PATH
) must be configured. -
TestNG JAR should be downloaded (or available via Maven if in a project).
-
Your
testng.xml
and compiled.class
files (or.jar
) must be available.
Sample Project Structure
<strong>SampleTestNGProject</strong>/
│
├── <strong>bin</strong>/ --> Contains compiled .class files
│ └── com/example/TestClass.class
├── <strong>lib</strong>/ --> Contains TestNG and other required JARs
│ └── testng-7.9.0.jar
├── <strong>testng.xml</strong> --> TestNG suite file
testng.xml from command line
Now let us see how to invoke testng.xml from command line step by step:
- Create new project, In example we have created project ‘SampleTestNGProject‘
- Create new package. ‘com.testingdocs.sample’ as example package created here.
- Create sample classes which has @Test methods.
To execute ‘testng.xml’ file from command line:
Open command prompt and issue the following command:
/> java -cp <classpath> org.testng.TestNG testng.xml
This should execute the tests that we have specified in testng.xml file. In the command, for ‘java -cp‘ argument, we provide the classpath i.e. path to classes / libraries that our program requires to run.