Unclear usage of IMethodSelector.setTestMethods method

40 views
Skip to first unread message

Aleksei Kolesnikov

unread,
Jun 27, 2023, 5:19:55 AM6/27/23
to testng-users
I can't clearly understand how IMethodSelector.setTestMethods should be used.
I found the following points about this method: 
  1. The input parameter of method always an empty list. 
  2. The method called before IMethodSelector.includeMethod, while the javadoc comment say it should be called after test methods are known [0]
  3. Usage of the method in the internal XmlMethodSelector is commented as ugly [1]
So my question is how this method should be used? And in case usage should be avoided, are there any plans to remove this method? 

⇜Krishnan Mahadevan⇝

unread,
Jun 27, 2023, 6:58:24 AM6/27/23
to testng...@googlegroups.com
I think this functionality is perhaps broken and needs to be fixed.
Can you please help file a bug at https://github.com/testng-team/testng/issues so that we dont forget to address this ?


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/f565261e-9612-43b5-bc41-8eaad8125aaen%40googlegroups.com.

⇜Krishnan Mahadevan⇝

unread,
Jun 27, 2023, 7:11:54 AM6/27/23
to testng...@googlegroups.com
I dug in a bit more.

The issue seems to be hovering around setMethods() only. Either that functionality is broken or it never worked at all (i couldnt find any unit tests for this part of IMethodSelector.

Here's a sample that shows the part wherein you decide if a method is to be selected or not in action.

Sample test case

Java
import org.testng.annotations.Test;

public class TestClassExample {

  @Test
  public void dragonWarriorTestMethod() {
    System.err.println("Dragon Warrior");
  }

  @Test
  public void shifuTestMethod() {
    System.err.println("Master Shifu");
  }
}

Sample method selector

Java
import java.util.List;
import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;

public class SampleMethodSelector implements IMethodSelector {

  @Override
  public boolean includeMethod(IMethodSelectorContext context, ITestNGMethod method,
      boolean isTestMethod) {
    boolean select = method.getMethodName().startsWith("dragonWarrior");
    try {
      return isTestMethod && select;
    } finally {
      //Don't evaluate any other method selector (TestNG adds a default one viz., XmlMethodSelector)
      context.setStopped(true);
    }
  }

  @Override
  public void setTestMethods(List<ITestNGMethod> testMethods) {
  }
}

Sample suite xml

Markup
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="609_suite">
  <test thread-count="5" name="609_test" verbose="2">
    <method-selectors>
      <method-selector>
        <selector-class name="com.example.selectors.SampleMethodSelector"/>
      </method-selector>
    </method-selectors>
    <classes>
      <class name="com.example.selectors.TestClassExample"/>
    </classes>
  </test>
</suite>


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/

Aleksei Kolesnikov

unread,
Jun 27, 2023, 9:28:42 AM6/27/23
to testng-users

вторник, 27 июня 2023 г. в 14:11:54 UTC+3, Krishnan Mahadevan:
Reply all
Reply to author
Forward
0 new messages