testNG优先事项&依赖冲突

时间:2018-06-13 10:46:42

标签: java testng

我有2节课。 1)UpdateCustomerAddress 2)ResetCustomerPassword

UpdateCustomerAddress class is having 5 test methods

@Test(priority = 0)
public void createAddresses() { .... }

@Test(priority = 1, dependsOnMethods ={"createAddresses"})
public void updateAddress()  { ... }

@Test(priority = 2, dependsOnMethods ={"createAddresses"})
public void setDefaultBillingAndShippingAddress()  { ... }

@Test(priority = 3, dependsOnMethods ={"createAddresses"})
public void deleteAddress()  { ... }

@Test(priority = 4, dependsOnMethods ={"createAddresses"})
public void getAddress()  { ... }



 ResetCustomerPassword is having 2 test methods
    @Test()
    public void validPasswordReset(){ ... }

    @Test()
    public void invalidPasswordResetMissingAttribute(){ ... }



 **testng.xml**
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite">
        <test name="Mandatory test suite" preserve-order="true">
            <classes>
                <class name="webservice.customer.UpdateCustomerAddress" />
                <class name="webservice.customer.ResetCustomerPassword" />

            </classes>
        </test> <!-- Test -->

        <test name="Api test">

            <packages>
                <package name="webservice.cms"></package>
                <package name="webservice.catalog"></package>
            </packages>
        </test>

    </suite>

如果我没有为ResetCustomerPassword类中的测试设置优先级。 TestNG只会在UpdateCustomerAddress类中执行createAddresses测试,然后跳过剩下的4个测试,然后继续执行ResetCustomerPassword来执行2个测试。

如果我将优先级设置为高于UpdateCustomerAddress类,那么它将正常工作。 TestNg将在UpdateCustomerAddress类中执行5个测试,然后在ResetCustomerPassword中执行2个测试。

ResetCustomerPassword is having 2 test methods
    @Test(priority = 5)
    public void validPasswordReset(){ ... }

    @Test(priority = 6)
    public void invalidPasswordResetMissingAttribute(){ ... }

这不太理想,因为将来它可能会与其他测试方法发生冲突。

我尝试过分组4个测试依赖于UpdateCustomerAddress类中的createAddresses()方法,但它不起作用。

解决这个问题的理想情况是什么?

1 个答案:

答案 0 :(得分:0)

看起来,所有的测试方法都是以优先级和没有优先级的方式为我执行的。我只是提供一些额外的信息。

priority -> priority will be applied within a class and execution will be happened based on the ascending order rank.

dependsOnMethods --> It specifies the list of methods this method depends on. Dependent Method will be executed first and then corresponding test method will be executed. If the dependent method fails, then actual test method will be skipped.

如果你想以某种特定的顺序执行测试,那么使用类中的优先级(优先级也可以从ResetCustomerPassword中的0开始,并且没有必要使用前一类的优先级延续)否则,测试方法将以随机顺序执行。