Simplifying Data-Driven Automation in Postman for Effortless API Testing

Introduction

Automating API testing is crucial for ensuring software reliability. Data-Driven Testing, a powerful methodology, involves executing test cases with different data sets to enhance test coverage. In this short guide, we'll explore Data-Driven Testing in Postman, using CSV and JSON files, along with environment variables. Let's dive in!

What is Data-Driven Testing?

Data-Driven Testing (DDT) is a testing approach where the same test logic is executed with multiple sets of input data. Instead of creating separate test cases for each scenario, DDT allows testers to use different datasets, enhancing test coverage and efficiency. In simple terms, it ensures that an application performs well under various conditions by testing with diverse input values.

Types of Data-Driven Testing in Postman

Postman, a widely used API testing tool, provides robust support for Data-Driven Testing :

1. CSV Data Files

CSV (Comma-Separated Values) files are a simple and widely adopted method for Data-Driven Testing. In Postman, you can create a collection and use a CSV file to provide different sets of input data. Each row in the CSV file represents a unique set of inputs for a test case.

2. JSON Data Files

JSON (JavaScript Object Notation) is another format that can be leveraged for Data-Driven Testing in Postman. Similar to CSV files, JSON files store data in a structured format. By utilizing a JSON file, you can manage complex data structures and use them as input for your API tests.

Run a collection with data files

In this example, you will create a collection, write a test for variable values, and import a CSV file into the Collection Runner. You will preview the file, specify any column types if necessary, and run the collection. Then, you will inspect your log to ensure your data was parsed correctly.

1.Run a collection with csv file

  1. From the Collections menu, select + to add a new collection. Then, name the collection Data Driven.

  2. Add a Post request with the following address. Name it Register.

     https://reqres.in/api/register/prepod
    

    Let the body of API be:

     {
         "email": "{{email}}",
         "password": "{{password}}"
     }
    

    Here we accessing the variable that will be created by csv or json file. please don't create variable manually we will fetch email and password variable from csv file or json file.

  3. Add a test script to test the variable values:

     javascriptCopy codepm.test("Status code is 201", function(){
         pm.response.to.have.status(201);
     });
    
     var em = pm.variables.get("email");
     var pwd = pm.variables.get("password");
    
     pm.test("Check email " + em, function () {
         var jsonData = pm.response.json();
         pm.expect(jsonData.email).to.eql(em);
     });
    
     pm.test("Check password " + pwd, function () {
         var jsonData = pm.response.json();
         pm.expect(jsonData.password).to.eql(pwd);
     });
    
  4. Create a test CSV file as shown below. Name it test.csv.

     graphqlCopy codeemail,password
     pankaj@gmail.com,Pankaj124
     rinaldo@gmail.com,Rin1234
     don@gmail.com,Don1234
    
  5. Return to the collection overview and select Run.

  6. Select your data file using the Select File button.

  7. After you select your data file, select Preview to inspect the data in the file before you start the run.

  8. Select Run CSV Data Types to begin the run with the values from the file. The Collection Runner runs the collection requests for each iteration (row) in the data file. The output indicates the results for any tests you defined in your collection requests.

  9. Inspect the Console log by selecting Console in the footer, and observe that both the values and the variable validation are correct.

    2.Run a collection with json file

    1. Create a test JSON file as shown below. Name it APIJSONDATA.json.

       [
         { "email": "user1@example.com", "password": "password123" },
         { "email": "user2@example.com", "password": "securePass" },
         { "email": "admin@example.com", "password": "adminPass456" }
       ]
      
    2. Clear All tab of postman. Return to the collection overview and select Run.

    3. Select your data file using the Select File button.

    4. After you select your data file, select Preview to inspect the data in the file before you start the run.

    5. Click On Run Button And Inspect the Console log by selecting Console in the footer, and observe that both the values and the variable validation are correct.

Conclusion

In summary, Data-Driven Testing in Postman enhances API testing efficiency by executing the same test logic with various input data sets. This approach ensures comprehensive test coverage and robustness across different scenarios.