Solution for Chapter 9: Testing APIs

Design and Build Great Web APIs — by Mike Amundsen (120 / 127)

The Pragmatic Programmers
The Pragmatic Programmers
5 min readApr 28, 2021

--

👈 8: APIs | TOC | 10: APIs 👉

In the exercise for this chapter (Chapter 9, Testing APIs), your task is to create a Postman test collection for your credit-check API project and write two tests: a “happy path” test for the Home resource (http://localhost:8181/) and a “sad path” test for the From resource (http://localhost:8181/form/). You were supplied with extensive instructions for each test.

For extra credit, you were given the task to export the completed tests and use the newman command-line utility to run the tests locally.

Note: Before you start this exercise, make sure your local version of the credit-check API that you built in the last chapter is up and running. You can use the copy of that service in the /testing/exercises/before/credit-check folder of the source code associated with this book.

Create the Credit-Check Collection in Postman

First, we need to create a new test collection in Postman. Let’s name it “Credit-Check.” In the Postman client, click the “+ New Collection” link in the left sidebar, enter “Credit-Check” in the Name field, and then press the Create button in the lower-right corner of the dialog box.

Add the Home Test

Click the three dots near the collection name and pick Add Request from the drop-down menu. Enter Home as the Request Name and press the Save button. Next, click the Home test in the left sidebar to bring it up to the main editor view.

Now we can add the Request URL. Enter “localhost:8181/” and press the Send button. You should get a response body back that looks like this:

Write the Happy-Path Home Test

Now we’re ready to write our test script. Click the Tests tab underneath the Request URL field to bring up the blank editor and enter the following script:

(Remember, you can find all the source code for this solution in the code folder (code/testing/exercise/) associated with this chapter.)

Save the test and press the Send button again to confirm all the tests pass.

Create the Form Test

To add our next test, click the three dots near the collection name and pick Add Request from the drop-down menu. Enter “Form (missing properties)” as the Request Name and press the Save button. Next, click the FORM test in the left sidebar to bring it up to the main editor view.

Now we can add the Request URL. Enter “localhost:8181/” and change the GET option from the drop-down menu next to Request URL to POST. Then press the Send button. You should get a response body back that looks like this:

Write the Sad-Path Form Test

Now it’s time to write our sad-path test to confirm that the API rejects any attempts to add a company record when required fields are missing. Using the test rules from the exercise as a guide, your results should look similar to the script that follows. (A copy of this script is available in the /testing/exercise/before/ folder in the source code for this book.)

After adding the tests to the Postman client, press the Save button and then Send to confirm all the tests pass.

Export the Test Collection

To export your test collection from Postman onto your local drive, click the three dots near the collection name in the Postman UI sidebar and select Export from the drop-down menu. Make sure the “Collection v2.1” radio button is selected and press the Export button that appears in the lower-right corner of the dialog box. Then save the collection to your local drive. The default name for your collection will look something like Credit-Check.postman_collection.json.

Run the Exported Tests with Newman

Finally, you can run the exported test collection with the newman utility by opening up a command window and navigating to the folder that holds your exported collection document. Then type the following:

This will execute both your Home and Form tests and write out an HTML report that will appear in the code/testing/newman/ folder.

That wraps up the testing portion of our project. Next, you’ll be ready to add security features and then deploy the API to a public server.

👈 8: APIs | TOC | 10: APIs 👉

Design and Build Great Web APIs by Mike Amundsen can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

Unlisted

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.