2

when I tried to execute this below code, I am confused with the order of Test Methods E and A.

My Output order is C->D->E->A->B

public class Example5 
{

    @Test
    public void A()
    {
        System.out.println("A");
    }
    @Test(dependsOnGroups={"MM"})
    public void B()
    {
        System.out.println("B");
    }
    @Test(groups={"MM"})

    public void C()
    {
        System.out.println("C");
    }
    @Test(groups={"MM"})
    public void D()
    {
        System.out.println("D");
    }
    @Test
    public void E()
    {
        System.out.println("E");
    }
}

From the output, I can see Test Methods C and D got executed before B method(this I can understand), but what I don't understand is the sequential order of E and A methods.

Please explain how TestNG follows sequential order in this code

Manglu
  • 10,744
  • 12
  • 44
  • 57
J_Coder
  • 707
  • 5
  • 14
  • 32

1 Answers1

2
<suite name="Suite-A">
<test name="test">
    <classes >
        <class name="stack1.LoginTest"></class>
        <methods>
            <include name="A" />
            <include name="E" />
            <include name="B" />
            <include name="C" />
            <include name="D" />
        </methods>
    </classes>
</test>
</suite>

Use this suite to run where methods are explicitly mentioned in the class. And you can see the out put as A > E > C > D > B . I guess this is what you are expecting to see.

If you want to set the order in code, you can use @Test( priority = 1 ) for your methods. Lower priorities will be scheduled first. So for example -2 will execute before 1.

If you want to preseve order for multiple classes, use group-by-instances="true" in the testng.xml suite.


Related Stackoverflow Links:

Cedric's Blog:
http://beust.com/weblog/2008/03/29/test-method-priorities-in-testng/

TestNG document:
http://testng.org/doc/documentation-main.html

Community
  • 1
  • 1
Chandan Nayak
  • 10,117
  • 5
  • 26
  • 36