top of page

Automating your API testing with AWS and Rest-assured

We recently shared a guide post on our LinkedIn page from the AWS DevOps blog series that gives a step by step guide on setting up a pipeline to build and deploy a simple API, then execute some tests written with Postman.


We wanted to use this as a pattern and show some examples using tests created with different tools and tech. First up we're going to show you how to integrate REST-assured tests into this pipeline as an additional step in the pipeline after the postman tests have ran.


If you want to follow along hands on we're assuming you've read and followed the previous guide already and are familiar with the project used and how to deploy it. If you follow this as well you'll end up with a pipeline that looks like the picture below.


As a point of note this guide focus on getting a pattern for running REST-assured tests in a AWS CodePipeline. We haven't written tests here that run against the API deployed, which is easy enough to do once you've set up the foundations defined below.



First up we'll create a REST-assured project. One of the easiest ways to do that is through the course on the Test Automation University. There's links in there to the project used for the course which you can fork, clone and play with.


You'll need to add a buildspec YAML file, which gives CodeBuild the commands needed to build the test execution runtime then execute the tests. In this case we'll use a simple mvn command to execute all the tests in the project.

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto8
  build:
    commands:
      - echo Build started on `date`
      - chmod -R 755 .
      - mvn clean test
      - echo Build completed on `date`

  post_build:
    commands:
      - echo Deploy completed on `date`

Next up, we're going to update the petstore-api-pipeline.yaml file that we created in the previous guide. What we need to do here is add another set of actions into the source stage to source the REST-assured project. We then need to a step into the pipeline to build and run the REST-assured test project. We'll also update the parameter list so that we can pass in the details on the second repository.


Below is the snippet to add to the parameter list in the petstore-api-pipeline.yaml file, replacing - making sure the parameter name is different for the repo that hosting the code from the previous guide.


GitHubRepositoryNamexxx:
  Type: String
  Description: GitHub repository name for the restassured project.
  MinLength: 1
  MaxLength: 100

Next up we're going to define the environment configuration for the container that will be used to build and execute the REST-assured tests. To get started you can simply copy the code in the PostmanNewmanCodeBuildProject section of the petstore-api-pipeline.yaml file, and rename values of newman with restassured. Two crucial amendments needed are details in the green text below. These are the parameter names that will be passed into the CloudFormation CLI command to be used to trigger the pipeline.


BuildSpec: "rename to the location of the buildspec file created in the previous step"

Location: !Join
  - ''
  - - 'https://github.com/'
    - !Ref parameter name of your github user
    - '/'
    - !Ref parameter name of your github repo

We need to define how we source the REST-assured project. In this example I've done this within the existing Source stage if the pipeline, by adding the following code to the petstore-api-pipeline.yaml file.


- Name: CheckoutRestAssuredTests
  ActionTypeId:
    Owner: ThirdParty
    Category: Source
    Version: 1
    Provider: GitHub
  Configuration:
    Owner: !Ref GitHubUser
    PollForSourceChanges: false
    Repo: !Ref parameternametoidentifyrepoaddedabove
    Branch: !Ref GitHubBranch
    OAuthToken: !Ref GitHubToken
  InputArtifacts: []
  OutputArtifacts:
    - Name: 'nameofyouroutputartifact'
  RunOrder: 2

Next up we'll add another stage to the pipeline defining the point in the pipeline that the tests executed, passing in the build configuration and importantly the input artifact, which is the output from sourcing the REST-assured project from its repository. Code will look something like the snippet below.


- Name: nameofyourpipelinestep
  Actions:
    - Name: nameofyourpipelinestepaction
      ActionTypeId:
        Owner: AWS
        Category: Build
        Version: 1
        Provider: CodeBuild
      Configuration:
        ProjectName: !Ref nameofyourbuildconfiguration #add something here
      InputArtifacts:
        - Name: 'outputartifactnamefromsourceaction'
      OutputArtifacts:
        - Name: 'someoutputvalueyouwanttodefine'
      RunOrder: 1

So now your code should be all set up, the final stage is to run the CloudFormation CLI command to trigger the pipeline. This will need to be updated to define the parameter key and value for the repo that you are using to store your REST-assured project. If its in a different account to the original petstore prject used in this guide, then you'll need to add parameter for your user and auth token aswell!


Code snippet of what to add to the aws cloudformation create-stack command.

ParameterKey=parameternameforyourrepo,ParameterValue=yourreponame

Now you are ready to run the "aws cloudformation create-stack...... command you have saved from the previous guide/walkthrough. Punch it into your terminal and watch the pipeline run with the additional actions we have added in. You can check the CodeBuild logs to see the REST-assured test being executed, you'll see something like the picture below.




We hope you found this short guide useful from a practical sense and helps you improve your 'cloud-testing' game!


If you have any problems following this guide please let us know we'd be happy to help! Thanks for reading.


2,394 views

Recent Posts

See All
bottom of page