In this post, we will run testng.xml using the command line. There are multiple scenarios where you want to run a testng.xml using the command-line tool. Maybe, you already made a maven project and want to run your testng.xml file using the command line now, or perhaps you are new to TestNG and just want to learn about the same. Don’t worry, we will be covering both of these scenarios in full.
Make a new java project and run testng.xml from the command line
Make a new Java Project
- Click on the File menu.
- Now, click on New and then click on the java project. We have named our project Practice. You can choose any name of your liking 😛
- Now, we will add the necessary jars to our project. We will push our code to our git repository, so you can always look for anything you need. If you need the jars we used, you can download them from our git repository. We have kept the required jars in the jars folder, as shown below.
- Right-click on the project > Build Path > Configure Build Path.
- Now, go to the Libraries tab, click on the Add External JARs and add all the jars that we kept in the jars folder earlier.
- As shown below, we will now make a package ( say Test ) under which we will make our class ( say CodekruTest ).
Make a simple test in the CodekruTest class.
package Test;
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void test() {
System.out.println("Executing the test in the CodekruTest class");
}
}
Make the testng.xml file.
Now we will make the testng.xml file to run our test class.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="codekru">
<test name="codekru">
<classes>
<class name="Test.CodekruTest" />
</classes>
</test>
</suite>
You can run this XML file from eclipse too by right-clicking on the XML file and then choosing Run As > TestNG Suite.
Run XML file using the command line
Now, we will run the testng.xml using the command line, and for that, you will need some things before –
- Your compiled classes classpath ( like for CodekruTest.java, where is my CodekruTest.class generated? ). We don’t have to worry about it as eclipse generates the compilable classes in the bin folder. So, we will be leaving it there only.
- Second, we will need the necessary jars to run our XML file. We did that, too, as we kept all of the required files in the jars folder earlier.
Now, go to your project path and run the below command in the command line tool to execute your testng.xml file.
java -cp bin\;jars\* org.testng.TestNG testng.xml
This will execute your testng.xml file as shown in the below screenshot.
Note: bin folder contains our .class compiled files but make sure never to use bin\* in the command as it will give you the below error.
[TestNG] [ERROR]
Cannot find class in classpath: Test.CodekruTest
The jars folder contains the necessary jars to execute the testng.xml file.
So, this is it. We have successfully made a new java project and ran our testng.xml using the command-line tool. Now, it’s time to see what we should do if we have an existing maven project and want to execute the testng.xml of that project?
Running the XML file of the existing maven project from the command line
There are two things that we need to look at here –
- The compiled classes of a maven project go into the target\classes folder instead of the bin folder if we use eclipse, which might vary for other IDEs. So, we need to give the path of the compiled classes folder in the command.
- We need to add the necessary jars to execute the testng.xml file. We can add the same jars that we have added earlier here in the jars folder
Now, the command to run the testng.xml file will be
java -cp target\classes\;jars\* org.testng.TestNG testng.xml
We hope you have liked this article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.