TestNG how to get the Test suite name and conditionally skipping a Test method ?

Sameera De Silva
2 min readOct 3, 2019

--

Suppose I want to get TestNG suite.xml’s name not the actual xml file name (GetSuiteNameExample.xml)

<suite name=”SmokeTestSuite”>

based on that I can perform my actions in the scripts like if it’s running using a test plan and if the name is “SmokeTestSuite” , send email after the execution, and if the automation engineer runs the script directly so no need to send an email and skip that part .

If the class run without a plan the name will be “Default suite”

First let’s look and the test plan GetSuiteNameExample.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="SmokeTestSuite"><test name="Get suite name value of a Test suite "><classes><class name="com.webdriver.blogs.TestNgGetSuiteName"/></classes></test></suite>

Now let’s look at the code itself and it’s heavily commented with explanations so please follow it .

package com.webdriver.blogs;import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class TestNgGetSuiteName {
static WebDriver driver;
static String isEnabled;
Boolean isOfficalSuiteExection = false;
@Test(priority = 1, description = "Extract and Verify testNG suite name")
public void getAndVerifyTestNgSuiteName(ITestContext context) {
// Get current class name using Java
String currentClassName = this.getClass().getSimpleName();
// Get TestNG suite name
//To get suite xml file name
String suiteFileName = context.getCurrentXmlTest().getSuite().getFileName();

// If the class run without a plan the name will be "Default suite"
if (suiteName.contentEquals("Default suite")) {
System.out.println(currentClassName + " Test class is running without a test plan ");
} else {
System.out.println(currentClassName + "Test class ran using a plan and Suite name is " + suiteName);
// Set it to true so we can skip sendEmailsAfterTestSuiteexecution
isOfficalSuiteExection = Boolean.valueOf("true");
}
}@Test(priority = 2, description = "Send emails if it's running using a plan if not skip the sending email")
public void sendEmailsAfterTestSuiteexecution() {
if (isOfficalSuiteExection == true) {
System.out.println("Emails sent");
} else {
System.out.println("Skipping the email sending method hence it's not an official test");
// Skip execution by throwing an exception, you could do nothing here as well
// but this for demo purposes
throw new SkipException("Skipping this exception");
}
}
// Using this can get current Method
@AfterMethod(description = "Get and print method name")
public void getCurrentMethodName(ITestResult result) {
System.out.println("method name:" + result.getMethod().getMethodName());
}
}

Output when running using a Test suite xml

=====Name of the suite is : SmokeTestSuiteTestNgGetSuiteNameTest class ran using a plan  and Suite name is SmokeTestSuitemethod name:getAndVerifyTestNgSuiteNameEmails sentmethod name:sendEmailsAfterTestSuiteexecution

Output when directly run it as a TestNG class

=====Name of the suite is : Default suiteTestNgGetSuiteName Test class is running without a test planmethod name:getAndVerifyTestNgSuiteNameSkipping the email sending method hence it's not an official testmethod name:sendEmailsAfterTestSuiteexecutionPASSED: getAndVerifyTestNgSuiteName(org.testng.TestRunner@67784306)Extract and Verify testNG suite  nameSKIPPED: sendEmailsAfterTestSuiteexecutionSend emails if it's running  using a plan if not  skip the sending email

Free video link- https://youtu.be/yaYjvdNXqbQ

Contact me — https://www.linkedin.com/in/sameera-de-silva-11247922/

--

--

No responses yet