REST API Testing with Bruno
Introduction
Bruno is a new and innovative open-source API client, aimed at revolutionizing the status quo represented by popular API testing tools out there. Bruno stores the collections directly in a folder on your filesystem. Bruno uses a plain text markup language, Bru, to save information about API requests. Bruno is lightweight, it supports REST and GraphQL APIs. One can use git or any version control of your choice to collaborate over your API collections. Please download Bruno from here.
Why should Bruno be preferred for API Testing?
How are collections saved?
Bruno uses a plain text markup language, Bru to save information about API requests instead of using a giant JSON file. Git or any version control of your choice can be used to collaborate over API collections. No more horror stories of “Bob had the collections for the payment-API service, but he left the company last month.”
Where collections are saved?
Bruno stores your collections directly in a folder on your filesystem instead of the cloud.
How are API Requests made?
Bruno makes API requests directly from your computer. You are in control of your data. There is no proxy server involved which is a huge security risk.
Online vs Offline
Bruno is a desktop app and is made for offline use.
Team Collaboration
Bruno is free and open source. Git or any version control of your choice can be used to collaborate over your API collections.
Collection Runs
With Bruno, one can run collections infinite times.
Loading NPM Modules
With Bruno, one can use package.json to install the npm modules of your choice, and can require them inside your scripts like how developers normally would.
Declarative Scripting
Bruno supports declarative scripting to update your post-response variables, by using a simple expression. To learn more, please see here.
Declarative Assertions
Bruno supports declarative assertions to write tests, by using simple expressions. To learn more, please see here.
Creating collections in Bruno
It is always a good practice to create a collection and add requests under the collection. To create a collection first click on Create Collection button.
Enter a name for the collection, add a Folder Name and then select a location from your filesystem using Browse option and click on Create button.
The new collection created will be displayed under the Collections section.
Adding the first request under the Collection
In this blog I will be using REST APIs provided by reqres.in. I have used the CREATE USER API that supports the HTTP POST method.
Click on the three dots displayed against the collection name and click on New Request.
In the NEW REQUEST window, select Type as HTTP, then give a name to Request and change the HTTP method to POST using the drop-down and click on Create.
The new request will be displayed under the newly created collection.
Now click on the Create User request, then select the Body tab. After that from the No Body drop-down select JSON as the value.
Paste the JSON payload in the Body tab and save the request details using Ctrl+S. Then click on Send button and verify the response.
Creating an environment and adding variables under it
Click on the No Environment drop-down and click on the Configure option.
From the ENVIRONMENTS window click on the Create Environment button.
From the CREATE ENVIRONMENT window, add an Environment Name and click on the Create button.
After clicking the Create Button, the environment will get automatically changed to the newly created environment and the environment will be displayed under the ENVIRONMENTS window. To add the variable under the environment click on the Add Variable button.
After clicking the Add Variable button, add Name as well as Value and click on the Save button.
Accessing environment variable
To access the environment variable use {{variableName}}
object in the request section and also make sure that the correct environment is selected from the top right corner for the request. Then click Send and the same result will be displayed as earlier.
Adding Assertions for the request
Click on the Assert tab and click on Add Assertion button.
After that add the expression under column Expr, choose an Operator value from the dropdown and finally add the response value under the Value column.
For the request, I have added assertions to check the status code value, name and job. For asserting the status code use the expression res.status and to assert any response key use the expression res.body.key. Then save the request using Ctrl+S. Finally, click on Send button and check the assertions in the Tests tab. The Tests tab will show the count of passed assertions in Green color along with details of Assertions. In case of failures, the Tests tab will show the count of failed assertions in Red color along with details of Assertions. You can find the Assertions documentation from this link.
Now I am going to intentionally fail the assertions by giving wrong response values. After that will be clicking the Send button and will be verifying the results in the Tests tab. The Tests tab will display the reasons for the failure of assertions along with count of failed Tests in red color.
Adding Tests for the request
Bruno supports writing automation test scripts in JavaScript for your API requests to assert its behavior. Click on the Tests tab and add the following Tests shown in the screenshot. Then click on the Send button and verify the results in the Tests tab. The tests tab will show the result of Tests and Assertions added for the request. You can find the Tests documentation from this link.
Now I am going to intentionally fail the Tests by giving the wrong response values. After that will be clicking the Send button and will be verifying the results in the Tests tab. The Tests tab will display the reasons for the failure of tests.
Running collection using Bruno CLI
First, make sure Node.js is already available in the system. To verify it, use the command node -v in the command prompt. If Node.js is not present please install it from this link:https://nodejs.org/en/download. To install Bruno CLI use the following command:npm install -g @usebruno/cli
Now go to the location where the collection is saved and open the command prompt from that location. After that use the following command to execute the collection with the environment:
bru run — env “Reqres Env”
Please make changes based on your collection names and the environment. Please go through the blog on Bruno CLI.
If you need any clarifications regarding this post, please comment below.
Comments
Post a Comment