Alternative for IMethodInterceptor

46 views
Skip to first unread message

santhosh

unread,
Nov 29, 2017, 12:43:16 PM11/29/17
to testng-users
Hi All,

I am looking for the alternative for the listener "IMethodInterceptor".

Basically I am looking for the way to get the list of the test methods included for the run with out running the testng.xml. 

I have some requirement to get the list of scoped test method with some programmatic way not with the listner for which we need to run tests.

Note: We are using beanshell to include the test methods with the group name.

Below is the sample example:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite thread-count=“1” configfailurepolicy="continue" name=“SAMPLETEST SUITE” parallel="tests">

  <test name=“SAMPLE” group-by-instances="true">

    <method-selectors>

      <method-selector>

        <script language="beanshell">

          <![CDATA[(groups.containsKey("P0")) && (groups.containsKey(“P1”))]]>

        </script>

      </method-selector>

    </method-selectors>

    <packages>

      <package name="*">

        <include name=".*"/>

      </package> 

    </packages>

  </test> 

</suite> 


The above configuration is the input for me to parse and get the list of the methods. But I am not sure that how to get the list of the included methods from the bean shell with above xml in programatic way.


Thanks
Santhosh.

Krishnan Mahadevan

unread,
Nov 29, 2017, 9:56:19 PM11/29/17
to testng...@googlegroups.com

Santhosh,

 

TestNG currently doesn’t have something called a “dry-run” mode wherein you could have easily figured out the list of methods that would be executed, but without actually those methods being executed.

 

The BeanShell that you shared is an example of method selection and not necessarily method interception. These two are two different things.

 

Beanshell is another way of plugging in a org.testng.IMethodSelector using which you get to decide which method to include and which one not to.

 

The org.testng.IMethodInterceptor basically lets you do the following :

 

  • Re-order the tests (For e.g., you might want to sort the tests based on some annotation, such that the fast running tests run first and then the slow ones)
  • Increase/decrease the number of tests that would be executed by TestNG. Take a look at this blog post that Cedric wrote, wherein he shows how to leverage the IMethodInterceptor and simulate Mock testing

 

So for your usecase the IMethodInterceptor should suffice I guess, wherein you get the list of methods that would be executed. But since TestNG doesn’t have a dry-run option yet [ There’s a bug that is tracking this https://github.com/cbeust/testng/issues/1503 ], you could perhaps go extreme and just invoke System.exit(0) from within your Interceptor after you have gotten the list of methods, and maybe print it or something. Just talking loud here!

 

 

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/

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

Cédric Beust ♔

unread,
Nov 29, 2017, 11:15:14 PM11/29/17
to testng...@googlegroups.com

Krishnan,

FYI, it would be pretty easy to implement -dryRun:

  • Create a subclass of IWorker which, instead of actually running a test, immediately returns with a success status
  • Return these workers from the IWorkerFactory.

-- 
Cédric


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.

Krishnan Mahadevan

unread,
Nov 29, 2017, 11:29:31 PM11/29/17
to testng...@googlegroups.com

Thanks Cedric. I have self-assigned https://github.com/cbeust/testng/issues/1503

I will give this a try and raise a PR.

 

 

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/

 


-- 

Cédric

 

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.

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

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

oleksandr...@gmail.com

unread,
Dec 1, 2017, 3:22:33 PM12/1/17
to testng-users
I use for this purpose interfaces IHookable, IConfigurable:

public abstract class BaseTest implements IHookable, IConfigurable {

@Override
public void run(IHookCallBack callBack, ITestResult testResult) {

if (Config.getProperty("skip_test_execution", false))
return;
callBack.runTestMethod(testResult);
}


@Override
public void run(IConfigureCallBack callBack, ITestResult testResult) {

if (Config.getProperty("skip_test_execution", false)) return;
callBack.runConfigurationMethod(testResult);
}
}
But all your tests should extend this class

среда, 29 ноября 2017 г., 19:43:16 UTC+2 пользователь santhosh написал:

santhosh soma

unread,
Dec 2, 2017, 9:43:50 AM12/2/17
to testng...@googlegroups.com
Thanks for the quick response. 

--
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+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages