API Testing Using Postman For Beginners
Tags: api, testing, beginners
Published: 2023-06-26 14:13:49

API testing is an essential task for backend engineers, whose primary responsibility is to manage application APIs.

[The Test Tribe](https://www.thetesttribe.com/) released an in-depth course on API Testing using Postman.

The course teaches you:

- Everything you need to know about APIs.

- How to make the most out of Postman.

This article will share what I've learned from the course.

## What is an API?

API stands for Application Programming Interface. APIs are how two software systems (usually a client and a server) communicate.

An everyday example of API is the PayPal API.

Many online stores give you the option to purchase an item using PayPal.

The website doesn't have your bank details. These would be stored in your PayPal account for security purposes. The website communicates with PayPal via an API.

## What is API testing?

API testing is a [form of testing](https://techwithmaddy.com/3-types-of-software-testing-for-developers) used to validate whether an API works as expected.

API testing involves creating code using the API, hitting an endpoint by sending an [HTTP request](https://techwithmaddy.com/java-interview-question-what-is-http), and verifying that the response is what you expect.

You perform API testing **_before_** your code goes into production.

### Use-case example

You want to verify that you get a status code once an item has been deleted.

Therefore, you create a `DELETE` request by inserting the item you deleted as part of the request, and you expect to get back a `404 Not Found` status code.

## JSON

JSON is the main format used to transmit data over the network. JSON stands for JavaScript Object Notation, and it looks like this:

```
{
    "name":"Lauren",
    "city": "Melbourne",
    "hobby": "Singing"
}

```

## Why do you need to understand API Testing?

Backend engineers mostly do API testing.

An advantage of API testing is that you don't need the frontend to test an API.

You simply need backend working code and tools such as Postman or Swagger to test your API.

When you test an API, you catch potential bugs before they reach production.

## What is Postman?

Postman is an API testing tool that allows you to set a type of request, URL and body of a request to test a specific endpoint.

## How do you perform API testing using Postman?

To perform API testing using Postman, you must install Postman first.

> Read [Installing and updating Postman](https://learning.postman.com/docs/getting-started/installation-and-updates/) to install Postman on your local machine.

[![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687698200342/20ffc192-09ac-4677-9b9c-c7abcb92961b.jpeg)](https://www.postman.com/)

### Create a collection

Under **My Workspace** :

- Select **Collections** > **+** > **Create new collection**.

- Name your collection as **Your\_First\_API\_Testing**.

### Send a GET request

- Once you create your collection, select the **\>** , and then **Add a request.**

- Choose **GET**.

- Copy and paste this URL <mark>https://reqres.in/api/users?page=2</mark>

- Select the **SEND** button.

You should see the following result:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687700175660/48799476-86d4-4721-89cc-6d2892fe6640.jpeg)

In this case, the GET request retrieves an array of users on page 2 whose details are the id, email, first\_name, last\_name, and avatar.

### Send a POST request

To send a POST request:

- Select **POST** instead of GET.

- Copy and paste this URL: <mark>https://reqres.in/api/users</mark>

- Select **Body** \> **raw** radio button > **JSON** from the drop-down list.

- Enter the below JSON:

```
{
    "name": "Lauren",
    "job": "Singer"
}

```

You should expect the following result:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687701367071/9f2def08-99e1-43fa-9988-a231931df13c.jpeg)

## Advanced Postman features

### Dynamic variables

Instead of manually entering data, Postman allows you to generate data, such as names, job titles, and more.

Use the `$` to see the list of dynamic variables Postman offers.

```
{
    "name": "{{$randomFirstName}}",
    "job": "{{$randomJobTitle}}"
}

```

Select **SEND**. You should see a randomly generated first name and job title.

[![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687702252010/37d102b4-d601-4041-80bd-423a15dd931b.jpeg)](https://learning.postman.com/docs/writing-scripts/script-references/variables-list/)

### Pre-request scripts

If you want to run a piece of code before execution, Postman allows you to create pre-request scripts:

- Select Pre-request Script.

- Add the following snippet:

```
pm.environment.set("Fullname", pm.variables.replaceIn('{{"$randomFullName"}}'));

pm.environment.set("Job Title", pm.variables.replaceIn('{{"$randomJobTitle"}}'));

```

Select **SEND**. The response should generate a randomly generated Full name and Job Title.

### Test scripts

Test scripts are executed once a response has been received.

On Postman, select **Tests** > Snippets > **Status code: Code is 200**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687710046612/5f90eb51-2bad-4b5c-b8b7-42e04ad81efc.jpeg)

For a POST request, the expected response status is `201 Created`.

Amend the JavaScript to reflect the expected response code:

```
pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});

```

Select **SEND**. Your test should display a PASS.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687710183263/850aa112-50b6-4465-b968-59535b0bfd46.jpeg)

## About The Test Tribe

[The Test Tribe](https://www.thetesttribe.com/) is a testing community that offers several resources such as courses, memberships, conferences, bootcamps, and more.

The course is split into several modules, with a quiz at the end of each module.

Throughout each module, you're accompanied by an instructor who will guide you through the instructions. The videos are pre-recorded.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687711399871/84c53411-99dc-4013-a0d1-c5be962a9c59.jpeg)

## Conclusion

After reading this article, you know how to:

- Retrieve data using a GET request.

- Create data using a POST request.

- Create pre-request scripts.

- Validate test cases after the response has been received.

If you enjoyed this article, you'd benefit from subscribing to my [FREE weekly newsletter](https://techwithmaddy.com/newsletter).

### Resources:

- [The Test Tribe Community](https://www.thetesttribe.com/thrive-edschool/)