Is it possible to get all test methods that belong to a group?

79 views
Skip to first unread message

Nagesh

unread,
Apr 20, 2016, 4:18:17 AM4/20/16
to testng-users
Hello!

Is there a programmatic way to get all test methods that belong to a certain group?

⇜Krishnan Mahadevan⇝

unread,
Apr 20, 2016, 7:33:41 AM4/20/16
to testng...@googlegroups.com
1. Create a XmlTest instance.
2. Invoke org.testng.internal.annotations.AnnotationHelper#findMethodsWithAnnotation [ here you can pass an instance of JDK15AnnotationFinder() for IAnnotationFinder)
3. Now from the ITestNGMethod[] array build Set<ITestClass> (just to ensure we get only the unique classes.
4. Now invoke org.testng.internal.MethodGroupsHelper#findGroupsMethods with the Set<ITestClass> obtained from (3) and false [ so that we get all beforeXXX methods that are part of your group ]
5. Repeat step (4) with false [so that we get all afterXXX methods that are part of your group ]

The map that you get from (4) and (5) essentially would hold the details that 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 Scribbings @ http://rationaleemotions.wordpress.com/

On Wed, Apr 20, 2016 at 1:48 PM, Nagesh <nagesh....@gmail.com> wrote:
Hello!

Is there a programmatic way to get all test methods that belong to a certain group?

--
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 post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Nagesh

unread,
Apr 21, 2016, 12:20:23 AM4/21/16
to testng-users
Thanks Krishnan. I was struggling to define what I was actually looking for. I got an idea of what needed to be done, from one of your earlier posts and was able to adapt it to what I wanted to do.

ramnarayan patro

unread,
Apr 21, 2016, 1:32:07 AM4/21/16
to testng-users
Krishnan Sir, Can you give a demo example ? or else any blog you have written on this.

Thanks in Advance.

Ram Narayan

⇜Krishnan Mahadevan⇝

unread,
Apr 22, 2016, 3:49:38 AM4/22/16
to testng...@googlegroups.com
Ram,
Let me see if I can put up something over the weekend.

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 Scribbings @ http://rationaleemotions.wordpress.com/

⇜Krishnan Mahadevan⇝

unread,
Apr 28, 2016, 12:11:36 AM4/28/16
to testng...@googlegroups.com
It turns out that there's a much more simpler way of doing this.
I have created a sample to show how to get this information from a ISuiteListener implementation 


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 Scribbings @ http://rationaleemotions.wordpress.com/

Nagesh

unread,
May 13, 2016, 9:43:06 PM5/13/16
to testng-users
Neat solution. Thanks for sharing Krishnan. 

What I needed and eventually ended up doing after I asked this question was to get a unique list of groups that belong to the test classes in my suite.

May not be optimal, but did the trick for me.

private List<String> getListOfGroupsInTestClasses(ITestContext context) {
Collection<ITestClass> testClassesMap = ((TestRunner) context).getTestClasses();

List<List<String>> groupsList = new ArrayList<>();
for (ITestClass testClass : testClassesMap) {
List<ITestNGMethod> testMethods = Arrays.asList(testClass.getTestMethods());
for (ITestNGMethod testMethod : testMethods) {
List<String> groupsPerMethod = Arrays.asList(testMethod.getGroups());
groupsList.add(groupsPerMethod);
}
}
List<String> overallGroupsList = new ArrayList<>();
for (List<String> groups : groupsList) {
for (String finalGroup : groups) {
overallGroupsList.add(String.valueOf(finalGroup));
}
}
return new ArrayList<>(new LinkedHashSet<>(overallGroupsList));
}

-Nagesh
Reply all
Reply to author
Forward
0 new messages