Listener for test class

106 views
Skip to first unread message

Raghu Nandan

unread,
Jun 3, 2011, 2:31:17 AM6/3/11
to testn...@googlegroups.com
Hi,

My tests have some common operations to be performed (like logging ) before and after every test class. Since these operations are common for all classes, I don't want to put them in @BeforeClass and @AfterClass of each test class. It would be great if I can do this one time in a listener.

For example, a listener, say ITestClassListener with methods onTestClassStart () and onTestClassFinish () called before @BeforeClass and after @AfterClass respectively :). I am already implementing ITestListener/ISuiteListener to do common operations before/after test and suite.

Please guide me if there is already a way to achieve my requirement. Thanks!

Thanks,
Raghu

Nalin Makar

unread,
Jun 3, 2011, 4:01:21 AM6/3/11
to testn...@googlegroups.com
A simple way is to have these classes extend a common base class and putting these common methods in there.

Is there anything keeping you from adding inheritance?

-nalin



--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to testn...@googlegroups.com.
To unsubscribe from this group, send email to testng-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en.

Cédric Beust ♔

unread,
Jun 3, 2011, 12:48:04 PM6/3/11
to testn...@googlegroups.com
Hi Raghu,

A couple of suggestions:
  • Use an IConfigurationListener. One caveat: I just noticed that this interface doesn't extend ITestNGListener, which might or might not be a problem depending on how you declare that listener. I'll push a new beta that fixes this shortly.

  • Use an IObjectFactory. This is a bit more heavyweight in the sense that your factory will be invoked whenever TestNG needs to instantiate an object but this is also one way you can be informed whenever a new class is about to be run by TestNG (and obviously, you don't need to override the construction of the object itself, just invoke the constructor and its arguments as they are passed to you).
Hope this helps.

-- 
Cédric




On Thu, Jun 2, 2011 at 11:31 PM, Raghu Nandan <ragh...@gmail.com> wrote:

Raghu

unread,
Jun 4, 2011, 9:09:28 PM6/4/11
to testng-dev
Hi Cedric,

Thanks a lot. I will try your suggestions.

Thanks,
Raghu

On Jun 3, 9:48 pm, Cédric Beust ♔ <ced...@beust.com> wrote:
> Hi Raghu,
>
> A couple of suggestions:
>
>    - Use an IConfigurationListener. One caveat: I just noticed that this
>    interface doesn't extend ITestNGListener, which might or might not be a
>    problem depending on how you declare that listener. I'll push a new beta
>    that fixes this shortly.
>
>    - Use an IObjectFactory. This is a bit more heavyweight in the sense that
Reply all
Reply to author
Forward
0 new messages