Site icon TestingDocs.com

Run tests from TestNG Suite testng.xml file

Overview

In this tutorial, we will learn to run tests from the TestNG Suite testng.xml. The default test suite in TestNG is testng.xml. We can create TestNG test suites using the XML files.  Let’s create sample TestNG test methods and run them using the TestNG testng.xml suite file.

Create testng.xml

Right click in the Package explorer >> New >> File.

Create a new testng.xml file to the project and add the test classes information.

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="First Suite" verbose="1" >
    <test name="My First TestNG Test" >
      <classes>
        <class name="com.testingdocs.testng.tutorial.SampleTestClass" >
          <methods>
            <include name="testMethod1"/>
          </methods>
        </class>
      </classes>
    </test>
    <test name="My Second TestNG Test" >
      <classes>
        <class name="com.testingdocs.testng.tutorial.SampleTestClass" >
          <methods>
            <include name="testMethod2"/>
          </methods>
        </class>
      </classes>
    </test>
</suite>

Run TestNG tests

To run the tests in Eclipse, right-click on the TestNG Suite file, choose

Run As >> TestNG Suite

 

TestNG test classes

package com.testingdocs.testng.tutorial;

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

//SampleTestClass.java
public class SampleTestClass {
  /**
   * @BeforeSuite method is executed before 
   * any tests in the TestNG suite.
   */
  @BeforeSuite
  public void beforeSuite(){
    System.out.println("In Before Suite method");
  }

  /**
   * @AfterSuite method gets executed after 
   * execution of all the tests in a TestNG suite.
   */
  @AfterSuite
  public void afterSuite(){
    System.out.println("In After Suite method");
  }

  /**
   * @BeforeTest method is executed before the first
   * test-method mentioned in each test inside the '<test>' 
   * tag in test TestNG suite.
    */
    @BeforeTest
    public void beforeTest(){
      System.out.println("In Before Test method");
    }

    /**
   * @AfterTest method gets executed after 
   * the last test-method
   */
  @AfterTest
  public void afterTest(){
    System.out.println("In After Test method");
  }

  /**
   * @BeforeClass method gets executed before 
   * any of the test-methods inside a test class.
   */
  @BeforeClass
  public void beforeClass(){
    System.out.println("In Before Class method");
  }

  /**
   * @AfterClass method gets executed after 
   * all of the test-methods inside a test class gets executed.
   */
  @AfterClass
  public void afterClass(){
    System.out.println("In After Class method");
  }

  /**
   * @BeforeGroups method is executed before executing any of
   * the tests belonging to the group in the 'groups'
   * attribute.
   * This method is executed before execution of the
   * test-method belonging to the group "groupOne".  
   */
  @BeforeGroups(groups={"groupOne"})
  public void beforeGroupOne(){
    System.out.println("In Before Group One Test method");
  }

  /**
   * @AfterGroups method is executed after executing all the
   * tests belonging to the group as mentioned in the 'groups'
   * attribute.
   * This method gets executed after execution of the
   * test-methods belonging to group "groupOne".
   */
  @AfterGroups(groups={"groupOne"})
  public void afterGroupOne(){
    System.out.println("In After Group One Test method");
  }

  /**
   * This method is executed before execution of the 
   * test-method belonging to group "groupTwo".  
   */
  @BeforeGroups(groups={"groupTwo"})
  public void beforeGroupTwo(){
    System.out.println("In Before Group Two Test method");
  }

  /**
   * The following method gets executed after execution of the 
   * test-methods belonging to group "groupTwo".
   */
  @AfterGroups(groups={"groupTwo"})
  public void afterGroupTwo(){
    System.out.println("In After Group Two Test method");
  }

  /**
   * @BeforeMethod gets executed before each test-method.
   */
  @BeforeMethod
  public void beforeMethod(){
    System.out.println("In Before Method");
  }

  /**
   * @AfterMethod gets executed after each test-method.
   */
  @AfterMethod
  public void afterMethod(){
    System.out.println("In After Method");
  }

  /**
   * @Test method belongs to the group "groupOne".
   */
  @Test(groups={"groupOne"})
  public void testMethod1(){
    System.out.println("In Test one method");
  }

  /**
   * @Test method belongs to the group "groupTwo".
   */  
  @Test(groups={"groupTwo"})
  public void testMethod2(){
    System.out.println("In Test two method");
  }
}

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Exit mobile version