DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Agile: The Forgotten Parts
  • DevOps Best Practices
  • Test Automation Guilds: Advancing Excellence in Testing
  • The Naming Nuance of Non-Functional Requirements

Trending

  • Effective Communication Strategies Between Microservices: Techniques and Real-World Examples
  • Power BI: Transforming Banking Data
  • Navigating the AI Renaissance: Practical Insights and Pioneering Use Cases
  • Scaling Java Microservices to Extreme Performance Using NCache
  1. DZone
  2. Culture and Methodologies
  3. Agile
  4. Using Postman Snippets

Using Postman Snippets

Let's take a quick look at how Postman snippets can help us get started with scripting assertions to support REST API Testing.

By 
Alan Richardson user avatar
Alan Richardson
·
Updated Oct. 30, 23 · Tutorial
Like (16)
Save
Tweet
Share
26.8K Views

Join the DZone community and get the full member experience.

Join For Free

Scripting Using Snippets in Postman

Postman supports our manual interactive testing of REST APIs by allowing us to create requests, send them to the API and then we can look at the response to see if the API call worked as expected. DZone’s previously covered how to secure REST APIs with client certificates.

A lot of tools do that.

Postman Benefit: Scripting for Assertions

One of the benefits of using Postman is that it allows us to write JavaScript code, which can assert the responses and automatically check the response.

This code checks that the status of the message is 200, meaning Success.

Test Results Automatically Reported

And when I send the request, the code will run and the results will be shown in the Test Results tab.

How to Use This

This can be used to:

  • support exploratory testing by asserting on common assertions
  • move toward more automated validation of your application by using the Postman runner. To create a set or requests which are automatically sent and the assertions reported on.

What Does This Post Cover?

I will use the snippets to create a script with no coding knowledge:

  • create a basic GET Request
  • use the Snippets feature of the Tests tab to write assertions
  • run the assertions in the GUI

This post has a supporting video that you can find here, or embedded in the post below.

First Create a Request

I'll create a simple request to support this tutorial.

a GET http://localhost:4567/todos request on my test application returns a JSON response with two todo items in it.

This is hardcoded data in the application so it will always be there when the application first starts up.

In the Real World

If I do this in the real world I will:

  • Check the Status Code
  • Check the content type
  • Make sure the body is valid JSON

Check other conditions based on the state of the application.

I Can Automate These Conditions Without Coding

I can create assertions in Postman using the snippets functionality without knowing much about how to code.

Check the Status Code

When I have the "Tests" tab selected.

I can use the snippets on the side to have Postman automatically add code into my Tests.

In the snippets, I can see "Status code: Code is 200", when I click on that Postman will add code into my test.

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

We can add this test without needing much coding knowledge, but we do have to learn to read the code.

If I read this it means.

Have a postman test call "Status code is 200", this is a JavaScript function and it will expect the postman response to have status code of 200.

I can use this, without amending the code, and Postman will assert that the status code is 200.

With any test we really want to make sure it fails. When if I amend the GET to a URL that doesn't exist e.g. /todosNOT

We should receive a 404 from the application and the Test Results will be red and show us the number of tests which have failed, and we have only added one. And we can see it has failed.

Related DZone Tutorial: How to Secure REST API Endpoints with Spring Security and OAuth2.

Assert on Content Type

I can add a basic content type header check without coding, by selecting "Response header: content-type header check" from snippets.

pm.test("Content-Type is present", function () { pm.response.to.have.header("Content-Type");});

And if we read it it says.

Have a postman test call "Content-Type is present", this is a JavaScript function and it will expect the postman response to have a header of type "Content-Type".

But this doesn't assert on the value of the header so I can't check that it is "application/json" as we are expecting.

I need to write some code to check this.

I can add a line into the test:

pm.expect(pm.response.headers.get("Content-Type")).to.eql("application/json");

Snippets Can Help

Snippets can help you get started with assertions in Postman.

The assertions are useful when you are performing exploratory testing and want to have something monitor invariant assertions as you test. i.e. stuff you expect to always be true for the testing you are performing. For further reading, DZone’s previously covered how to secure your REST API with Keycloak.


Snippet (programming) Testing application Assertion (software development) agile

Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Agile: The Forgotten Parts
  • DevOps Best Practices
  • Test Automation Guilds: Advancing Excellence in Testing
  • The Naming Nuance of Non-Functional Requirements

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: