ITestListener for @BeforeMethod/BeforeClass methods

1,354 views
Skip to first unread message

Son Le

unread,
Feb 28, 2007, 4:29:21 PM2/28/07
to testng...@googlegroups.com
Hi,

I am trying to monitor all methods ran by TestNG. So far by using ITestListener.OnTestStart I can only see the main test methods. Those methods annotated by @BeforeMethod/@BeforeClass does not trigger the listener.

Is there any more general Listener Interface that can help me in this case?

Thanks.
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=67875&messageID=128481#128481

Alexandru Popescu

unread,
Feb 28, 2007, 5:04:26 PM2/28/07
to testng...@googlegroups.com
Yep, there are but currently they are internal to TestNG:

org.testng.internal.IConfigurationListener respectively
org.testng.internal.IResultListener.

Can you tell us what are you planning to do? This will help me decide
if the above methods should become public and so stable over time.

HTH,

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

haison3000

unread,
Feb 28, 2007, 6:25:07 PM2/28/07
to testng-users
I want to write a Logger for every test/methods. My approach is to use
a static Logger class and maintain a map from a Test to a log.
Therefore, I need to know when a test is ran so that I add an entry to
the map.

-haison

On Feb 28, 2:04 pm, "Alexandru Popescu"


<the.mindstorm.mailingl...@gmail.com> wrote:
> Yep, there are but currently they are internal to TestNG:
>
> org.testng.internal.IConfigurationListener respectively
> org.testng.internal.IResultListener.
>
> Can you tell us what are you planning to do? This will help me decide
> if the above methods should become public and so stable over time.
>
> HTH,
>
> ./alex
> --
> .w( the_mindstorm )p.
> TestNG co-founder
> EclipseTestNG Creator
>

> On 2/28/07, Son Le <testng-us...@opensymphony.com> wrote:
>
>
>
> > Hi,
>
> > I am trying to monitor all methods ran by TestNG. So far by using ITestListener.OnTestStart I can only see the main test methods. Those methods annotated by @BeforeMethod/@BeforeClass does not trigger the listener.
>
> > Is there any more general Listener Interface that can help me in this case?
>
> > Thanks.
> > ---------------------------------------------------------------------
> > Posted via Jive Forums

> >http://forums.opensymphony.com/thread.jspa?threadID=67875&messageID=1...

Cédric Beust ♔

unread,
Feb 28, 2007, 6:34:44 PM2/28/07
to testng...@googlegroups.com, Hai-son Le
Hai-son

On 2/28/07, haison3000 <haiso...@yahoo.com> wrote:

I want to write a Logger for every test/methods. My approach is to use
a static Logger class and maintain a map from a Test to a log.
Therefore, I need to know when a test is ran so that I add an entry to
the map.

Add your own IReporter.  At the end, its method generateReports() will be invoked with all the suites that were run.  From there, you can access all the methods from suite.getInvokedMethods(), including configuration methods.

You can check out SuiteHTMLReporter to see how TestNG generates the reports for the alphabetical and chronological methods.

--
Cédric

Alexandru Popescu

unread,
Feb 28, 2007, 6:44:39 PM2/28/07
to testng...@googlegroups.com
Have you checked the Reporter class in TestNG?

cheerio,

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

haison3000

unread,
Mar 7, 2007, 2:40:30 PM3/7/07
to testng-users
Ive been thinking about this problem over the weekend. Let me take a
step back and explain more what I want to achieve:

- I want to Log exceptions/debug messages for every methods invoked by
TestNG( including configuration methods).
- Therefore, I want to be notified when a method is invoked so I can
add a new entry to the hashmap corresponding to this method, and after
just log all messages to this entry.

Currently, by using ITestListener, I am only be notified by the @Test
method, by not @before and @after configuration methods.

I think your suggestion using IReporter is not possible since it is
called only once after everything is done. The Reporter class log
method does not do everything I want. I want to log more than just a
String.

I looked/stepped into the current implementation of TestNG, it seems
that just an extra line in Invoker class to invoke the listeners and
add some more methods to ITestListener interface would do the job.

I also think about extending TestRunner & Invoker but its m_invoker is
private and is tightly coupled with the original Invoker.

On 28 Feb, 15:44, "Alexandru Popescu"


<the.mindstorm.mailingl...@gmail.com> wrote:
> Have you checked the Reporter class in TestNG?
>
> cheerio,
>
> ./alex
> --
> .w( the_mindstorm )p.
> TestNG co-founder
> EclipseTestNG Creator
>

> On 2/28/07, Son Le <testng-us...@opensymphony.com> wrote:
>
>
>
> > Hi,
>
> > I am trying to monitor all methods ran by TestNG. So far by using ITestListener.OnTestStart I can only see the main test methods. Those methods annotated by @BeforeMethod/@BeforeClass does not trigger the listener.
>
> > Is there any more general Listener Interface that can help me in this case?
>
> > Thanks.
> > ---------------------------------------------------------------------
> > Posted via Jive Forums

> >http://forums.opensymphony.com/thread.jspa?threadID=67875&messageID=1...

Alexandru Popescu

unread,
Mar 7, 2007, 2:55:55 PM3/7/07
to testng...@googlegroups.com
It looks like you are completely ignoring my suggestion... and I wondering why.

[quote]


Yep, there are but currently they are internal to TestNG:

org.testng.internal.IConfigurationListener respectively
org.testng.internal.IResultListener.

Can you tell us what are you planning to do? This will help me decide
if the above methods should become public and so stable over time.

[/quote]

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

haison3000

unread,
Mar 7, 2007, 3:06:09 PM3/7/07
to testng-users
sorry. I may have overlook at your comment.
Anyways, this is what I want to do:
- log exceptions/debug messages/stack traces for every methods invoked

by
TestNG( including configuration methods).

-haison

On 7 Mar, 11:55, "Alexandru Popescu"


<the.mindstorm.mailingl...@gmail.com> wrote:
> It looks like you are completely ignoring my suggestion... and I wondering why.
>
> [quote]
> Yep, there are but currently they are internal to TestNG:
>
> org.testng.internal.IConfigurationListener respectively
> org.testng.internal.IResultListener.
>
> Can you tell us what are you planning to do? This will help me decide
> if the above methods should become public and so stable over time.
> [/quote]
>
> ./alex
> --
> .w( the_mindstorm )p.
> TestNG co-founder
> EclipseTestNG Creator
>

Alexandru Popescu

unread,
Mar 7, 2007, 3:11:54 PM3/7/07
to testng...@googlegroups.com
On 3/7/07, haison3000 <haiso...@yahoo.com> wrote:
>
> sorry. I may have overlook at your comment.
> Anyways, this is what I want to do:
> - log exceptions/debug messages/stack traces for every methods invoked
> by
> TestNG( including configuration methods).
>

As already suggested: pls try out a custom implementation of
IResultListener (you should get notification for both @Test and
configuration methods). If it fulfilles your needs just let me know
and I will move those interfaces in the public package of TestNG.

toodle oo,

Steve Loughran

unread,
Mar 7, 2007, 4:09:28 PM3/7/07
to testng...@googlegroups.com
On 07/03/07, haison3000 <haiso...@yahoo.com> wrote:
>
> Ive been thinking about this problem over the weekend. Let me take a
> step back and explain more what I want to achieve:
>
> - I want to Log exceptions/debug messages for every methods invoked by
> TestNG( including configuration methods).

I havent looked at the source, but does testng use something like
log4j for its internal output?

> I looked/stepped into the current implementation of TestNG, it seems
> that just an extra line in Invoker class to invoke the listeners and
> add some more methods to ITestListener interface would do the job.

Adding new methods to an interface is a no-no for backwards
compatibility. You'd need an ITestListener2 with extra methods...

-Steve

> I also think about extending TestRunner & Invoker but its m_invoker is
> private and is tightly coupled with the original Invoker.

Short term, patching your own source may be the best option.

Alexandru Popescu

unread,
Mar 7, 2007, 4:21:16 PM3/7/07
to testng...@googlegroups.com
On 3/7/07, Steve Loughran <steve.l...@gmail.com> wrote:
>
> On 07/03/07, haison3000 <haiso...@yahoo.com> wrote:
> >
> > Ive been thinking about this problem over the weekend. Let me take a
> > step back and explain more what I want to achieve:
> >
> > - I want to Log exceptions/debug messages for every methods invoked by
> > TestNG( including configuration methods).
>
> I havent looked at the source, but does testng use something like
> log4j for its internal output?
>

We have an internal logging class, but it is for internal use only.

> > I looked/stepped into the current implementation of TestNG, it seems
> > that just an extra line in Invoker class to invoke the listeners and
> > add some more methods to ITestListener interface would do the job.
>
> Adding new methods to an interface is a no-no for backwards
> compatibility. You'd need an ITestListener2 with extra methods...
>
> -Steve
>
> > I also think about extending TestRunner & Invoker but its m_invoker is
> > private and is tightly coupled with the original Invoker.
>
> Short term, patching your own source may be the best option.
>

No, it is not an option... at least as long as there are a lot better solutions.

haison3000

unread,
Mar 8, 2007, 2:45:22 AM3/8/07
to testng-users
Thanks Alex. Another post of yours @ http://www.archivesat.com/TestNG/thread2331833.htm
is very helpful.

I solved my first problem. Now I can log all messages with @Test and
@configuration methods.
Unfortunately, I ran into another problem. I use ITestContext to get
all the ITestResult for all methods.
Is there anyway I can get the methods executed in chronological order?
The reason is that I want to know which
@configuration methods associated with each @Test method.

-haison

On 7 Mar, 12:11, "Alexandru Popescu"

Cédric Beust ♔

unread,
Mar 8, 2007, 2:49:06 AM3/8/07
to testng...@googlegroups.com
You should have the time in milliseconds when they started, isn't this enough?

--
Cedric

haison3000

unread,
Mar 8, 2007, 2:08:47 PM3/8/07
to testng-users
Well, that solution would not scale if we decide to run the tests
parallel.

On 7 Mar, 23:49, "Cédric Beust ♔ " <cbe...@google.com> wrote:
> You should have the time in milliseconds when they started, isn't this
> enough?
>
> --
> Cedric
>

> --
> Cédric

Cédric Beust ♔

unread,
Mar 8, 2007, 2:16:52 PM3/8/07
to testng...@googlegroups.com
Why not?

--
Cedric


--
Cédric

Alexandru Popescu

unread,
Mar 8, 2007, 2:19:33 PM3/8/07
to testng...@googlegroups.com
On 3/8/07, haison3000 <haiso...@yahoo.com> wrote:
>
> Well, that solution would not scale if we decide to run the tests
> parallel.
>

Not sure what you mean. Even if they are run in parallel the start
time will give you the correct order. Am I missing something?

haison3000

unread,
Mar 8, 2007, 2:59:37 PM3/8/07
to testng-users
The start time will give the order of execution. But I cannot
determine
which @configuration method associated with each @Test method.
Well, unless I can check the thread ID.

Is there a simpler solution?

On 8 Mar, 11:19, "Alexandru Popescu"
<the.mindstorm.mailingl...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages