How to invoke/run TestNG tests and different ways to run tests are explained in this post. Following are the different ways to run/invoke TestNG.

  • testng.xml file
  • Ant task
  • From Command line
Run with a testng.xml file

Below is the format of testng.xml file.

  • DTD for testng.xml (Document type definition)  :  Defines the document structure with a list of legal elements and attributes

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >

  • Suite tag with name attribute, this name will be shown in results
  • Suite contains one or more <test> tags.
  • These test tags contains details about classes that have TestNG annotations.
  • <suite name="TestNG Sample">
      <test name="Sample">
    
      </test>
    	  
      <test name="BVT">
    
      </test>
    </sample>
    

 You know already as discussed in previous post ,  classes that have at least one TestNG annotations will be considered for execution. We can specify those classes in two ways.

  1.  Specify the class details directly inside <classes> tag.

  • <test name="Sample">
      <classes>
        <class name="com.exploreselenium.SampleTestngUsage"/>
        <class name="com.exploreselenium.AnnotationsTest"/>
      </classes>
    </test>
    

In the above testng.xml file, specified classes SampleTestngUsage & AnnotationsTest are inside the package com.exploreselenium

   2.  Specify the package name instead of class details

  • <test name="">
      <packages>
         <package name="com.exploreselenium"/>
      </packages>
    </test>
    

 In this, TestNG will run all the classes in the package ‘com.exploreselenium’ if the class has TestNG annotations.

3.  The classes used above are specified in previous post, to view the code, refer
#SampleTestngUsage.java, #AnnotationsTest.java

 To run tests with testng.xml file:

Right click on testng.xml (sample), select Run option in context menu.

Run using Ant task
Run from Command line
  • Before moving to the topic, to get a clear idea, below is the project structure

project structure to run testng tests

  • Make sure TestNG jar file path and .class file path set to CLASSPATH variable.
  • Command to set classpath
    • set CLASSPATH=G:\ExploreSelenium\target\test-classes;G:\ExploreSelenium\lib\testng-6.4.jar 
      • Project located in “ExploreSelenium” directory in G:\ drive
      • .class files in located “target” directory
      • testng.jar located in “lib” directory
  • Run below command to invoke TestNG
    • java org.testng.TestNG <testng.xml name>
      • Need to specify at least one xml file that has TestNG suite (sample.xml).
      • java org.testng.TestNG sample.xml  
  • If we didn’t set the classpath as mentioned above, we can specify classpath with -cp while invoke the TestNG, below are the details.
    • Move to directory where testng.xml located (  Suites  directory inside the Config  directory)
    • run the below command
      • java -cp  ../../target/test-classes;../../lib/*  org.testng.TestNG sample.xml
      • ..  means one step back from the current directory.
      • In the above ../../ means two steps back from current directory, that means control moves to root directory (ExploreSelenim) from Suites directory.
      • Specified both  .class files and testng.jar path separated with ; symbol to the -cp

Below is the output when executed from command line.

Invoke testng from command line

Summary:

We learned on executing the TestNG tests in 3 different ways using xml, ant task and command line.

TestNG Annotations and Execution sequence
Execute TestNG Tests From Ant Task
0