Issue in ITestcontext.getAllTestMethods() with annotation @BeforeSuite

74 views
Skip to first unread message

Prakash Kannan

unread,
Jun 15, 2023, 1:51:52 PM6/15/23
to testng-dev
I have written logic to get all the enabled tests from the test classes and pass those enabled test names to other components that I use for my project. 

Test cases are enabled via the testng.xml file using the <methods> tag[ with <include>] under <class>. If I don't pass <methods> tag, then all tests under that test class are enabled, and if I pass the methods> tag, only those tests are enabled for execution.

I have 3 test classes which will have different test methods and all class should have the following code [To get the enabled test cases and pass those methods for other components in my project]

Test Class Name: CreateTests
Test Method Name: create
Test Class Name: UpdateTests
Test Method Name: update
Test Class Name: DeleteTests
Test Method Name: delete

@BeforeSuite
public void preConditionsSetUp(ITestContext preSetupContext) {
ArrayList<String> enabledTestCasesList = new ArrayList<>();
ITestNGMethod[] testNGMethod = preSetupContext.getAllTestMethods();
for (ITestNGMethod allMethods : testNGMethod) {
enabledTestCasesList.add(allMethods.getMethodName());
}
System.out.println("enabledTestCasesList in class 'UpdateTests' :: "+enabledTestCasesList);
}


My testng.xml file is like this

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="QA Test Suite" verbose="1">

<test name="CreateTest">
<classes>
<class name="testcases.module1.CreateTests"/>
</classes>
</test>
<test name="UpdateTest">
<classes>
<class name="testcases.module1.UpdateTests"/>
</classes>
</test>
<test name="Delete">
<classes>
<class name="testcases.module1.DeleteTests"/>
</classes>
</test>

</suite>


When I execute above testng.xml, enabledTestCasesList any of 3 test classes,  ITestcontext.getAllTestMethods() returns the method from last test case 'delete' instead 'create','update'.

I've attached the test maven project here.

TestNGIssue.zip

⇜Krishnan Mahadevan⇝

unread,
Jun 16, 2023, 4:20:23 AM6/16/23
to testn...@googlegroups.com
Prakash,
This is a bug in TestNG. Can you please help file an issue here: https://github.com/testng-team/testng/issues


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-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-dev/3ac175ec-4cad-472e-8c81-27c1ef9bbe51n%40googlegroups.com.

⇜Krishnan Mahadevan⇝

unread,
Jun 16, 2023, 4:54:35 AM6/16/23
to testn...@googlegroups.com
Just to add some additional details, when a "@BeforeSuite" or a "@AfterSuite" is being executed, it should be agnostic of ITestContext.
This is because suite level methods are never part of any particular <test> tag. So this injection itself is invalid.

You should be using "@BeforeTest" (or) "@AfterTest" for your logic. 

The bug will ensure that this dependency injection is disabled (i.e., if you try to inject ITestContext object into a @BeforeSuite or @AfterSuite method you will get an error message.

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/

Prakash Kannan

unread,
Jun 16, 2023, 12:29:53 PM6/16/23
to testng-dev
Thank you, Krishnan for the information and suggestion. I will use @BeforeTest 

Fyi - I tried with @BeforeClass and I am not seeing the issue.

Reply all
Reply to author
Forward
0 new messages