Best way to add custom BEFORE/AFTER annotation

167 kishi ochdi
Birinchi o‘qilmagan xabarni ochish

Baubak

o‘qilmagan,
26-sen, 2021, 06:20:1826/09/21
– testng-users
Hi there,

My current project is an open source plugin for TestNG (will be sharing it when I am happy with it), which introduces a category of tests with specific behaviors in a normal test project.

I would like to create my own @BeforeMyEvent method that is invoked before my category of tests are run.

There are a lot of workarounds I know. My question is if there is a clean way of doing that?

Obviously I can integrate it in a Test listener and invoke the @before method by parsing the code. Or set my tests in a dedicated group, and invoke beforeGroup. But ideally I would like to have my own annotation. Is theree a way to get my method to be invoked by the testng framework (picked by JDK15AnnotationFinder)?

Best regards,

Baubak

⇜Krishnan Mahadevan⇝

o‘qilmagan,
28-sen, 2021, 00:42:0828/09/21
– testng-users
Have you considered adding your custom annotation which gets parsed in a TestNG IInvokedMethodListener and depending on the annotation being found, invokes your custom behavior ? This is a standard way in which you can build custom annotation driven behavior.


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/


--
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/5c500c67-0846-43e6-9af9-a8947d74a371n%40googlegroups.com.

Baubak

o‘qilmagan,
28-sen, 2021, 03:40:0128/09/21
– testng-users
Thanks for your answer Krishnan,

What are the added values in using InvokedMethodListener rather than invoking it in another listener such as ITestListener?

Best regards,

Baubak

⇜Krishnan Mahadevan⇝

o‘qilmagan,
28-sen, 2021, 04:02:5928/09/21
– testng-users
IInvokedMethodListener would help you intercept both configuration and test methods, whereas the same is not going to be possible when you use ITestListener.

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/

Baubak

o‘qilmagan,
28-sen, 2021, 08:13:3428/09/21
– testng-users
Hi Again Krishnan,

By "adding your custom annotation which gets parsed in a TestNG IInvokedMethodListener", do you mean that I search for it in in the IInvokedMethodListener implementation, or that there is a smart way of having my annotated method to be considered as a configuration method? If there is I'd love an example of how it could work.

Best regards,

Baubak

On Tuesday, September 28, 2021 at 6:42:08 AM UTC+2 Krishnan Mahadevan wrote:

Krishnan Mahadevan

o‘qilmagan,
28-sen, 2021, 08:37:1028/09/21
– testng...@googlegroups.com
Please see here for an example of what I am suggesting.


In the above class I look for a custom annotation before deciding to do some browser management.


Thanks & Regards
Krishnan Mahadevan

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

Baubak

o‘qilmagan,
28-sen, 2021, 09:06:5828/09/21
– testng-users
Thanks a lot,

So if I understand you correctly the listener you have written assumes that the annotation should be on the test.

so it expects something like
@Test
@Browser
public void test() {
}

So what I can do is something like;

@Test
@MyCustomAnnoation() {
//Do stuff
}

And then when I reach it I can do the stuff inside?

Best regards,

Baubak

⇜Krishnan Mahadevan⇝

o‘qilmagan,
28-sen, 2021, 12:22:4928/09/21
– testng-users
Yes. That is correct. You just need to switch the annotation and then the annotation parsing logic in the listener.

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/

Baubak

o‘qilmagan,
3-okt, 2021, 10:55:1703/10/21
– testng-users
Hi,

After trying the IInvokedMethodListener, I decided that using another implementation of the IAnnotationTransformer would be more suitable for my use case:

public void transform(IConfigurationAnnotation annotation, Class testClass, Constructor testConstructor,
            Method testMethod)

It allowed me to intercept the configuration methods. In my case I wanted to make sure that in some cases my configuration method should be ignored. It seemed easier to implement that here. I am not sure the IInvokedMethodListener would allow me to disable a method execution.

Again, thanks for your suggetions :)

Baubak
Hammaga javob berish
Muallifga javob yozish
Uzatish
0 ta yangi xabar