Automating API Testing with Postman CLI: Advanced Capabilities and New Alternatives

Coco MinCoco Min
4 min read

Introduction to API Automation

In today's fast-paced development environments, automating the testing and orchestration of APIs is crucial. Postman, renowned for its intuitive interface, has introduced a Command Line Interface (CLI) to enhance automation and CI/CD workflows. This article explores the robust features of the Postman CLI and introduces EchoAPI, a superior tool for CLI-based API automation testing.

API.png

The Evolution of Postman’s CLI

Postman’s GUI is excellent for interactive API testing, but modern DevOps demands automation. Continuous Integration/Continuous Deployment (CI/CD) pipelines rely on automated API testing to maintain development speed and reliability. Postman’s CLI was born out of this need, bridging the gap between manual and automated API workflows. This tool allows teams to run collections, tests, and integrations from a terminal, fostering efficient and repeatable processes in an automated environment.

postman.png

How to Install and Setup Postman CLI

To leverage Postman CLI, you need a configured environment with a Postman API key for secure access to your workspace, collections, and environments. Here’s how to install it:

Windows Installation:

powershell.exe -NoProfile -InputFormat None -ExecutionPolicy AllSigned -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://dl-cli.pstmn.io/install/win64.ps1'))"

Mac (Apple Silicon) Installation:

curl -o- "https://dl-cli.pstmn.io/install/osx_arm64.sh" | sh

Mac (Intel) Installation:

curl -o- "https://dl-cli.pstmn.io/install/osx_64.sh" | sh

Linux Installation:

curl -o- "https://dl-cli.pstmn.io/install/linux64.sh" | sh

After installation, authenticate using your Postman API key:

postman login --with-api-key ABCD-1234-1234-1234-1234-1234

image.png

Running Collections and Environments

Postman CLI empowers you to execute collections, the core of Postman testing, with specific environments without modifying the collection itself:

postman run <collection.json> --environment <environment.json>

This flexibility allows for comprehensive testing across multiple configurations.

Advanced Configuration: Global and Environment Variables

Postman CLI excels at managing dynamic environments by allowing on-the-fly overrides and management of variables. This is critical for workflows where parameters need to change during testing:

postman run <collection.json> --env-var token=<new_token>

This level of control supports sophisticated API flows in dynamic testing environments.

Test Execution and Custom Reporting

Postman CLI supports automated test execution within collections and provides detailed log and report generation in multiple formats:

postman run <collection.json> --reporters cli,json --reporter-json-export output.json

This command facilitates integration with CI tools like Jenkins or GitLab, enhancing your project's test workflow.

Integrating Postman CLI into CI/CD Pipelines

Integrating Postman CLI with CI/CD pipelines is key to automated API testing. Here’s an example of a Jenkins pipeline integration:

pipeline {
    agent any
    stages {
        stage('Run Postman Tests') {
            steps {
                script {
                    sh 'postman run <collection> --reporters cli,junit --reporter-junit-export results.xml'
                }
            }
        }
    }
    post {
        always {
            junit 'results.xml'
        }
    }
}

This integration captures and reports test results, crucial for continuous deployment success.

Handling Failures and Debugging

Postman CLI provides detailed logging with the --verbose flag, essential for diagnosing test failures:

postman run <collection> --verbose

This depth of information is invaluable for debugging in CI/CD workflows.

Introducing EchoAPI: A Superior CLI Testing Tool

While Postman CLI offers robust features, EchoAPI presents an improved experience for CLI-based API testing. EchoAPI simplifies complex workflows and provides enhanced reporting capabilities, making it an excellent choice for modern API development and testing practices.

EchoAPI: A Superior CLI Testing Tool.jpg

Creating CI/CD Commands with EchoAPI

The EchoAPI CLI is designed to simplify and enhance the execution of interface cases and test cases via the command line, making it easy to integrate with CI/CD servers and build systems.

Installation

Use the following command to install EchoAPI CLI:

npm install -g echoapi-cli

If you have an older version of echoapi-cli installed, please uninstall it first:

npm list -g --depth=0  # List installed npm packages
npm uninstall -g echoapi-cli  # Uninstall command

Running Tests

Use the EchoAPI CLI to run a test suite with the following command:

echoapi run "https://app.echoapi.com/open/ci/automated_testing?ci_id=MTkzMDI0MTEwMDU2ODQ5NDA4OjEyOTMzMDc1MDgzNjc3NzEwOjEzMDcyNjE0MDg3OTQ2Mjcy&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxODE0NCwiaXNzIjoiYXBpcG9zdCIsImV4cCI6MTcxNzY5MDAxNX0.wNNw0MbsbobhDcAZmiXJQu6lmhWaES6E2y8YFyKkzm8" -n 5 -r cli,json

This command runs the test suite and generates reports in CLI and JSON format.

Key Options

  • -r, --reporters: Specify the type of test report (cli, html, json). Default is "cli".

  • -n, --iteration-count: Number of iterations. Default is 1.

  • --delay-request: Delay interval between requests. Default is 0.

  • -k, --insecure: Disable SSL verification. Default is 1.

  • --web-hook: Web-hook URL to send JSON report data post-task completion.

Example of Client Certificate Configuration File (JSON)

{
    "https://www.example.com:443": {
        "pfx": {"file_url": ""},
        "crt": {"file_url": ""},
        "key": {"file_url": ""},
        "password": ""
    },
    "https://*.echoapi.com:443": {
        "key": {"file_url": ""},
        "pfx": {"file_url": ""},
        "crt": {"file_url": ""},
        "password": ""
    }
}

Upgrading EchoAPI CLI

To upgrade to the latest version of the EchoAPI CLI tool, use:

npm install echoapi-cli@latest -g

Conclusion

Automating API testing is essential for modern development environments. Postman CLI provides a powerful suite of tools for integrating automated API tests into CI/CD workflows. However, EchoAPI extends these capabilities with improved usability and reporting features. By leveraging these tools, teams can enhance their API development and testing processes, ensuring robust, reliable integrations across all stages of development.

Try EchoAPI Interceptor today!

https://www.echoapi.com/plugin/chrome?utm_source=6715d073

0
Subscribe to my newsletter

Read articles from Coco Min directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Coco Min
Coco Min