Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running dependent and independent test methods in expected sequence

Tags:

testng

I want to execute test methods in sequence. I have used classes in testng.xml with preserve-order set to true.

<test name="group-test" preserve-order="true" >
    <classes>
        <class name="com.dependency.ClassA">
            <methods>
                <include name="create"/>
                <include name="enter"/>
                <include name="delete"/>
            </methods>
        </class>
    </classes>
</test>

and my Test class is

public class ClassA {


@Test()
public void Create() throws Exception
{
    System.out.println("in method create");

}   
@Test(dependsOnMethods= "Create")
public void Enter() throws Exception
{
    System.out.println("in method Enter");

}
@Test()
public void delete() throws Exception
{
    System.out.println("in method delete");

}

After executing the test my output is
in method create,
in method delete,
in method enter

But what I want is to first execute "create" then "enter" then "delete" method. Here delete is an independent test method.

I read on a google group question where Cedric Beust mentions that you can either use dependency OR explicitly include test methods in testng.xml. I don't understand why is this an enforcement? What if I want to execute independent and dependent test methods together in any sequence I want? I have observed that independent methods get executed first and then the dependent methods.


Ideally dependency should not be for preserving order but to skip test if the previous method fails. The kind of enforcement TestNG has is causing a lot of trouble!

like image 218
Rit Avatar asked Dec 19 '25 21:12

Rit


1 Answers

I am strugging with the same major (and I must say obvious) deficiency of testng. The best solution I have found so far is to use priorities. EG @Test(priority = 10), then next test @Test(priority = 20), etc. The documentation and internet searches I found so far have all said to use @Test(priority = 1), then @Test(priority = 2), but then you run into the obvious future maintenance problem of having to renumber all your tests every time you add one in the middle somewhere... So this solution of 10, 20, etc. Is much better as it at least allows you to add an @Test(priority = 11), 12, etc in between test1 and test2. It does work, I've verified. Lucky for us testng does not enforce 1,2,3 or we'd really be in trouble! Oh and BTW, if you have group and method dependancies (which you shouldn't use on all tests unless required!) then it just looks like @Test(priority = 10, groups = "login"), @Test(priority = 20, groups = "login")etc. Also, it seems like you already know, but for others maybe wondering, remember if dependancies are used to set test run ordering then if one fails then all tests after are skipped- which is not at all what you want. Anyway, hope this helps to get you unstuck, at least until a better solution comes along. Good luck!

like image 121
M_Tech Avatar answered Dec 21 '25 17:12

M_Tech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!