Streamline your testing workflow with HOPPSCOTCH CLI 🛸
Sanskriti Harmukh
Posted on July 31, 2023
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
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.
- 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;
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());
- Try adding a new quote and making a POST ➕ Request,
- 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.
- Perform DELETE ❎ operation and delete one of the quotes by providing id.
3️⃣ Export 🚀 the collection and environment in json format as shown below.
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>
5️⃣ Following is the test summary 🧪 of the API requests we made.
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.
Posted on July 31, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.