A Simple API Test Automation Setup Using Newman, GitHub Actions and Slack

Afsal Backer
8 min readAug 28, 2022

In my previous article, we have seen how to set up Selenium and GitHub actions integration to perform UI automation testing. In this article we will explain how we can set up a simple API test automation framework by creating tests in Postman, running these tests in the command line using Newman — a command-line Collection Runner for Postman, set up GitHub Actions Workflow and send test reports to Slack messenger app.

Getting Started — Test Website

We will be using https://reqres.in/ as our API testing website. The website has a detailed explanation of each endpoint, request and the expected response for each request.

The tests we will be running against this website are :

  • LIST USERS
  • SINGLE USER
  • CREATE and
  • DELETE

Test creation on Postman

First, we will create a Collection in Postman. My collection is named ‘Newman Demo’.

Now we will create a new request under Newman Demo collection. This request is to list all users in the database. ReqRes has LIST USERS as a ‘GET’ request with endpoint https://reqres.in/api/users?page=2 . Once the request is sent, the response code should be 200 and it should print a response body listing all users.

Sending the request in Postman gives the below result.

Any test is incomplete without an assertion. For the above test we can assert two things to make sure our test is working fine. One is the response code 200 and the response body by verifying it is returning some Ids.

To do this, we can use Postman scripts to write some tests under the ‘Tests’ tab of the same request.

From ReqRes sample we know what a successful response looks like

It returns a data[] array containing different objects {} in it with id has one of the properties. We can use this to write a test script as shown below.

Running the test will give us the below result.

Great! We have our first test ready.

Similarly, we can write tests for SINGLE USER, CREATE and DELETE by creating new requests for each under the same collection and changing the test scripts a little based on the expected response for each request. You can download the full collection from my GitHub repo.

If we observe the endpoints for all these requests, we can see all endpoints start with https://reqres.in/ . So we can store this as a variable and use it in all test requests to avoid inputing it every time. To do this, we can use Postman Variables and Environments. Environments are a great way of running tests in different test sandboxes of your application and variables can be used to set environment specific data helping us overcome hardcoding issues.

For our example, we will create a new environment named ReqRes Dev and a new variable hostname to store the URL https://reqres.in/

Now we will select the environment from the dropdown on right-top corner, replace the URL with the hostname variable and execute the test.

Test execution was successful. We will do the same for all our test requests.

Now let’s run the entire collection in one go with the ‘Run Collection’ feature.

This will run all the tests in the test suite and give us the result.

Setting up Newman and running tests in the command line.

It’s now time to set up Newman and run the above tests in the command line. First, we start by installing Newman.

Once Newman is installed, to run the tests in our collection, we need to export our collection and the environment as .json files.

Let’s create a new folder named ‘tests’ on my desktop and saved these files inside the folder.

Open the command line tool (Terminal in my case), CD into the ‘tests’ folder we created and execute the below command

newman run NewmanDemo.postman_collection.json -e ReqResDev.postman_environment.json
newman

This will execute all the tests in the collection supplying variables from the environment wherever necessary and give us the below output

To beautify things a little, we can use a custom reporter which can be found here. This reporter gives a dashboard view of the test results. Run

npm install -g newman-reporter-htmlextra to install the reporter and use

newman run NewmanDemo.postman_collection.json -e ReqResDev.postman_environment.json -r htmlextra — reporter-htmlextra-export htmlreport.html — reporter-htmlextra-darkTheme > runreport1.html

command to run the tests again. This will create an html report in the root folder of the project.

Doesn’t that look great?! With that we are all set to configure GitHub Actions.

Setting up GitHub Actions

To set up the GitHub Action workflow, we need a .yml file as YAML syntax is used for workflow files. Let’s first make the root folder containing Postman files a git repo using ‘git init’ command and add a .github>workflows folder inside the root folder. Inside workflows folder we will add a .yml file named ‘postman-api-tests.yml’
This YAML file contains the below code:

We are setting the workflow trigger as ‘workflow_dispatch’. This means it will execute only when triggered manually. Working directory is set to ‘tests’ folder. Tests will be run on an ubuntu machine, Node.js is installed first followed by newman and the the newman reporter. Finally we run the test using newman run command mapping it to the collection and environment .json files.

This workflow when triggered will execute all tests and generate an html report. We will be publishing this report to a Slack channel.

Setting up Slack and publishing the report.

To publish the report to a Slack channel, we will first create a new channel named ‘api-test-reports’.

Next, we need to create a Slack app that GitHub can access using an API token. For that we go to https://api.slack.com/authentication/basics and create a new app from scratch, give it a name and select our Slack workspace.

Within the app page, open ‘OAuth & Permissions’ tab, and under Scopes section click on ‘Add an OAuth Scope’ button. Add the below scopes.

Scroll up the page and click on ‘Install to workspace’ > Allow

Notice that this generates a Bot User OAuth Token. Copy and save this token to use later.

Now open the Slack app. Verify that under Apps section the created app ‘Newman reporter’ is added. Open the channel and add this app to the integrations section.

Next, we need to link the Slack app to GitHub. For that create a new repository in GitHub and open it’s Settings.

Navigate to Secrets > Actions > New repository secret.

Give ‘SLACK_TOKEN’ as the name and paste Bot User OAuth Token we copied earlier in the value field and save. GitHub is now connected to our Slack app.

To publish the report to Slack after execution, we will add one more step to our .yml file.

This simply means we are using the slack token we added in GitHub to connect to the slack channel ‘api-test-reports’ and use a file upload action template to upload the report. Report name will be ‘postmanReport’ and type will be html.

With that, everything is done. We can now push all the code to the repository we created earlier.

Next, go to Actions and run the workflow.

This will run all tests

and once the job is completed the result will be posted in our Slack channel.

Conclusion

In this article, we saw how to build a small API test automation setup using Newman, GitHub actions and Slack app. Combining this with your existing test automation framework will help your team tremendously. If you have some crucial API tests, changing the trigger to on deploy to master will warn the developers immediately if something doesn’t work right after deployment.

I hope you found this useful, if you have any questions please let me know and if you liked the article give it a like and share!

--

--