Skip to content

Automating Postman Tests with GitHub Actions

Adeshile Oluwatosin edited this page Aug 24, 2024 · 4 revisions

Documentation: Automating Postman Tests with GitHub Actions

Overview

This documentation outlines the steps taken to set up an automated testing workflow for the hng_boilerplate_golang_web repository using GitHub Actions. The workflow is designed to run Postman tests every 15 minutes and upload the test results to a specified API endpoint.

Objectives

  • Set up a GitHub Actions workflow that runs Postman tests using Newman.
  • Schedule the workflow to run every 15 minutes.
  • Upload the test results to an API endpoint.

Requirements

  • GitHub repository: hng_boilerplate_golang_web
  • A Postman collection to be tested: KimikoGolangSwagger.postman_collection.json
  • GitHub Actions enabled for the repository
  • An API endpoint to receive the test results

Steps Followed on GitHub

1. Navigating to the Repository

Go to the hng_boilerplate_golang_web repository on GitHub.

2. Creating a New Branch

In the GitHub repository, click on the branch dropdown (usually shows main or master). Type a new branch name, such as setup-postman-tests, and press Enter to create the branch.

3. Setting Up the Workflow File

  1. Create a New File: In the new branch, navigate to the .github/workflows directory. If it does not exist, create it.
  2. Add a New File: Click on "Add file" > "Create new file".
  3. File Naming: Name the file run-postman-tests.yaml.
  4. Edit the File: Copy and paste the following content into the editor:
name: Run Postman Tests Every 15 Minutes

on:
  workflow_dispatch:
  schedule:
    - cron: '*/15 * * * *' # Every 15 minutes

jobs:
  run-postman-tests:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install Newman
        run: npm install -g newman

      - name: Run Newman tests
        id: newman
        run: |
          newman run status/postman/KimikoGolangSwagger.postman_collection.json --reporters cli,json --reporter-json-export report.json
        continue-on-error: true

      - name: Upload Newman Report to API
        run: |
          curl -X POST ${{ vars.API_URL }}/api/v1/api-status \
          -H "Content-Type: multipart/form-data" \
          -F "result_file=@report.json"
  1. Commit Changes: Scroll down, provide a commit message like "Add GitHub Actions workflow for Postman tests", and commit the file directly to the new branch.

4. Creating a Pull Request (PR)

  1. Open a PR: Once the file is committed, GitHub will prompt you to open a pull request. Click "Compare & pull request".
  2. Review PR: Review the changes, ensure everything looks good, and add a description.
  3. Create PR: Click "Create pull request" to submit the PR for review.

5. Merging the Pull Request

After the PR is reviewed and approved (or if you are the only maintainer), merge the PR into the main branch. This will make the workflow active, and it will start running as scheduled.

Golang 2

6. Monitoring the Workflow

Navigate to the "Actions" tab in the repository. You can see the workflow runs, their statuses, and logs to debug if necessary.

image

Testing and Validation

  1. Check Workflow Run: The workflow should run every 15 minutes as scheduled. You can also manually trigger it using workflow_dispatch.
  2. Validate Results: After each run, ensure the tests pass and the results are correctly uploaded to the API endpoint.
  3. Debugging: If tests fail or the report upload does not work, review the logs available in the "Actions" tab for troubleshooting.

Conclusion

By setting up this GitHub Actions workflow, you have automated the process of running Postman tests and uploading the results at regular intervals. This helps in maintaining the reliability of the API by ensuring that it is continuously tested, and any issues are detected early.

This setup provides a robust CI/CD pipeline for API testing, ensuring that the system remains reliable and errors are identified promptly.