Testng:仅称为第二类测试套件中的测试方法一次

发布于 2025-01-29 00:15:10 字数 1747 浏览 4 评论 0原文

我在不同类别中有两种测试方法。第一种方法是说“ createCustomer”是使用dataprovider从Excel文件中获取输入并创建三个客户。

 @Test(dataProvider = "createCustomerTestData", dataProviderClass = createCustomerDataProvider.class)
    public void addStockAccountWithAllInput(ITestContext context) throws JsonProcessingException {

    //Rest-Assured code goes here

        ISuite suite = context.getSuite();
        suite.setAttribute("customerID", js.get("customerID"));

}

第二种方法是,假设“ getCustomer”应该从“ createCustomer”方法的输出中获取客户的ID,并使用iTestContext和Isuite将其用作输入。

@Test
public void testGetCustomer(ITestContext context) {

    ISuite suite = context.getSuite();

//Rest-Assured code goes here

    Assert.assertEquals(js.getString("customerID"), suite.getAttribute("customerID"));
}

当我从TestNG XML文件运行测试套件时,仅将第三个客户的ID传递给“ GetCustomer”方法。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true"         
        <classes>
            <class name="addCustomer">
                <methods>
                    <include name="testAddCustomer"/>
                </methods>
            </class>
        </classes>
    </test>
    <test verbose="2" preserve-order="true"
                  <classes>
            <class name="getCustomer">
                <methods>
                     <include name="testGetCustomer"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

我如何更改设置或代码,以便每次调用CreateCustomer并创建客户时都会调用“ GetCustomer”方法?

I have two test methods in different classes. The first method, let's say "createCustomer" is using dataProvider to get the inputs from an excel file and creates three customers.

 @Test(dataProvider = "createCustomerTestData", dataProviderClass = createCustomerDataProvider.class)
    public void addStockAccountWithAllInput(ITestContext context) throws JsonProcessingException {

    //Rest-Assured code goes here

        ISuite suite = context.getSuite();
        suite.setAttribute("customerID", js.get("customerID"));

}

The second method, let's say "getCustomer" is supposed to get the id of the customers from output of "createCustomer" method and use them as input, using ITestContext and ISuite.

@Test
public void testGetCustomer(ITestContext context) {

    ISuite suite = context.getSuite();

//Rest-Assured code goes here

    Assert.assertEquals(js.getString("customerID"), suite.getAttribute("customerID"));
}

When I run the test suite from the testng xml file, only the id of the third created customer will be passed to "getCustomer" method.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true"         
        <classes>
            <class name="addCustomer">
                <methods>
                    <include name="testAddCustomer"/>
                </methods>
            </class>
        </classes>
    </test>
    <test verbose="2" preserve-order="true"
                  <classes>
            <class name="getCustomer">
                <methods>
                     <include name="testGetCustomer"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

How can I change the settings or the code so that the "getCustomer" method gets called every time createCustomer is called and a customer created?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2025-02-05 00:15:11

这对您有用。

//Create a temp list of String to save ID
static List<String> ids = new ArrayList<>();

@Test(dataProvider = "createCustomerTestData", dataProviderClass = createCustomerDataProvider.class)
public void addStockAccountWithAllInput(ITestContext context) throws JsonProcessingException {

    //Rest-Assured code goes here

    ISuite suite = context.getSuite();
    String id = js.get("customerID");
    ids.add(id);
    suite.setAttribute("customerIDs", ids);
}

@Test
public void testGetCustomer(ITestContext context) {

    ISuite suite = context.getSuite();

    //Make api call based on the list IDs from above test 
    for (int i = 0; i < suite.getAttribute("customerIDs").size(); i++){
        //Rest-Assured code goes here
        Assert.assertEquals(js.getString("customerID"), suite.getAttribute("customerIDs").get(i));
    }
}

This would work for you.

//Create a temp list of String to save ID
static List<String> ids = new ArrayList<>();

@Test(dataProvider = "createCustomerTestData", dataProviderClass = createCustomerDataProvider.class)
public void addStockAccountWithAllInput(ITestContext context) throws JsonProcessingException {

    //Rest-Assured code goes here

    ISuite suite = context.getSuite();
    String id = js.get("customerID");
    ids.add(id);
    suite.setAttribute("customerIDs", ids);
}

@Test
public void testGetCustomer(ITestContext context) {

    ISuite suite = context.getSuite();

    //Make api call based on the list IDs from above test 
    for (int i = 0; i < suite.getAttribute("customerIDs").size(); i++){
        //Rest-Assured code goes here
        Assert.assertEquals(js.getString("customerID"), suite.getAttribute("customerIDs").get(i));
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文