How to Execute TestNG.xml from command prompt?

There are various ways we can execute testing.xml file. We can run this from Eclipse itself. We can run it from command prompt, we can run it by creating .bat (batch) file, we can run it from any continuous integration tool.
Let us see here how can we run this command prompt.
Create sample java project.
 
Write some testing test in the project.
Create testing.xml file.
Sample test: (we will run this test from command prompt)

package runTestNGxmlFromCMD;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class RunTestNGTest {

@Test
public void test() throws InterruptedException{

WebDriver driver = new FirefoxDriver();

driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");

System.out.println("navigate to website");

Thread.sleep(3000); //sleep for 3 second so that you can observe that site is opened, before it closes quickly

driver.close();

}

}

Testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="runTestNGxmlFromCMD.RunTestNGTest"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
Now, my project structure looks like – 

Here is below commands to execute ‘testng.xml’ file from command line
Now,   my project location is:    C:UsersPrakashworkspaceRunTestNGXML
           Location of my jar files:  C:UsersPrakashworkspaceRunTestNGXMLlib*
      By default, class files will be created in project location (here – C:UsersPrakashworkspaceRunTestNGXMLbin)
So, based on above information, we need to run below piece of code in you command prompt.
cd C:UsersPrakashworkspaceRunTestNGXML

java -cp C:UsersPrakashworkspaceRunTestNGXMLlib*;C:UsersPrakashworkspaceRunTestNGXMLbin org.testng.TestNG testng.xml

Hope this helps !!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories