Re: [testng-dev] add test method execution using IMethodInterceptor listener. ... .

18 views
Skip to first unread message

Michael Braiman

unread,
Mar 7, 2019, 11:32:35 AM3/7/19
to testn...@googlegroups.com
wWw X&*#
 .   . .  

On Wed, Mar 6, 2019, 2:53 AM Alexey Anischenko, <avam0...@gmail.com> wrote:
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

On Wednesday, March 6, 2019 at 7:06:46 AM UTC+3, Krishnan Mahadevan wrote:

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/

 

From: <testn...@googlegroups.com> on behalf of Alexey Anischenko <avam0...@gmail.com>
Reply-To: <testn...@googlegroups.com>
Date: Tuesday, March 5, 2019 at 7:46 PM
To: testng-dev <testn...@googlegroups.com>
Subject: [testng-dev] add test method execution using IMethodInterceptor listener

 

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

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

--
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 7, 2019, 3:48:02 PM3/7/19
to testng-dev
 Michael, 
can you elaborate your point a little bit more? ;)

Speaking of me, I managed to make it work for spherical tests in vacuum (see my sample), except the fact that TestNG 6.14 and 7 crashes reporters at the end. 

For real ones (mine are using Webdriver, and I store the driver instance in a field), there are tons of other problems with TestNG  - it seems to continue calling @aftertest-annotated method somewhere in the middle of test flow,
and/or creating different class object instances for different calls of the same test method - thus losing the object fields. 
But I managed to make it work single-threaded by marking these @beforeTest and @afterTest methods as static members, and making the fields I use there as static too.

Michael Braiman

unread,
Mar 7, 2019, 6:15:44 PM3/7/19
to testn...@googlegroups.com
Sorry, my android phone typed this message in my pocket.
Reply all
Reply to author
Forward
0 new messages