Execute TestNg Using Command Prompt

Execute TestNg Using Command Prompt: In our previous post, we understood how to execute our selenium TestNG class using any editor like Eclipse. But running the TestNG classes using IDE is not only one way, but we can also execute our TestNG Class by the below methods:

  • Eclipse
  • IntelliJ
  • Ant / Maven – Using any continuous integration tool.
  • Command Line
  • Batch File – run it by creating .bat (batch) file

Previously, we have seen running a TestNG class using IDE, and we need to right-click on the testng.xml file and select run as TestNG, which invokes the test class. Now let us see how we can invoke the testng.xml file from the command line to run the test class step by step:

  • Open the IDE and Create a Java class inside the package.
  • Create a directory inside the project folder and store all the jar files. We have created a folder named “lib” and placed all the jar files required in any Java Selenium TestNG Class.
  • Write a Java Program and Save that.
  • Convert that program to TesNG by right-clicking on the class file, selecting testNG, and then clicking Convert to TestNG. In this way, we can prepare the testng.xml. When we call the testng.xml file, it executes the test methods inside the test class.
  • Open Command Prompt and go to the directory where your testng.xml file is present.

The syntax for TestNG using command prompt Like

Tell you about the syntax, and you have learned a few more things that will help you to understand how we prepare the syntax:

In the above Example:

  • My Project Location is: C:\Users\Admin\Desktop\ SeleniumTutorial\ workspace\SoftwareTestingo
  • Location Of All Jar Files: C:\Users\Admin\Desktop\ SeleniumTutorial\workspace\ SoftwareTestingo\lib\*
    When we compile our Java program, By Default, the .class file will be stored inside the project directory, in this case: C:\Users\Admin\Desktop\ SeleniumTutorial\workspace\ SoftwareTestingo\bin.

So, Based on the Above Information, we need to run below the code in your command prompt.

Go to the project directory using Command Prompt like this:

CD C:\Users\Admin\Desktop\ SeleniumTutorial\workspace\SoftwareTestingo and Hit Enter

Then The Final Command Looks something like below:

java -cp C:\Users\Admin\Desktop\SeleniumTutorial\workspace\SoftwareTestingo\lib\*; 
C:\Users\Admin\Desktop\SeleniumTutorial\workspace\SoftwareTestingo\bin org.testng.TestNG testng.xml

In the above command, for the ‘java -cp‘ argument, we provide the classpath, i.e., the path to classes/libraries that our program requires to run. This way, you can run the TestNG class without using any IDE. Similarly, we can run our TestNG class with the help of Batch File.

How to Run TestNG.xml File Using Batch File

Let’s See how we can execute the same Testng.XML File with the help of a batch file. A batch file (.bat) is used in the DOS and Windows operating system, an unformatted text consisting of a series of commands to be executed by the command-line interpreter.

Let us take the above Java File and TestNG.XMl File and then try to execute using a batch file.

How to Create a Batch File?

Note: Batch files are specific to Windows; if you want to run the test on a test platform that supports Java and TestNG, we recommend running your TestNG through Java instead of a batch file.

Steps to Create a Batch File

  • Open notepad
  • Mentioned the Project location like C:\Users\Admin\Desktop\SeleniumTutorial
  • Paste the code below:
set projectLocation=C:\Users\Admin\Desktop\SeleniumTutorial
cd %projectLocation%
set classpath=%projectLocation%\bin;%projectLocation%\lib\*
java org.testng.TestNG %projectLocation%\testng.xml
pause

Or you can use the method below also:

cd C:\Users\Admin\Desktop\SeleniumTutorial
java -cp C:\Users\Admin\Desktop\SeleniumTutorial\workspace\SoftwareTestingo\lib\*; 
C:\Users\Admin\Desktop\SeleniumTutorial\workspace\SoftwareTestingo\bin org.testng.TestNG testng.xml
pause
  • Save the file as ‘testNGBatchFile.bat’ where you want to save it.

In the end, we have mentioned “Pause,” which will prevent the auto-closing of the console after the execution is completed. To run the batch file, double-click on that so your test methods will get executed.

Note: Don’t add spaces; otherwise, it will not work.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment