Skip to main content
Join the Agentforce Hackathon on Nov. 18-19 to compete for a $20,000 Grand Prize. Sign up now. Terms apply.

Test Responses

Learning Objectives

After completing this unit, you’ll be able to:

  • Insert a test snippet to assert a response.
  • Save a request to a collection.

Insert a Test Snippet

You can write Postman tests in JavaScript to ensure an API is functioning as expected. Under the Tests tab of your request, you can input JavaScript manually, use artificial intelligence to generate tests, or use the SNIPPETS list to the right of the code editor. Clicking one of these popular use cases automatically inserts a bit of code into the editor for you to modify.

  1. With your request open, click Tests.
  2. In the SNIPPETS list, find and click Status code: Code is 200 to insert the snippet into the code editor.
    Test tab is open and Status code: Code is 200 test script link is highlighted

 A Postman test consists of the pm.test() function, which accepts two parameters:

  • The name of the test used to identify it in the Test Results output
  • A function that returns a Boolean (true or false) value to indicate whether the test passes or fails

In our example, the name of our test is "Status code is 200". The function contains an assertion about our response, that we will receive an HTTP status code of 200, formatted using Chai BDD syntax.

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

Run a Test

Since tests are assertions about a server response, tests execute after you run the request and receive a response from the API. 

  1. With your test added to the request, click Send to run the request.
  2. Open Test Results in the response section at the bottom.
    Test Results open and the pass response PASS Status code is 200

If the call returns a 200 HTTP status code, the test will pass, otherwise it will fail. Try changing the expected status code in your test script to 404, and run the request again. You’ll see that the Test Result gives you a fail and error message.

Save the Request to a Collection

The History tab on the left sidebar maintains a record of the most recent requests you send from Postman. Combing through your history to find and reuse requests can be time-consuming. Instead, you can save a collection of requests to organize your work. Besides improving productivity, collections are the foundation of more advanced features in Postman, like automation, monitoring, and mock servers.

  1. Click history
  2. Hover over the first request you made and click Add request to Collection (+). This should be the request you made before adding a date parameter.
    List of requests with the first request hovered over and Add request to Collection message
  3. Name your collection Trailhead module
  4. Click Save. Your request is saved to your new collection.
  5. Hover over your second call and click Add request to Collection (+).
  6. The destination should already be Trailhead module. Go ahead and click Save.

Update the name of the first call to Picture of the Day and the name of the second call to Pictures from November 2023.

  1. Click Collections.
  2. Expand the Trailhead module collection.
  3. Hover over the top request and click more actions icon depicted by three dots
  4. Click Rename.
  5. Enter Picture of the Day
  6. Rename the second request in your collection Pictures from November 2023.
    My First Workspace collection Trailhead module with Picture of the Day and Pictures from November 2023

Now, let's ensure you're actually requesting all of the pictures from November 2023. 

  1. Click Pictures from November 2023.
  2. Hover over the date key you created and click the trash can icon to delete it.
  3. Enter a new key to start your retrieval array on the first day of November.
    • KEY: start_date
    • VALUE : 2023-11-01
  1. Enter another key to conclude the retrieval array on the last day.
    • KEY: end_date
    • VALUE : 2023-11-30
  1. Save your work.
  2. Click Send to run the request.

Check out the NASA Astronomy Picture of the Day API documentation for more information about its query parameters.

Keep It DRY

DRY stands for don’t repeat yourself. Imagine you always expect JSON objects to be returned from all your endpoints. Instead of adding the same test to every request individually, you can add the test assertion at the collection level to run after every request in the collection. With Postman, you can add tests to individual requests, folders, or collections.

  1. Click Trailhead module.
  2. Click the Tests tab.
  3. Under SNIPPETS, select Status Code: Code is 200 to add the test at the collection level.
  4. Click Save.
  5. Next, click Run collection Run collection
  6. Then, click Run Trailhead module. After the collection runs, you can see that each request has passed under the All Tests tab.
    Trailhead module collection showing All Tests and all requests with a Pass status

In the next unit, let’s learn about another way to stay DRY, using variables to reduce redundant code.

Resources 

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback