Aks..
Aks..

Reputation: 1403

How to get the name of the class that is acted upon by listener of TestNG?

Hello I have a simple testNG project which has a SampleTest.java class file which has 2 test cases and I have added a listener called MyListener to it . For this I have a MyListener.java class file which extends the TestListener of TestNG where in I'm printing pass or fail or skipped depending upon the test case execution. So every time i run SampleTest I can see Pass/fail in the console.. But I want it with the classname

My problem statement is, How can i get the Test Case file name (i.e. Sampletest here) in the MyListener class??? I tried with stacktrace but no help.. As I guess its not being called but its just acting upon/listening to the testcases in the file.. Please let me know how can I get the name of that class in listener????

SampleTest.java:

package com.abc;

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners({ com.mindtree.MyListener.class})
public class SampleTest {

    @Test
    public void SampleTest() throws Exception{
        System.out.println("Hello world");
    }
    @Test
    public void SampleTest1() throws Exception{
        System.out.println("Hello Swarna");
    }
}

MyListener.java:

package com.abc;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class MyListener extends TestListenerAdapter  {

    private int m_count = 0;

      @Override
      public void onTestFailure(ITestResult tr) {
        log("Fail");
      }

      @Override
      public void onTestSkipped(ITestResult tr) {
        log("Skipped");
      }

      @Override
      public void onTestSuccess(ITestResult tr) {
        log("\nSuccess\n");
      ## WANT TO PRINT HERE THE TESTCASE CLASS NAME
      }

      private void log(String string) {
        System.out.print(string);
        if (++m_count % 40 == 0) {
          System.out.println("");
        }
      }

}

but wont work for multiple testcase files.. Just create an object of SampleTest in MyListener access the classname through getters and setters

Upvotes: 3

Views: 6775

Answers (5)

Cristian Contreras
Cristian Contreras

Reputation: 128

None of the solution posted worked for me, however I found that it is possible to get the required information by accessing the "ITestResult" object in the following manner:

public void onTestSuccess(ITestResult result) {
    log("\nSuccess\n");
    String className = result.getInstanceName();      
    log(className);
}

NOTE: The above code was tested on my environment successfully

Upvotes: 2

drkthng
drkthng

Reputation: 6929

From your description I understand you want to get the name of the class where your @Test annotated test-methods reside, and only the class name without the package name

In your case that would be the output: "SampleTest".

You can achieve this the following way:

public void onTestSuccess(ITestResult tr) {
        log("\nSuccess\n");
        String className = tr.getTestClass().getRealClass().getSimpleName()      
        log(className);
}

The solution of @JacekM also works fine but in your case would return "com.abc.SampleTest"

Upvotes: 2

JacekM
JacekM

Reputation: 4099

You have the access to ITestResult object, so you should be able to do:

public void onTestSuccess(ITestResult tr) {
   String className = tr.getMethod().getTestClass().getName();
   System.out.println(className);
}

Upvotes: 0

zenbeni
zenbeni

Reputation: 7213

Use a beforeMethod method in which you can get the name of the method

@BeforeMethod
public void beforeMethod(final Method method) throws Exception {
String testName = method.getName();
}

Upvotes: 0

R.Moeller
R.Moeller

Reputation: 3446

You can find out by examining the stacktrace like:

StackTraceElement trace[] = Thread.currentThread().getStackTrace(); you can get the the calling class by trace[i].getClassName().

Note this is pretty expensive, so don't use in loggers etc.

Upvotes: 0

Related Questions