API testing is a crucial aspect of ensuring the reliability and functionality of your applications. Postman, a widely used tool for crafting and running API tests, seamlessly integrates with GitHub Actions, offering an automated testing solution within your development workflow. Explore the steps involved in configuring GitHub Actions to execute Postman Cloud API tests, thereby boosting the effectiveness of your testing pipeline.
What is Postman Cloud?
Postman Cloud is a collaborative platform that allows developers to design, test, and document APIs in one place. It provides a user-friendly interface for creating and managing API requests, making it an invaluable tool for developers and testers alike.
Why GitHub Actions?
GitHub Actions is a continuous integration and continuous deployment (CI/CD) platform integrated into GitHub. It enables you to automate various tasks directly within your repository, triggered by events such as pushes, pull requests, or releases.
With GitHub Actions, you can define custom workflows using YAML files, allowing you to automate your build, test, and deployment processes effortlessly.
Following is the step-by-step guide.
Prerequisites:
- GitHub Account: Make sure you’ve a GitHub account and a repository where the GitHub actions can be configured.
- Postman Account: Create an account on the Postman platform (https://www.postman.com/) if you don’t have one already.
Setting Up GitHub Actions Workflow:
1. Create a Postman Collection:
Start by creating a Postman Collection containing the API tests you want to automate. Ensure that your collection is organized with suitable folders and requests.
2. Generate Postman API key:
You need a valid Postman API key to send requests to the Postman API. To generate the Postman API key, see here.
3. Store Secrets in GitHub:
Postman recommends that you store your Postman API key and other keys as a secret in GitHub to keep it secure. To learn more about secrets, see the GitHub documentation.
In your GitHub repository, go to Settings > Secrets and add a new secret for your Postman API key required for your tests.
4. Create a Workflow YAML File:
Inside your repository, create a .github/workflows directory and add a YAML file (e.g., postman_tests.yml). Define your workflow, specifying the triggers, jobs, and steps.
Below is a simple example:
Explanation:
- `on`: Specifies the events that initiate the workflow. In this example, the workflow runs on every push to the main branch.
- `workflow_dispatch`: Allows you to run the workflow manually (if required) from the GitHub Actions tab.
- `jobs`: Defines the jobs that the workflow performs. Here, there is one job named “test_api.”
- `runs-on`: Specifies the virtual environment for the job. In this case, it utilizes the latest version of Ubuntu.
- `steps`: Contains a series of steps that the job performs. These steps include checking out the repository, setting up Node.js, installing Newman (Postman CLI), HTML Reporter, Slack Reporter, and running Postman API tests.
- Checks-out your repository so your job can access it:
- – uses: actions/checkout@v3
- Install Node on the runner:
- – name: Install Node
- uses: actions/setup-node@v1
- with: node-version: “16.x”
- Install the Newman command line utility, the HTML extra reporter, and Slack Reporter:
- name: Install Newman, HTMLReporter and Slack Reporter
- run: |
- npm install -g newman
- npm install -g newman-reporter-htmlextra
- npm install -g newman-reporter-slackreporter
- newman – It’s a Postman’s command-line Collection Runner that allows direct execution and testing of Postman Collections through the command line.
- newman-reporter-htmlextra – An HTML reporter for Newman that presents details about the collection run in HTML format.
- newman-reporter-slackreporter – A newman reporter for Slack.
- Make a directory to upload the results:
- – name: Make Directory for Test results
- run: mkdir -p testResults
- Run the POSTMAN Collection:
- run: |
- newman run https://api.getpostman.com/collections/$COLLECTION_UID?apikey=$API_KEY
- –environment https://api.getpostman.com/environments/$ENVIRONMENT_UID?apikey=$API_KEY
- –suppress-exit-code –reporters slackreporter,htmlextra
- –reporter-htmlextra-export testResults/htmlReport.html
- –reporter-slackreporter-webhookurl $SLACK_WEBHOOK_URL
- –reporter-slackreporter-collection ‘API Regression Test Suite’
- –reporter-slackreporter-environment QA
- –reporter-slackreporter-buildurl https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}
- Checks-out your repository so your job can access it:
- `env`: Allows you to set environment variables, in this case, providing the Postman API key, Collection UID, Environment UID, and Slack Webhook URL from GitHub Secrets.
- COLLECTION_UID: ${{secrets.COLLECTION_UID}}
- ENVIRONMENT_UID: ${{secrets.ENVIRONMENT_UID}}
- API_KEY: ${{secrets.API_KEY}}
- SLACK_WEBHOOK_URL: ${{secrets.SLACK_WEBHOOK_URL_GH_ACTIONS}}
Note: Hit https://api.getpostman.com/collections & https://api.getpostman.com/environments from the Postman app to fetch respective uid. Don’t forget to pass X-Api-Key: <Postman API Key> as a header in the above GET request.
To set up Slack webhook and fetch slack_webhook_url, refer here.
Steps to execute tests:
1. Commit and Push:
Commit your changes, including the new workflow YAML file, and push them to your GitHub repository.
2. Monitor Workflow Execution:
Visit the Actions tab in your GitHub repository to monitor the execution of your Postman API tests. The workflow results will indicate whether the tests passed or failed.
After Selecting the specific GitHub Action workflow, you will see the below screen:
On click of the ‘Run Workflow’ button, a new run will be triggered:
On clicking the workflow run details, you will see the test status along with test reports.
Download the reports from the Artifacts tab and open the index.html file to view the detailed test report:
Test results will also be shared on Slack as shown below:
Conclusion:
By integrating Postman Cloud API tests into your GitHub Actions workflow, you can automate the validation of your APIs with every code change. This not only improves the efficiency of your testing process but also ensures the reliability of your applications as you continue to develop and deploy new features. Happy testing