Thursday, 12 March 2020

TestNG: Run Test suite from xml file

In TestNG, you can group test classes in a <test> element and all the <test> elements can be grouped in a suite file.

suiteDemo.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="sanity test suite">

 <test name="Sanity test 1">
  <classes>
   <class name="com.sample.app.arithmetic.TestClass1"></class>
   <class name="com.sample.app.arithmetic.TestClass2"></class>
  </classes>
 </test>

 <test name="Sanity test 2">
  <classes>
   <class name="com.sample.app.arithmetic.TestClass3"></class>
   <class name="com.sample.app.arithmetic.TestClass4"></class>
  </classes>
 </test>

</suite>

Define ‘suiteDemo.xml’ file in the same package where your test classes exist.

As you see the xml, this test suite has two test elements. First test element groups the test classes TestClass1 and TestClass2. Second test element groups the test classes TestClass3 and TestClass4.

BaseTest.java
package com.sample.app.arithmetic;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseTest {

 @BeforeSuite
 public void beforeSuite_1() {
  System.out.println("Inside beforeSuite_1");
 }

 @BeforeSuite
 public void beforeSuite_2() {
  System.out.println("Inside beforeSuite_2\n");
 }

 @AfterSuite
 public void afterSuite_1() {
  System.out.println("\nInside afterSuite_1");
 }

 @AfterSuite
 public void afterSuite_2() {
  System.out.println("Inside afterSuite_2");
 }
}

TestClass1.java
package com.sample.app.arithmetic;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClass1 extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("********************************");
  System.out.println("TestClass1: Inside before class 1");
 }

 @BeforeClass
 public void beforeClass_2() {
  System.out.println("TestClass1: Inside before class 2");
 }

 @BeforeMethod
 public void beforeMethod_1() {
  System.out.println("\nTestClass1: Inside beforeMethod_1");
 }

 @BeforeMethod
 public void beforeMethod_2() {
  System.out.println("TestClass1: Inside beforeMethod_2");
 }

 @Test
 public void testCase1() {
  System.out.println("TestClass1: testCase1 executing");
 }

 @Test
 public void testCase2() {
  System.out.println("TestClass1: testCase2 executing");
 }

 @AfterMethod
 public void afterMethod_1() {
  System.out.println("TestClass1: Inside afterMethod_1");
 }

 @AfterMethod
 public void afterMethod_2() {
  System.out.println("TestClass1: Inside afterMethod_2");
 }

 @AfterClass
 public void afterClass_1() {
  System.out.println("\nTestClass1: Inside after class 1");
 }

 @AfterClass
 public void afterClass_2() {
  System.out.println("TestClass1: Inside after class 2");
 }

}

TestClass2.java
package com.sample.app.arithmetic;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClass2 extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("\n********************************");
  System.out.println("TestClass2: Inside before class 1");
 }

 @BeforeClass
 public void beforeClass_2() {
  System.out.println("TestClass2: Inside before class 2");
 }

 @BeforeMethod
 public void beforeMethod_1() {
  System.out.println("\nTestClass2: Inside beforeMethod_1");
 }

 @BeforeMethod
 public void beforeMethod_2() {
  System.out.println("TestClass2: Inside beforeMethod_2");
 }

 @Test
 public void testCase1() {
  System.out.println("TestClass2: testCase1 executing");
 }

 @Test
 public void testCase2() {
  System.out.println("TestClass2: testCase2 executing");
 }

 @AfterMethod
 public void afterMethod_1() {
  System.out.println("TestClass2: Inside afterMethod_1");
 }

 @AfterMethod
 public void afterMethod_2() {
  System.out.println("TestClass2: Inside afterMethod_2");
 }

 @AfterClass
 public void afterClass_1() {
  System.out.println("\nTestClass2: Inside after class 1");
 }

 @AfterClass
 public void afterClass_2() {
  System.out.println("TestClass2: Inside after class 2");
 }

}

TestClass3.java
package com.sample.app.arithmetic;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClass3 extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("\n********************************");
  System.out.println("TestClass3: Inside before class 1");
 }

 @BeforeClass
 public void beforeClass_2() {
  System.out.println("TestClass3: Inside before class 2");
 }

 @BeforeMethod
 public void beforeMethod_1() {
  System.out.println("\nTestClass3: Inside beforeMethod_1");
 }

 @BeforeMethod
 public void beforeMethod_2() {
  System.out.println("TestClass3: Inside beforeMethod_2");
 }

 @Test
 public void testCase1() {
  System.out.println("TestClass3: testCase1 executing");
 }

 @Test
 public void testCase2() {
  System.out.println("TestClass3: testCase2 executing");
 }

 @AfterMethod
 public void afterMethod_1() {
  System.out.println("TestClass3: Inside afterMethod_1");
 }

 @AfterMethod
 public void afterMethod_2() {
  System.out.println("TestClass3: Inside afterMethod_2");
 }

 @AfterClass
 public void afterClass_1() {
  System.out.println("\nTestClass3: Inside after class 1");
 }

 @AfterClass
 public void afterClass_2() {
  System.out.println("TestClass3: Inside after class 2");
 }

}

TestClass4.java
package com.sample.app.arithmetic;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClass4 extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("\n********************************");
  System.out.println("TestClass4: Inside before class 1");
 }

 @BeforeClass
 public void beforeClass_2() {
  System.out.println("TestClass4: Inside before class 2");
 }

 @BeforeMethod
 public void beforeMethod_1() {
  System.out.println("\nTestClass4: Inside beforeMethod_1");
 }

 @BeforeMethod
 public void beforeMethod_2() {
  System.out.println("TestClass4: Inside beforeMethod_2");
 }

 @Test
 public void testCase1() {
  System.out.println("TestClass4: testCase1 executing");
 }

 @Test
 public void testCase2() {
  System.out.println("TestClass4: testCase2 executing");
 }

 @AfterMethod
 public void afterMethod_1() {
  System.out.println("TestClass4: Inside afterMethod_1");
 }

 @AfterMethod
 public void afterMethod_2() {
  System.out.println("TestClass4: Inside afterMethod_2");
 }

 @AfterClass
 public void afterClass_1() {
  System.out.println("\nTestClass4: Inside after class 1");
 }

 @AfterClass
 public void afterClass_2() {
  System.out.println("TestClass4: Inside after class 2");
 }

}

How to run the test suite file?
Right click on the xml file.

Run As -> TestNG Suite.



You will get below messages in console.

Inside beforeSuite_1
Inside beforeSuite_2

********************************
TestClass1: Inside before class 1
TestClass1: Inside before class 2

TestClass1: Inside beforeMethod_1
TestClass1: Inside beforeMethod_2
TestClass1: testCase1 executing
TestClass1: Inside afterMethod_1
TestClass1: Inside afterMethod_2

TestClass1: Inside beforeMethod_1
TestClass1: Inside beforeMethod_2
TestClass1: testCase2 executing
TestClass1: Inside afterMethod_1
TestClass1: Inside afterMethod_2

TestClass1: Inside after class 1
TestClass1: Inside after class 2

********************************
TestClass2: Inside before class 1
TestClass2: Inside before class 2

TestClass2: Inside beforeMethod_1
TestClass2: Inside beforeMethod_2
TestClass2: testCase1 executing
TestClass2: Inside afterMethod_1
TestClass2: Inside afterMethod_2

TestClass2: Inside beforeMethod_1
TestClass2: Inside beforeMethod_2
TestClass2: testCase2 executing
TestClass2: Inside afterMethod_1
TestClass2: Inside afterMethod_2

TestClass2: Inside after class 1
TestClass2: Inside after class 2

********************************
TestClass3: Inside before class 1
TestClass3: Inside before class 2

TestClass3: Inside beforeMethod_1
TestClass3: Inside beforeMethod_2
TestClass3: testCase1 executing
TestClass3: Inside afterMethod_1
TestClass3: Inside afterMethod_2

TestClass3: Inside beforeMethod_1
TestClass3: Inside beforeMethod_2
TestClass3: testCase2 executing
TestClass3: Inside afterMethod_1
TestClass3: Inside afterMethod_2

TestClass3: Inside after class 1
TestClass3: Inside after class 2

********************************
TestClass4: Inside before class 1
TestClass4: Inside before class 2

TestClass4: Inside beforeMethod_1
TestClass4: Inside beforeMethod_2
TestClass4: testCase1 executing
TestClass4: Inside afterMethod_1
TestClass4: Inside afterMethod_2

TestClass4: Inside beforeMethod_1
TestClass4: Inside beforeMethod_2
TestClass4: testCase2 executing
TestClass4: Inside afterMethod_1
TestClass4: Inside afterMethod_2

TestClass4: Inside after class 1
TestClass4: Inside after class 2

Inside afterSuite_1
Inside afterSuite_2



Previous                                                    Next                                                    Home

No comments:

Post a Comment