Running testng.xml using Command Prompt
Running testng.xml using command prompt will discuss about how to execute xml file from the command prompt. In our previous blogs we just created testng.xml file but did not verified it by running testng.xml file. In this blog will discuss how to run testng.xml configuration file. There are multiple ways of running the testng.xml file.
There are 2 ways to run the testng.xml file:
- Using Command Prompt
- Using IDE (i.e. Eclipse)
Using Command Prompt:
We can execute the testng.xml file through the command prompt. This way allows the use of multiple testng.xml files to execute simultaneously through TestNG. Before running a testng.xml suite through the command prompt, we need to compile our project code. The code compiled by Eclipse can be found under a folder named “bin” inside your Java project folder.
Below are the steps to run the testng.xml from command prompt:
1. Open the command prompt on your system.
2. Go to the Java project folder where the new testng.xml is created.
Note: To work with command prompt commands need to download testng.jar file and jcommander.jar files. Place these files in one separate folder (here we will place in libs folder under the project folder).
3. Now type the below command(s) in the command prompt. Here you have 2 ways to execute the testng.xml from command prompt.
a) First, we need to set the classpath from the command prompt. For this we need to mention the bin (all compiled files will be resides in this) folder path and required jar files folder.
set classpath=[Project Folder]\bin;[Project Folder]\libs\*
Example:
set classpath=D:\TestProject\bin;D:\TestProject\libs\*
Second, execute the testng.xml file from the command prompt.
java org.testng.TestNG testng.xml
b) Instead giving 2 different commands, we can combine and run as a single command to execute the testng.xml file.
Find the command below:
Here, we are in the project folder. So, no need to mention the project folder path explicitly.
java -cp “.\bin;.\libs\*” org.testng.TestNG testng.xml
In the above command we are adding the TestNG JAR and the project compiled code to the Java classpath by using the -cp option of Java.
4. In the both ways you can find the result screen as below
5. Here org.testng.TestNG consists of the main method that Java will use to execute the testng.xml file, which is passed as an argument at the command line.
6. After execution an HTML report is generated by TestNG in a folder named test-output under the same directory where you had run the command.
The following is the HTML test report generated by TestNG:
7. In case you would like to execute multiple testng.xml files, you can use the previous command by passing the other XML files as added arguments to the command line.
The following is a sample command:
java -cp “.\bin;.\libs\*” org.testng.TestNG testng.xml testng1.xml
And you can see the HTML report from the test-output folder.
8. TestNG also allows executing a particular test from the testng.xml file. To execute a particular test from the testng XML file, use the option -testnames at the command line with comma-separated names of tests that need to be executed. The following is a sample command:
java -cp “.\bin;.\libs\*” org.testng.TestNG -testnames “SampleTest” testng.xml
The preceding command will execute a test with the name SampleTest from testng.xml if it exists in the defined suite.
Below is the screenshot for the same:
Please watch the YouTube video for better understanding.