TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

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

 

Tests TestNG Suite testng.xml

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

Related Posts

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

Before and After TestNG Annotations

TestNG /

TestNG Before and After Annotations

‹ Add the TestNG library to the project

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version