Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Suite Name at Runtime in TestNG

Tags:

testng

How can I get the current running suite name at run time in my test case? I am using the piece of code shown below for getting the current suite name.

Listeners class:

public class SuiteListener implements ISuiteListener{

private static ThreadLocal<ISuite> ACCESS = new ThreadLocal<ISuite>();

public static ISuite getAccess() {
         return ACCESS.get();
    }

@Override
public void onFinish(ISuite suite) {
    ACCESS.set(null);
}

@Override
public void onStart(ISuite arg0) {
    ACCESS.set(arg0);
}

 }

Test Class:

    @Listeners({ SuiteListener.class })
public class Practise {

    @DataProvider(name = "getXlsPath")
    public Object[][] createData() throws Exception {
        String [][] testCasesPaths=null;
        ISuite suiteListner = SuiteListener.getAccess();
        String runningSuite=suiteListner.getName();
        System.out.println(runningSuite);
        testCasesPaths[0][0]="1.xls";
        testCasesPaths[1][0]="2.xls";
        return testCasesPaths;
    }

    @Test(dataProvider="getXlsPath")
    public void test2(String xlsPath){
        System.out.println(xlsPath);
    }
}

Testng xml:

<suite name="tables" >
<test name="vendor" >
    <classes>
        <class name="Practise" ></class>
    </classes>
</test>

The code works perfectly until I specify the parallel="tests" attribute in the suite:

<suite name="tables" parallel="tests" >

In this case I could not get the suite name--it's not executing. Can someone help me on this, to get the suite name?

like image 725
pavan raju Avatar asked Aug 30 '12 14:08

pavan raju


2 Answers

You can access the Xml file and the current Xml test from the ITestContext, which can be injected in test methods:

@Test
public void f(ITestContext ctx) {
  String suiteName = ctx.getCurrentXmlTest().getXmlSuite().getName();
like image 110
Cedric Beust Avatar answered Nov 15 '22 23:11

Cedric Beust


You can read it from ITestContext

public static String getSuiteName(ITestContext context){
    return context.getCurrentXmlTest().getSuite().getName();
}
like image 1
Sandeep Chaudhary Avatar answered Nov 15 '22 22:11

Sandeep Chaudhary