Pass test data from one test case to another

TryHard :

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.

My code:

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();
    }

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

        }

How can I use the result of the first API call in the second? Or better say how can I pass the information from test1 to the data provider of test2?

lauda :

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() -> returns data saved locally in a class if available, else generate and create data in the app

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20901&siteId=1