Streamline your testing workflow with HOPPSCOTCH CLI 🛸
Tags: api, testing, opensource, cli
Published: 2023-07-31 09:09:11

[Hoppscotch](https://hoppscotch.io/)  offers a variety of methods to interact with and customize your APIs. In this blog, we'll take you through a hands-on experience of using Hoppscotch CLI to craft and execute API test cases from the comfort of your terminal. So, say goodbye to tedious manual testing and hello to automation magic! 

It’s time to grab your command prompt and dive into the realm of Hoppscotch CLI testing.

1️⃣ Install 📩 Hoppscotch CLI through the following command:
    
```
npm i -g @hoppscotch/cli
```
    
2️⃣ To understand better, let’s consider [an application](https://github.com/SanskritiHarmukh/hoppscotch-cli-demo) that performs basic CRUD operations on quotes. This API provides endpoints to retrieve all quotes, retrieve a specific quote by its ID, add a new quote, update an existing quote, and delete a quote.

- Start off by creating a **GET** ✅ Request to retrieve all the quotes. Create an [environment variable](https://docs.hoppscotch.io/documentation/getting-started/rest/environment-variables) hand in hand for the base URL {http://localhost:5000/quotes} and access in the request URL bar. 
Further add a test case where we define a call back function setting an expectation for the returned response to be 200. This test case will be applied here in all the request methods.

![GET all quotes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk78duhk3ztyvucloqcs.png)


- We initially have 3 quotes stored in an array. To **GET a quote** by specific id, we’ll write a [pre-request script](https://docs.hoppscotch.io/documentation/getting-started/rest/pre-request-scripts) 📝,

```
const randomId = Math.floor(Math.random() * 3) + 1;
```

> where Math.random() returns a random decimal betweem 0 (inclusive) and 1 (exclusive) which when multiplied by 3 is passed to Math.floor() function, further used to round down this number to the nearest integer, effectively converting it to one of the three possible integers: 0, 1, or 2 and then adding 1 to it to generate values as 1,2,3.

To use randomId, we create an environment variable in the script and access it as path to the endpoint in URL bar.

```
pw.env.set('id', randomId.toString());
```


![Get a quote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1442xlo4i3kt3dvaw0m.png)



- Try adding a new quote and making a **POST** ➕ Request,


![Add a quote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gozmov8w8u0nswg3479d.png)


- Update the quote by creating a **PUT** 🔁 Request on the recently added quote by inserting a full stop towards the end of the quote and providing the id as path in the Request Bar.
  


![Update a quote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0kqxl3l6rwl7lj3joii.png)


- Perform **DELETE** ❎ operation and delete one of the quotes by providing id.



![Delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43pxmcleob5dkeh4sj5o.png)


    
3️⃣ Export 🚀 the collection and environment in json format as shown below. 


![Export the collection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsos88fu7z0syld8ahj2.png)




> _quotes-api.json_
> ```
> {
>   "folders": [],
>   "v": 1,
>   "requests": [
>     {
>       "body": {
>         "body": "{\n  \"id\": 3,\n  \"author\": \"Dr. B. R Ambedkar\",\n  \"quote\": \"Cultivation of mind should be the ultimate aim of human existence\"\n}",
>         "contentType": "application/json"
>       },
>       "headers": [],
>       "v": "1",
>       "preRequestScript": "",
>       "name": "Get quotes details",
>       "auth": {
>         "authType": "bearer",
>         "token": "*****************",
>         "authActive": true
>       },
>       "method": "GET",
>       "testScript": "pw.test(\"Response is ok\", () => {\n  pw.expect(pw.response.status).toBe(200);\n});",
>       "params": [],
>       "endpoint": "http://localhost:5000/quotes"
>     },
>     {
>       "name": "get a quote",
>       "preRequestScript": "const randomId = Math.floor(Math.random() * 3) + 1;\npw.env.set('id', randomId.toString());",
>       "testScript": "pw.test(\"Response is ok\", () => {\n  pw.expect(pw.response.status).toBe(200);\n});",
>       "v": "1",
>       "auth": {
>         "authActive": true,
>         "authType": "bearer",
>         "token": "*****************"
>       },
>       "endpoint": "<<base_url>>/<<id>> ",
>       "body": {
>         "contentType": "application/json",
>         "body": "{\n  \"id\": 3,\n  \"author\": \"Dr. B. R Ambedkar\",\n  \"quote\": \"Cultivation of mind should be the ultimate aim of human existence.\"\n}"
>       },
>       "headers": [],
>       "params": [],
>       "method": "GET"
>     },
>     {
>       "headers": [],
>       "testScript": "pw.test(\"Response is ok\", () => {\n  pw.expect(pw.response.status).toBe(200);\n});",
>       "body": {
>         "body": "{\n  \"id\": 4,\n  \"author\": \"Aristotle\",\n  \"quote\": \"We are what we repeatedly do. Excellence, then, is not an act, but a habit\"\n}",
>         "contentType": "application/json"
>       },
>       "v": "1",
>       "endpoint": "<<base_url>>",
>       "preRequestScript": "",
>       "method": "POST",
>       "name": "Post quote",
>       "auth": {
>         "authActive": true,
>         "authType": "bearer",
>         "token": "*****************"
>       },
>       "params": []
>     },
>     {
>       "body": {
>         "body": "{\n  \"id\": \"4\",\n  \"author\": \"Aristotle\",\n  \"quote\": \"We are what we repeatedly do. Excellence, then, is not an act, but a habit.\"\n}",
>         "contentType": "application/json"
>       },
>       "auth": {
>         "authType": "bearer",
>         "authActive": true,
>         "token": "*****************"
>       },
>       "preRequestScript": "",
>       "headers": [],
>       "v": "1",
>       "method": "PUT",
>       "testScript": "pw.test(\"Response is ok\", () => {\n  pw.expect(pw.response.status).toBe(200);\n});",
>       "name": "Update quote",
>       "params": [],
>       "endpoint": "<<base_url>>/4"
>     },
>     {
>       "body": {
>         "contentType": "application/json",
>         "body": "{\n  \"id\": 3,\n  \"author\": \"Dr. B. R Ambedkar\",\n  \"quote\": \"Cultivation of mind should be the ultimate aim of human existence.\"\n}"
>       },
>       "testScript": "pw.test(\"Response is ok\", () => {\n  pw.expect(pw.response.status).toBe(200);\n});",
>       "v": "1",
>       "endpoint": "<<base_url>>/4",
>       "method": "DELETE",
>       "params": [],
>       "auth": {
>         "authType": "bearer",
>         "token": "*****************",
>         "authActive": true
>       },
>       "name": "Delete quote",
>       "headers": [],
>       "preRequestScript": ""
>     }
>   ],
>   "name": "quotes-api"
> }
> ```
And
> _quotes_env.json_
> ```
> {
>   "base_url": "http://localhost:5000/quotes",
>   "id": "1"
> }
> ```

4️⃣ Let’s test the API request using the below command on CLI,

```
hopp test [-e <environment file>] <hoppscotch collection file>
```

5️⃣ Following is the test summary 🧪 of the API requests we made.

![Test Summary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k00o3lm45a21vutolkvj.png)




And that’s it, with a few API calls, you can get the test report 📋 and behaviour of the requests. Execute tests in headless environments and create robust API test suites with Hoppscotch CLI. 

Play around and explore how [Hoppscotch](https://github.com/hoppscotch/hoppscotch) 🛸 can make API testing easier. Join our open-source community and stay tuned for future updates.