Listener for test class invocation (start and finish)

183 views
Skip to first unread message

Benjamin Muschko

unread,
Aug 29, 2017, 3:14:52 PM8/29/17
to testng-users
Hi,

Is there a listener I could use to react to the following events?

1. A test class was configured and is about to execute its first test method.
2. A test class executed all of its test methods and is about to be destroyed.

To illustrate the functionality I could imagine the listener interface to look like this:

public interface ITestClassListener extends ITestNGListener {
   
void onStart(ITestClass testClass);
   
void onFinish(ITestClass testClass);
}

My use case: I would like to react to the fact that a test class (not its individual methods) has been started or finished. I searched through the API but couldn't find a fitting listener. The class ITestListener can provide access to the test class but it feels somewhat unnatural having to go down that route.

Please let me know what you think.

Thanks,

Ben

Julien Herr

unread,
Aug 29, 2017, 3:17:03 PM8/29/17
to testng-users
You mean like IClassListener?

Benjamin Muschko

unread,
Aug 29, 2017, 4:03:58 PM8/29/17
to testng-users


On Tuesday, August 29, 2017 at 3:17:03 PM UTC-4, Julien Herr wrote:
You mean like IClassListener?

That looks like the one. Thanks for the hint. I will play around with that.

Do you know which version of TestNG introduced the interface? I think I am on a version (6.3.1) that doesn't provide it yet. Granted that version is very old.

Julien Herr

unread,
Aug 29, 2017, 4:10:55 PM8/29/17
to testng-users
Since 6.9.10 https://github.com/cbeust/testng/blob/master/CHANGES.txt#L167
But it had some issues and you should use 6.11 or 6.12 

Benjamin Muschko

unread,
Sep 8, 2017, 12:40:59 PM9/8/17
to testng-users
Thanks for the information. That works. I have a follow up question though. 

Is there a way to tell what suite an instance of ITestClass belongs to? Or alternatively can you retrieve this information from an instance of ITestContext? If that information can be resolved can you point me to the TestNG version that implements it?

Krishnan Mahadevan

unread,
Sep 8, 2017, 12:57:24 PM9/8/17
to testng...@googlegroups.com

Would this do?

 

import org.testng.IClassListener;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestClass;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;

@Listeners(GetClassInfo.ClassListener.class)
public class GetClassInfo {
   
@Test
   
public void testMethod() {
        System.
err.println("Hello world");
    }

   
public static class ClassListener implements IClassListener, IInvokedMethodListener {

       
@Override
       
public void onBeforeClass(ITestClass testClass) {
            String clazzName = testClass.getName();
            XmlSuite suite = testClass.getXmlTest().getSuite();
            System.
err.println("Suite Object for " + clazzName);
            System.
err.println(suite.toXml());
        }

        
@Override
       
public void onAfterClass(ITestClass testClass) {

        }

       
@Override
       
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
            String clazzName = method.getTestMethod().getTestClass().getName();
            ITestContext context = testResult.getTestContext();
            System.
err.println(clazzName + " has its test context as " + context);

        }

       
@Override
       
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {

        }
    }
}

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages