Skip to main content

Command Palette

Search for a command to run...

Testing with Postman: Reusable Code

Updated
2 min read
Testing with Postman: Reusable Code
B
I build automation frameworks — not just tests. Over 10 years I've designed and shipped production testing platforms across payment, insurance, and healthcare domains. My instinct is always toward architecture: clean abstractions, maintainable patterns, and systems that don't fall apart when the product scales. What I'm working on now: - A Claude/Playwright agent framework that generates test cases directly from Jira tickets - Concurrency and race-condition coverage for financial transactions - A modular Playwright/JavaScript platform covering UI and 20+ API service integrations with 100+ JSON schema validators I've built production automation in Playwright, Geb/Spock, Rest-Assured, and Selenium across payment, insurance, and healthcare domains. When a stack isn't working, I replace it. When a pattern is wrong, I fix it. If the automation problem is interesting, I'm probably already thinking about it.

Since I talked about reusable code in your environmental variable last week lets see how that works this week.

Adding Reusable Code to Your Variable

//Here we create a function that is then stored in the environmental variable. This is so we can then call this from any following test. This can be stored in the pre-req. of your first test in your collection.

postman.setEnvironmentVariable("commonTests", () => {
     //These are test that are ran on every call no matter if it is a positive or negative test.
     pm.test("Response time is below 300ms", function() {
          pm.expect(pm.response.responseTime).to.be.below(300)
     })

      //This is the test we want to run on all positive outcomes.
     //Email enrollment does not have a valid json by design
     var positive = (validJson = true) => {
          pm.test("Status code is 200", function() {
                    pm.response.to.have.status(200)
          })
          pm.test("Response does not error", function() {
               pm.response.to.not.be.error
               pm.response.to.not.have.jsonBody("error")
          })
          if (validJson) {
               pm.test("Response must be valid and have a body", function() {
                     pm.response.to.be.json // this assertion checks if a body exists
               });
          }     };
     //This is for our negative test that we want to fail. In Javascript we can set a default value for our incoming variable. 
     var negative = (code = 400) => {
            if (code === 400) {
                  pm.test("Status code is 400", function() {
                       pm.response.to.have.status(400)
                 })
          } else if (code === 403) {
               pm.test("Status code is 403", function() {
                    pm.response.to.have.status(403)
                })
         } else if (code === 404) {
                  pm.test("Status code is 404", function() {
                       pm.response.to.have.status(404)
                 });
        } else if (code === 500) {
               pm.test("Status code is 500", function() {
                     pm.response.to.have.status(500)
               })
       }
//This could be simpler code of below.
var negative = (code = 400) => {
                 pm.test("Status code is correct", function() {
                       pm.response.to.have.status(code)
//another test to be added
     pm.test("Reports Error", function() {
          pm.response.to.be.error
          })     }
//we return the functions so they can be used outside of the environmental variable.
     return {
          testType: {
                positive,
                negative
         }
     }
})

Using the Code in Test

//for positive test add this line to the top of your test.
eval(environment.commonTests)().testType.positive();

//for negative test add this line to the top of your test.
eval(environment.commonTests)().testType.negative(); 
//Can also use eval(environment.commonTests)().testType.negative(404); for when the code is 404

Links

Postman Quick Reference Guide Chai Assertion Library Regex Cheat Sheet Postman: The Complete Guide on Udemy

More from this blog

B

Ben Weese - Senior QA Engineer

13 posts

As the son of a programmer I grew up on computers and have always loved them. Now I work as a Software Development Engineer in Test to write code and test things. I also enjoy my homelab hobby.