Automation Using Selenium Webdriver
Showing posts with label TestNG XML example to execute Multiple Classes. Show all posts
Showing posts with label TestNG XML example to execute Multiple Classes. Show all posts

Tuesday 22 November 2016

TestNG XML example to execute Multiple Classes

TestNG XML example to execute Multiple Classes

In testng.xml file we can specify multiple name (s) which needs to be executed.

In a project there may be many classes, but we want to execute only the selected classes.

We can pass class names of multiple packages also. If say suppose, we want to execute two classes
in one package and other class from some other package.

The below is the example testng.xml which will execute the class names that are specified.

Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="example suite 1" verbose="1" >
  <test name="Regression suite 1" >
    <classes>
      <class name="com.first.example.Demo1"/>
      <class name="com.first.example.Demo2"/>
      <class name="com.second.example.Demo3"/>
    </classes>
 </test>
</suite>
We need to specify the class names along with packages in between the classes tags.

In the above xml, we have specified class name as “com.first.example.” and “com.first.example.demoOne”
 which are in “com.first.example” package. And class name demoThree is in package “com.second.example.”

All the classes specified in the xml will get executes which have TestNG annotations.

Below are the two example classes under “com.first.example” package which is executed.

Package: “com.first.example”
Classname: Demo1
package com.first.example;

import org.testng.annotations.Test;

public class Demo1 {

@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoOne Class");
}

@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoOne Class");
}
}
Classname: Demo2
package com.first.example;

import org.testng.annotations.Test;

public class Demo2 {

@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoTwo Class");
}

@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoTwo Class");
}
}
Package: “com.second.example”
Classname: Demo3
package com.second.example;

import org.testng.annotations.Test;

public class Demo3 {

@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoThree Class");
}
@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoThree Class");
}
}
We need to run the testng.xml file. (Right click on testng.xml and select Run as ‘TestNG Suite”)
The below is the output for the above example code.