Listener for test class invocation (start and finish)

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Listener for test class invocation (start and finish)

Benjamin Muschko
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Listener for test class invocation (start and finish)

Julien Herr
You mean like IClassListener?

Le mardi 29 août 2017 21:14:52 UTC+2, Benjamin Muschko a écrit :
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Listener for test class invocation (start and finish)

Benjamin Muschko


On Tuesday, August 29, 2017 at 3:17:03 PM UTC-4, Julien Herr wrote:
You mean like <a href="https://jitpack.io/com/github/cbeust/testng/master-6.12-g8a3b74b-30/javadoc/org/testng/IClassListener.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjitpack.io%2Fcom%2Fgithub%2Fcbeust%2Ftestng%2Fmaster-6.12-g8a3b74b-30%2Fjavadoc%2Forg%2Ftestng%2FIClassListener.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGa40A5Blnv_3iWE41NWLyQWhsm3w&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjitpack.io%2Fcom%2Fgithub%2Fcbeust%2Ftestng%2Fmaster-6.12-g8a3b74b-30%2Fjavadoc%2Forg%2Ftestng%2FIClassListener.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGa40A5Blnv_3iWE41NWLyQWhsm3w&#39;;return true;">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Listener for test class invocation (start and finish)

Julien Herr
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 

Le mardi 29 août 2017 22:03:58 UTC+2, Benjamin Muschko a écrit :


On Tuesday, August 29, 2017 at 3:17:03 PM UTC-4, Julien Herr wrote:
You mean like <a href="https://jitpack.io/com/github/cbeust/testng/master-6.12-g8a3b74b-30/javadoc/org/testng/IClassListener.html" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjitpack.io%2Fcom%2Fgithub%2Fcbeust%2Ftestng%2Fmaster-6.12-g8a3b74b-30%2Fjavadoc%2Forg%2Ftestng%2FIClassListener.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGa40A5Blnv_3iWE41NWLyQWhsm3w&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjitpack.io%2Fcom%2Fgithub%2Fcbeust%2Ftestng%2Fmaster-6.12-g8a3b74b-30%2Fjavadoc%2Forg%2Ftestng%2FIClassListener.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGa40A5Blnv_3iWE41NWLyQWhsm3w&#39;;return true;">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Listener for test class invocation (start and finish)

Benjamin Muschko
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?

On Tuesday, August 29, 2017 at 4:10:55 PM UTC-4, Julien Herr wrote:
Since 6.9.10 <a href="https://github.com/cbeust/testng/blob/master/CHANGES.txt#L167" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fcbeust%2Ftestng%2Fblob%2Fmaster%2FCHANGES.txt%23L167\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGnKD1oZe0_ZcanSvl4QmOYRVNV7A&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fcbeust%2Ftestng%2Fblob%2Fmaster%2FCHANGES.txt%23L167\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGnKD1oZe0_ZcanSvl4QmOYRVNV7A&#39;;return true;">https://github.com/cbeust/testng/blob/master/CHANGES.txt#L167
But it had some issues and you should use 6.11 or 6.12 

Le mardi 29 août 2017 22:03:58 UTC+2, Benjamin Muschko a écrit :


On Tuesday, August 29, 2017 at 3:17:03 PM UTC-4, Julien Herr wrote:
You mean like <a href="https://jitpack.io/com/github/cbeust/testng/master-6.12-g8a3b74b-30/javadoc/org/testng/IClassListener.html" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjitpack.io%2Fcom%2Fgithub%2Fcbeust%2Ftestng%2Fmaster-6.12-g8a3b74b-30%2Fjavadoc%2Forg%2Ftestng%2FIClassListener.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGa40A5Blnv_3iWE41NWLyQWhsm3w&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjitpack.io%2Fcom%2Fgithub%2Fcbeust%2Ftestng%2Fmaster-6.12-g8a3b74b-30%2Fjavadoc%2Forg%2Ftestng%2FIClassListener.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGa40A5Blnv_3iWE41NWLyQWhsm3w&#39;;return true;">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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Listener for test class invocation (start and finish)

Krishnan Mahadevan

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/

 

From: <[hidden email]> on behalf of Benjamin Muschko <[hidden email]>
Reply-To: <[hidden email]>
Date: Friday, September 8, 2017 at 10:10 PM
To: testng-users <[hidden email]>
Subject: [testng-users] Re: Listener for test class invocation (start and finish)

 

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?

On Tuesday, August 29, 2017 at 4:10:55 PM UTC-4, Julien Herr wrote:

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 

Le mardi 29 août 2017 22:03:58 UTC+2, Benjamin Muschko a écrit :



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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.