Friday, November 14, 2025

thumbnail

Testing REST APIs in Python with Postman

 1. Testing REST APIs in Postman


Postman is a powerful tool to interact with REST APIs. It allows you to send requests, view responses, and automate API tests.


Steps:


Install Postman:

If you haven’t installed Postman yet, download it from Postman’s official website

.


Create a Request:


Open Postman and click on the "New" button to create a new request.


Set the HTTP method (GET, POST, PUT, DELETE, etc.) for the request.


Provide the API endpoint URL.


Add any necessary headers (e.g., Authorization, Content-Type) and parameters if required.


Send the Request:


Click the “Send” button to execute the request.


Review the response in the lower panel. Postman shows details like the response body, headers, status code, and more.


Test the Response:


Postman allows you to write tests (in JavaScript) to validate the response. For example:


pm.test("Response code is 200", function () {

    pm.response.to.have.status(200);

});

pm.test("Response contains userId", function () {

    pm.response.to.have.jsonBody('userId');

});



You can see the test results in the "Tests" tab of the response section.


2. Testing REST APIs in Python


In Python, you can use libraries like requests and pytest to test REST APIs.


Setting up:


Install required packages:


pip install requests pytest



Write a Basic Test:

Here’s an example of testing a simple GET request to a public API like JSONPlaceholder

:


import requests


def test_get_posts():

    url = "https://jsonplaceholder.typicode.com/posts"

    response = requests.get(url)

    

    # Assert status code is 200

    assert response.status_code == 200

    

    # Assert the response contains a list of posts

    assert isinstance(response.json(), list)

    

    # Further checks (e.g., check the first post)

    first_post = response.json()[0]

    assert 'userId' in first_post

    assert 'title' in first_post

    assert 'body' in first_post



Running the test with pytest:

You can run your test with pytest, which will give you a clean output and handle test discovery for you.


pytest test_api.py


Testing POST requests:


You can also test POST requests. Here's how you can send data in a POST request and verify the response:


import requests


def test_post_data():

    url = "https://jsonplaceholder.typicode.com/posts"

    

    # Data to send in the POST request

    data = {

        "title": "foo",

        "body": "bar",

        "userId": 1

    }

    

    response = requests.post(url, json=data)

    

    # Assert status code is 201 (Created)

    assert response.status_code == 201

    

    # Verify the response JSON contains the data

    response_data = response.json()

    assert response_data["title"] == data["title"]

    assert response_data["body"] == data["body"]

    assert response_data["userId"] == data["userId"]


Testing with Authentication:


If your API requires authentication (e.g., Bearer token), you can pass the token in the headers:


def test_get_with_auth():

    url = "https://api.example.com/data"

    headers = {

        "Authorization": "Bearer your_token_here"

    }

    

    response = requests.get(url, headers=headers)

    

    assert response.status_code == 200

    assert "data" in response.json()


3. Automating API Tests with Postman and Newman (CLI Tool)


You can also run Postman collections from the command line using Newman, which is Postman’s command-line collection runner. This is useful for automating tests or integrating with CI/CD pipelines.


Install Newman:

First, install Newman globally using npm (Node.js package manager).


npm install -g newman



Export your Postman Collection:


Open Postman and go to your collection.


Click on the three dots next to the collection and select Export.


Choose the format (e.g., v2.1) and save it.


Run Postman Collection with Newman:


newman run your_collection_file.json



This will execute the collection and output the results in your terminal. You can also specify output formats (e.g., HTML, JSON).


4. Conclusion


Postman is great for manual testing and API exploration.


Python with requests is perfect for scripting and automating API tests.


Newman is a good option if you want to integrate Postman collections into your CI/CD pipeline.

Learn Fullstack Python Training in Hyderabad

Read More

Automating Tests for Full Stack Python Projects

How to Use PyTest for Full Stack Python Development

Writing Integration Tests for Python Web Applications

Debugging Your Full Stack Python Application with PDB

At Our Quality Thought Training Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive