Running dependent and independent test methods in

2019-04-01 01:07发布

问题:

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!

回答1:

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!



标签: testng