testng-team/testng

Issue in ITestcontext.getAllTestMethods() with annotation @BeforeSuite

Closed this issue · 0 comments

TestNG Version

7.8.0

Issue

ITestcontext.getAllTestMethods() with annotation @BeforeSuite returns the method names incorrectly from other test classes

Whole context of an issue

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 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

TestNG Dev email conversation - https://groups.google.com/g/testng-dev/c/iM7Ot6pLyIg/m/zp3WqHq3AwAJ