DEV Community

Cover image for Streamline your testing workflow with HOPPSCOTCH CLI ๐Ÿ›ธ
Sanskriti Harmukh for Hoppscotch

Posted on • Updated on

Streamline your testing workflow with HOPPSCOTCH CLI ๐Ÿ›ธ

Hoppscotch 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


Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ To understand better, letโ€™s consider an application 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 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

  • We initially have 3 quotes stored in an array. To GET a quote by specific id, weโ€™ll write a pre-request script ๐Ÿ“,


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


Enter fullscreen mode Exit fullscreen mode

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());


Enter fullscreen mode Exit fullscreen mode

Get a quote

  • Try adding a new quote and making a POST โž• Request,

Add a quote

  • 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

  • Perform DELETE โŽ operation and delete one of the quotes by providing id.

Delete

3๏ธโƒฃ Export ๐Ÿš€ the collection and environment in json format as shown below.

Export the collection

quotes-api.json



{
  "folders": [],
  "v": 1,
  "requests": [
    {
      "body": {
        "body": "",
        "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": ""
      },
      "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": ""
      },
      "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>


Enter fullscreen mode Exit fullscreen mode

5๏ธโƒฃ Following is the test summary ๐Ÿงช of the API requests we made.

Test Summary

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 ๐Ÿ›ธ can make API testing easier. Join our open-source community and stay tuned for future updates.

Top comments (0)