How to invoke TestNG annotations from a different class

553 views
Skip to first unread message

MCD dotCom

unread,
Feb 13, 2022, 1:58:04 PM2/13/22
to testng-users

Hello,

Test1.java

Public class Test1 extends BaseTest(){

….

}



Test2.java

Public class Test2 extends BaseTest(){

….

}



ClassA.java

Public class ClassA(){

@BeforeSuite

Public void beforeSuite(){


System.out.println(“before suite”);


}



@BeforeMethod

Public void beforeMethod(){

System.out.println(“before method”);

}

}



If I run test1 and test2 (testng tests), BeforeSuite and BeforeMethod annotations do not get invoked as expected. 


One way I could fix it is by including these annotations in BaseTest.java but I do not want to include these two annotations in BaseTest. 



In BaseTest.java, is there a way I can invoke BeforeSuite and BeforeMethod annotations without extending ClassA? I am not a java expert. I assume I need to add some code in BaseTest and ClassA files? Please let me know if you have any suggestions.


⇜Krishnan Mahadevan⇝

unread,
Feb 14, 2022, 11:31:12 PM2/14/22
to testng-users

In BaseTest.java, is there a way I can invoke BeforeSuite and BeforeMethod annotations without extending ClassA? I am not a java expert. I assume I need to add some code in BaseTest and ClassA files? Please let me know if you have any suggestions.

No you cannot. For TestNG to be able to execute one or more configuration classes, it has to satisfy one of the following:
  1. The configuration annotated method is found in the current class.
  2. The configuration annotated method is found in the parent class.
  3. The configuration annotated method is found in one of the classes that are being executed as part of the current TestNG suite (Applicable for "@BeforeSuite|@BeforeTest|@BeforeGroups") 
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/1388e5c8-9cc8-43f6-9879-54af6c613961n%40googlegroups.com.

MCD dotCom

unread,
Feb 17, 2022, 2:58:41 PM2/17/22
to testng-users
Thank you!

Eric Angeli

unread,
Mar 11, 2022, 10:18:10 AM3/11/22
to testng-users
The Before/After annotations can also be inherited. So you could create a base class with the Before/After methods and then have your test classes extend that base class. Testng is smart enough to only run the BeforeSuite methods once, so it doesn't matter how many extend that base class. I have found this useful for reusing the BeforeSuite method(s) in test classes that belong to different suites/groups.
Reply all
Reply to author
Forward
0 new messages