user3718160
user3718160

Reputation: 491

How to run a testng .xml file from batch for a maven project

I have created a maven project and within I have several testng.xml files for my different suites.

If I run my .xml files from within my IDE (Intelij) everything goes fine

But what I want to do is create a batch file that run these .xml for me so I can run it without my IDE.

I've seen a solution when you have a lib folder etc like this : https://www.seleniumeasy.com/testng-tutorials/how-to-run-testng-xml-via-batch-file-example

But it doesn't works for me since I have a maven project so no lib or bin folder.

I've also tried this

SET SUITE_LOCATION=C:\Users\%USERNAME%\IdeaProjects\Test_Automation\src\main\java\MyApp\Scenarios\CustomStuff
SET CLASSPATH=C:\Users\%USERNAME%\.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;
java org.testng.TestNG %SUITE_LOCATION%\testng.xml

or

java -cp org.testng.TestNG %SUITE_LOCATION%\testng.xml

But It doesn't work. I also tried to add the .class files in the classpath but didn't work either.

Any idea on how to run it ?

Thank you!

UPDATE

I'm getting close to something I think this is the last command I tried :

java -classpath C:\Users\%USERNAME%\.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\%USERNAME%\.m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;%CLASSPATH% org.testng.TestNG -d test-outputs mytestsuite.xml

Error was :

[TestNG] [ERROR] Cannot find class in classpath: "myApp.Scenarios.Scenario1"

this is my xml file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="AndroidSuite" parallel="tests" thread-count="1">
<test name="SamsungS6-Scenario1">
    <parameter name="deviceUDID"  value="04157df40862d02f"/>
    <classes>
        <class name="myApp.Scenarios.Scenario1"/>
    </classes>
</test>

Upvotes: 3

Views: 8396

Answers (4)

java -classpath C:\Users\%USERNAME%.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\%USERNAME%.m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;%CLASSPATH% org.testng.TestNG -d test-outputs mytestsuite.xml

In this, your jar file is missing and hence the class not found exception.

java -classpath C:\Users\%USERNAME%.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\%USERNAME%.m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;%CLASSPATH%; org.testng.TestNG -d test-outputs mytestsuite.xml

Upvotes: 0

SOAlgorithm
SOAlgorithm

Reputation: 425

If you are using maven project and java 8, then you need to do several steps before you run your testng xml file from batch file:

  1. In your pom xml file, you need add a code that will force maven to use 1.8 for the compiler because the default one is 1.5

    <properties>
    
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    
    </properties>
    
  2. In your pom xml file, you need to add the code for the maven-surefire-plugin:

    <plugins>
    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                </suiteXmlFiles>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </plugin>
    
    </plugins>
    

  3. Save your pom.xml file and right click on your maven project and select maven->update project and check the check box for "force update of snapshots/releases" and click ok

  4. Now create a batch file with an extension bat and put this sample code in it and change it according to your project.

    set projectLocation=C:\locationOfYourProject\locationOfYourTestNGXmlFile

    cd %projectLocation%

    mvn clean test -DsuiteXmlFile=yourXMLFileCanBeAnyName.xml

Upvotes: 2

S.K. Venkat
S.K. Venkat

Reputation: 1807

If you are using maven, run a testsuite like this :

mvn clean test -DsuiteXmlFile=src/test/resources/api/TestngSuite.xml

Add this command to a batch file and execute it.

Upvotes: 3

manishgdev
manishgdev

Reputation: 148

Even after including the jcommander and the testng jar in the class path while executing we would be required to reach to a folder from where the main package of the application can be reached

  1. For example if the *Test.java files are present in com.test.app package then the folder structure for the same with respect to the Project will be src/test/java/com/test/app

so on executing

javac -cp ".:/home/manish/.m2/repository/org/testng/testng/6.9.8/testng-6.9.8.jar:/home/manish/.m2/repository/com/beust/jcommander/1.48/jcommander-1.48.jar" $(find -name *.java)

will compile the java files in their respective package

user will have to go to the test/java folder (if user is in project root folder)

cd src/test/java
javac -cp ".:/home/manish/.m2/repository/org/testng/testng/6.9.8/testng-6.9.8.jar:/home/manish/.m2/repository/com/beust/jcommander/1.48/jcommander-1.48.jar" org.testng.TestNg testng.xml_file_path_relative_to_current_location
  1. If the java files are compiled using the mvn compiler:testCompile

then the user will have to go to the target/test-classes folder

cd target/test-classes 
javac -cp ".:/home/manish/.m2/repository/org/testng/testng/6.9.8/testng-6.9.8.jar:/home/manish/.m2/repository/com/beust/jcommander/1.48/jcommander-1.48.jar" org.testng.TestNg testng.xml_file_path_relative_to_current_location

both the above ways are executing the tests

Upvotes: 0

Related Questions