Executing the Testcase using TestNG.xml file

Lets execute your testcases using testng.xml. This is really helpful when you would like to execute only desired testcases,browser compatibility / parallel testing.

Create a java class file name Testng.java

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class Testng {
// this is test case 1
public void testCase1() {
System.out.println(“in test case 1”);

// this is test case 2
public void testCase2() {
System.out.println(“in test case 2”);

public void beforeMethod() {
System.out.println(“in beforeMethod”);

public void afterMethod() {
System.out.println(“in afterMethod”);

public void beforeClass() {
System.out.println(“in beforeClass”);

public void afterClass() {
System.out.println(“in afterClass”);

public void beforeTest() {
System.out.println(“in beforeTest”);

public void afterTest() {
System.out.println(“in afterTest”);

public void beforeSuite() {
System.out.println(“in beforeSuite”);

public void afterSuite() {
System.out.println(“in afterSuite”);


Next, let’s create the file testng.xml. Right click on the project, New–> File, Create ‘testng.xml’.Click on finish.

You will see testng.xml under your project.
Now paste this code in that file

<suite name="Suite1">
  <test name="test1">
       <class name="Testng"/>

Now Run the xml file.You will see the report generated in the test-output folder.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s