add test method execution using IMethodInterceptor listener

33 views
Skip to first unread message

Alexey Anischenko

unread,
Mar 5, 2019, 9:16:14 AM3/5/19
to testng-dev
Hi,

There are tons of examples on how to reorder, shuffle, delete some tests from the flow by implementing  IMethodInterceptor  and then adding this class as listener.


But, is there any way to ADD new test executions of THE SAME test method, in the specified order?

I mean, I have this interceptor method getting a list of: testmethod1, testmethod2.
I want for TestNG to execute the following sequence of test methods: testmethod1, testmethod2, testmethod2, testmethod1, testmethod1, testmethod2, testmethod1.

Creating a new list to return, and filling it with multiple copies of the corresponding IMethodInstance elements from the original list  - does not help. 
Need a way to create a NEW IMethodInstance elements specifying the test name. Is there any?

WBR,
Alexey

Krishnan Mahadevan

unread,
Mar 5, 2019, 11:06:46 PM3/5/19
to testn...@googlegroups.com

Alexey,

 

>>>> Creating a new list to return, and filling it with multiple copies of the corresponding IMethodInstance elements from the original list  - does not help. 

 

Why do you say so? Can you please help elaborate ?

 

The other option that you can use is to basically start duplicating XmlTest/XmlClass objects by implementing the IAlterSuiteListener, Would that help ?

 

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

Alexey Anischenko

unread,
Mar 6, 2019, 2:53:37 AM3/6/19
to testng-dev
Hi Krishnan,

 I need a way to change order of execution for test methods (I have multiple test-methods per test-class), not the whole test-classes.

Here's the simple example, it works fine adding my methods in the order I specified (except the fact that listener is called twice with the same list of methods). Until I add dependencies.


The listener:
---------------------------------------------
import java.util.LinkedList;
import java.util.List;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;

public class InterleaveExampleListener implements IMethodInterceptor {

@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
List<IMethodInstance> retVal = new LinkedList<IMethodInstance>(methods);
retVal.add(methods.get(0));
retVal.add(methods.get(0));
retVal.add(methods.get(1));
retVal.add(methods.get(1));
retVal.add(methods.get(0));
retVal.add(methods.get(0));
retVal.add(methods.get(1));
return retVal;
}
}
-----------------------

The test:

-----------------------
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({ InterleaveExampleListener.class })

public class InterleaveExample {

  @Test
  public void test0() {
  Reporter.log("this is test 0");
  }

//  @Test(dependsOnMethods = { "test0" }, enabled = true)
  @Test
  public void test1() {
  Reporter.log("this is test 1");
  }
  
//  @Test(dependsOnMethods = { "test1" }, enabled = true)
  @Test
  public void test2() {
  Reporter.log("this is test 2");
  }
}
----------------------

The above works fine. But, uncommenting test dependencies annotations ruins it all - listener is called once with full list of tests, then with test1 + test2, then with just test2, and does not execute the methods I add to the end of my returned list.

I need both the way to add my methods to the sequence, and the way to exactly specify the whole test method sequence. 
I will now play with removing all the dependencies from all my @Test annotations, and will then just create the proper orderfrom the scratch in method interceptor  listener. 

Other option is to rewrite my tests to be "one test-method per class" and then play with AlterSuiteListener.

WBR,
Alexey
Reply all
Reply to author
Forward
0 new messages