DEV Community

Jaira Encio for AWS Community ASEAN

Posted on • Updated on

Setting up Automated API testing with Postman Collection using Newman CLI, AWS Codebuild, CodePipeline, & S3

In this post you will learn how to automate your own build of postman collections with aws codecommit, codepipeline, and codebuild.

Newman is a command-line collection runner for Postman.

Postman is an API platform for building and using APIs.

CodeCommit is a secure, highly scalable, managed source control service that hosts private Git repositories.

CodeBuild compiles your source code, runs unit tests, and produces artifacts that are ready to deploy.

CodePipeline is a continuous delivery service you can use to model, visualize, and automate the steps required to release your software.

File Structure

── /CodeCommitRepo/         # root folder
  ├── /buildspec.yml        # source code for build commands
  └── /api.postman_collection.json # postman collection
Enter fullscreen mode Exit fullscreen mode

Overview

  1. Export Postman Collection
  2. Create Buildspec.yml
  3. Create CodeCommit Repository and push files
  4. Create S3 bucket for output report
  5. Create a build project in AWS CodeBuild
  6. Create pipeline in CodePipeline and build all together

1. Export Postman Collection

Open your postman collection and select the more actions icon More actions icon next to the collection, then select Export.

export postman1

Select the format you'd like your collection to export as then select export to download your newly generated JSON file.

export postman2

2. Create Buildspec.yml

Copy the code below to buildspec.yml. Make sure to place your file along with your exported postman collection.

version: 0.2

phases:
  pre_build:
    commands:
      - npm install -g newman
      - npm install -g newman-reporter-html

  build:
    commands:
      - newman run -r html,cli test_api.postman_collection.json --reporter-html-export report.html

artifacts:
  files:
    - report.html
Enter fullscreen mode Exit fullscreen mode

This installs newman and newman-reporter-html dependencies for our postman cli. After prebuild, this will run cli command to run and generate html report from our postman collection newman run -r html,cli {name}.postman_collection.json --reporter-html-export report.html.

3. Create CodeCommit Repository and push files

Open the CodeCommit console at https://console.aws.amazon.com/codesuite/codecommit/home then click create repository

Code Commit1

Enter Repository name and description (optional) then click create

Code Commit2

Code Commit3

Clone repository and push your buildspec and postman file. You can specify a certain branch but we will just be using master.

4. Create s3 bucket for report

This will serve as the output folder for our generated html report.

Open the S3 console at https://console.aws.amazon.com/s3/ then click Create bucket

s3 1

Enter bucket name and click Create bucket
s3 2

s3 4

s3 5

5. Create a build project in AWS CodeBuild

Open the AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home then click Create project

codebuild1

Fill in the following sections. Once complete, choose Create build project at the bottom of the page.

Sections:
Project configuration - Name and description
Source provider - AWS Codecommit, repo name
Environment - Ubuntu, Standard, aws/codebuild/standard:6.0
Buildspec
Batch configuration
Artifacts - s3, bucket name
Logs

codebuild2

codebuild source

codebuild environment

codebuild buildspec

codebuild artifacts

6. Create pipeline in CodePipeline and build all together

We will now combine our process by using AWS CodePipeline to pull our source code in CodeCommit, run build with AWS CodeBuild and output generated html in S3

Open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home and choose Create Pipeline

code pipeline1

On the Step 1: Choose pipeline settings page; in Pipeline name, enter the name for your pipeline.

Choose New service role to allow CodePipeline to create a new service role in IAM.

code pipeline step1

On the Step 2: Add source stage page; in Source provider, select AWS CodeCommit, repo name, and branch name.

code pipeline step2

On the Step 3: Add build stage page, select AWS Codebuild and project name

code pipeline step3

On the Step 4: Add deploy stage page, Choose Skip deploy stage.

On the Step 5: Review, Review all changes then click create pipeline.

Output

You should see Succeeded status after running pipeline. The build log shows from prebuild ,running command, and generating output.

run pipeline1

run pipeline2

run pipeline3

Go to your s3 bucket to view successful report

s3 output1

s3 output2

To share this html report with others. You can use presigned URLs

To generate a presigned URL using the AWS Management Console

  • In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for.
  • In the Objects list, select the object that you want to create a presigned URL for.
  • On the Actions menu, choose Share with a presigned URL.
    presigned url1

  • Specify how long you want the presigned URL to be valid.
    presigned url2

  • Choose Create presigned URL.

  • When a confirmation appears, the URL is automatically copied to your clipboard. You will see a button to copy the
    presigned URL if you need to copy it again.
    presigned url3

References:
https://learning.postman.com/docs/running-collections/using-newman-cli/installing-running-newman/

https://www.npmjs.com/package/newman-reporter-html

https://unmesh.me/2017/06/10/api-testing-with-postman-collections-in-aws-codepipeline/

Top comments (1)

Collapse
 
ivy07 profile image
Ivy ☕

Awesome article, will definitely try this!