将测试数据从一个测试用例传递到另一个 [英] Pass test data from one test case to another

查看:37
本文介绍了将测试数据从一个测试用例传递到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 TestNG 和 RestAssured 测试 API.要测试此 API,需要多次调用 API.我的第一个 API 调用请求了下一个 API 调用所需的数据.

I want to test an API with TestNG and RestAssured. To test this API, several API calls are necessary. My first API call requests data that is needed in the next API call.

我的代码:

我有一个数据提供程序,它从 Excel 文件中提取测试信息并将其打包到一个 Java 对象中.

I have a data provider that extracts the test information from an Excel file and packs it into a Java object.

 @Test(dataProvider = "test1", dataProviderClass = test1.class)
    public void test1(Transfer data) {

        //basic test case for consent
        response =
                given().
                        spec(spec).
                        body(data).
                        when().
                        post("/firstCall").
                        then().
                        assertThat().
                        body("accepted", equalTo("accepted")).
                        response();
    }

现在我有了第二个测试用例.此测试用例还应使用从 Excel 文件获取一些相关信息的数据提供程序.我还必须使用 test2 中 test1 的答案中的信息.

Now I have a second test case. This test case should also use a data provider that gets some relevant information from an Excel file. I also have to use information from the answer of test1 in test2.

@Test(dataProvider = "test2", dataProviderClass = test2.class, dependsOnMethods = { "test1" })
        public void test2(Transfer data2) {

        //use test data of test1 in this test

        }

如何在第二个中使用第一个 API 调用的结果?或者最好说一下如何将信息从 test1 传递给 test2 的数据提供者?

推荐答案

首先,测试应该是atomic,意味着一个测试不依赖另一个.

First of all, tests should be atomic, meaning one test does not depend on another.

现在,在任何需要一些数据的测试中,您应该尝试从测试逻辑中拆分数据;例如,创建一个处理数据生成的对象和一个用于设置的对象.

Now, in any test that you need some data you should try to split data from test logic; for example create an object that handles data generation and one for setup.

在您的测试套件中,您可以为套件范围创建数据对象(在套件运行时创建并可用)或具有静态字段/方法的对象.

In your test suite you could create a data object for a suite scope (is created and available while the suite is running) or an object with static fields/methods.

在设置中,您可以执行创建测试数据的操作,例如:从保存的类中获取数据,否则生成新数据并请求将其添加到应用程序中.

In the setup you can have actions for creating test data for example that can: get data from a class where is saved else generate new data and do a request to add it in the app.

getSomeData() ->如果可用,返回本地保存在类中的数据,否则在应用程序中生成和创建数据

因此,在您的特定情况下,您可以将数据保存在类测试范围之外的类中,并在设置类中创建一个方法来检查是否可用,否则调用一个方法来创建它.

So in your specific case you can save the data in a class outside the class test scope and create a method in a setup class that checks if available, else call a method to create it.

这篇关于将测试数据从一个测试用例传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆