Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

API Automated Testing with Postman and Newman

Tech 1

Background

Most developers and testers are familiar with Postmann. It's convenient to write and save test cases using Postman. However, Postman is a GUI tool, making it relatively difficult to automate testing directly. Fortunately, Postman provides a command-line tool called Newman, which allows us to combine Postman with Newman to achieve API automated testing.

Postman and Newman

Terminology

1. Collection

A Collection is a group of saved requests. Every request you send in Postman appears under the "History" tab in the sidebar. When the number of requests is small, reusing requests from history is convenient. As the number grows, finding a specific request in history becomes time-consuming. You can save all requests as a Collection for easy access.

2. Environment

An Environment is a set of variables you can use in Postman requests. You can create different environment files (e.g., dev, test, prod) containing relevant variables to test APIs against different environments.

Usage Guide

We'll use the API https://www.baidu.com/sugrec as an example.

1. Create a Collection

Create a new collection:

Create collection

Add a new request:

Add request

Test the request; expect status code 200:

Test request

Write a test assertion and verify:

pm.test("Return 200", function() {
    pm.response.to.have.status(200)
});

Test assertion

2. Create an Environment

Create a new environment:

Create environment

Add variable host:

Add variable

Use the variable in the request and test:

Use variable

3. Export Collection and Environment

Export collection as collection.json:

Export collection Export collection step 2

Export environment as environment.json:

Export environment Export environment step 2

Run API Tests

With collection.json and environment.json exported, you can run the test using a single Docker command:

docker run --rm -i -v /root/postman:/etc/newman \
    --entrypoint sh postman/newman:alpine -c \
    'npm i -g newman-reporter-html; \
    newman run collection.json \
    --suppress-exit-code 1 \
    --color off \
    --reporters cli,html \
    --reporter-html-export api_report.html \
    --environment=environment.json'

Comand breakdown:

Command breakdown

Execution result:

Result 1 Result 2

View HTML report:

HTML report

Integrate with CI for Automated API Testing

Simply prepare collection.json and environment.json for your code project, place them in a directory within your repository, and add the above Docker command to your Jenkins or GitLab CI configuration.

For more resources, including video tutorials on software testing (interviews, API, automation, performance, etc.), please refer to the link provided.

Tags: Postman

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.