Ordering issues in testng listeners

50 views
Skip to first unread message

Shubham Gupta

unread,
Nov 19, 2020, 11:30:01 PM11/19/20
to testng-users
I have put some listeners in the testNG.xml file and I want them to execute in a particular order. Is there a way to do it ? 

⇜Krishnan Mahadevan⇝

unread,
Nov 20, 2020, 3:44:54 AM11/20/20
to testng-users
TestNG listener ordering is not guaranteed.

If you are keen on ordering the listeners that were created by you (and not by others), then you can do something like this:
  1. Define your own interface (in simple terms you can merely extend the TestNG listener that you are interested in).
  2. Create implementations for your interface that you defined in step (1)
  3. Use the Service Provider Interface (Service Loaders approach) to create a text file whose file name matches the interface in (1)
  4. Create 1 generic TestNG listener implementation that implements all the listeners which you extended in (1)
  5. Wire in this listener (which was created in (4)) using any one of the mechanisms [ either via "@Listeners" (or) via <listeners> (or) via Service Loaders ]
  6. Now within (5) include logic to load the listeners that were injected via SPI [ as explained in step (3) ]
  7. Within (5) internally iterate through all the implementations that you wired in [ step (3) ] and call them in the order ]that you desire.
Basically, what I am suggesting is that you need to create one orchestrating listener [ which will be invoked by TestNG] and this orchestrating listener will invoke the other listeners that you need in the order in which you need them to be invoked ]

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 Scribblings @ https://rationaleemotions.com/


On Fri, Nov 20, 2020 at 10:00 AM Shubham Gupta <sgni...@gmail.com> wrote:
I have put some listeners in the testNG.xml file and I want them to execute in a particular order. Is there a way to do it ? 

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/c41b0989-dfb1-49a1-911c-f5429caf0819n%40googlegroups.com.

Shubham Gupta

unread,
Nov 20, 2020, 4:33:02 AM11/20/20
to testng...@googlegroups.com
Hello Krishnan, 
Can this be done using ListnerChain class implemented by one of the testNG devs ? If so, Can you please put a light on the same ?

⇜Krishnan Mahadevan⇝

unread,
Nov 20, 2020, 6:27:10 AM11/20/20
to testng-users
What exactly do you mean by implemented by TestNG dev ?

I didn't quite understand that part.

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 Scribblings @ https://rationaleemotions.com/

Shubham Gupta

unread,
Nov 20, 2020, 7:41:59 AM11/20/20
to testng...@googlegroups.com
There is a developer in testNG team who has defined a concept called Listenerchain. Refer below link for the discussion on the same topic:

⇜Krishnan Mahadevan⇝

unread,
Nov 20, 2020, 8:26:49 AM11/20/20
to testng-users
It looks to me that it is doing exactly what I mentioned in my previous email. You can try giving it a whirl and see if that helps. 
That implementation seems to be specifically cater to the order in which the listeners are being added (if that is what you are looking for).


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 Scribblings @ https://rationaleemotions.com/

Shubham Gupta

unread,
Nov 20, 2020, 8:36:29 AM11/20/20
to testng...@googlegroups.com
Ok, I went through the blog you wrote for the same thing but it was quite difficult to understand for me. If you have sometime Can we arrange a call for this (maybe on skype) ?  

Reply all
Reply to author
Forward
0 new messages