Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make TestNG @Factory test cases run in same order as provided

Tags:

java

testng

I have some inherited TestNG code, using a @Factory to create test cases. All works fine. However even though the test cases are certainly in order when returned from the @Factory method, they aren't executed in that order. I'd like to execute them in order for ease of debugging (easier on the developer if it keeps the tests together than some random order).

Is there an easy way to do this?

I'm using TestNG 5.9 but can upgrade if needs be.

Thanks.

like image 797
Dino Fancellu Avatar asked Feb 01 '26 15:02

Dino Fancellu


1 Answers

I am currently trying to do the same. I found the following maybe that helps you:

http://beust.com/weblog2/archives/000479.html

http://testng.org/doc/documentation-main.html#methodinterceptors

If I get some sort of solution for my problem I could add some of my code here if you want.

EDIT

I am checking for 2 Kinds of TestClasses that should be executed in the order 1 2 1 2 1 2 not 1 1 1 2 2 2 as done by TestNG

public class ExampleInterceptor implements IMethodInterceptor {

@Override
public List<IMethodInstance> intercept(List<IMethodInstance> paramList, ITestContext paramITestContext) {

     //You have to watch out to get the right test if you have other tests in oyur suite        
    if (!paramITestContext.getName().equals("UnwantedTest")) {
        for (IMethodInstance iMethodInstance : paramList) {
            Object[] obj = iMethodInstance.getInstances();
            if (obj[0] instanceof Class1) {
                //DO your stuff like putting it in a list/array
            } else {
                //DO your stuff like putting it in a list/array with the other Testclasses
            }
        }
    }
    List<IMethodInstance> result = new ArrayList<IMethodInstance>();

            //Put the results in the results

    }
    return result;
}

}

Hope that helps. If you got questions ask.

like image 72
Tarken Avatar answered Feb 03 '26 03:02

Tarken