Testing with Postman: Reusable Code

Search for a command to run...

No comments yet. Be the first to comment.
In this series I will cover testing with postman and using it for automation.
Sometimes the only thing you need to test is a header. This is when a PDF or something else is returned. Below is how you can test a header. Testing for Correct Headers and PDF Responses Below are test you can run on headers. The great thing about th...
I have spent the last few weeks adding AI agents to my Playwright framework. Specifically Playwright's planner, generator, and healer, running locally through Claude Code. The Playwright agents explor

I decided to do a dump or a one stop shop for learning postman. You can go through my other post or just read this one. Testing Basics So to get started with postman you will need your API url and whether it is a GET, POST, PUT, DELETE or various oth...

Next is to setup a DevOps job you can go here to learn how to do that. For this section we will focus on the Execute shell and newman. With this I suggest going through Postman's official documentation found here. This takes your postman game and ups...

Schema is something you need to test to make sure when they need a string that it is indeed a string. If you google postman and schema checking you will also find a cool tool called tv4. Do not use this as it is out dated and no longer supported. Ple...

Another javascript library built into Postman is Lodash. #Looping Arrays with Lodash Sometimes you will want to iterate through an array. Maybe your response itself is an array. Luckily there is an easy for loop that can be used. This is use for wher...

Since I talked about reusable code in your environmental variable last week lets see how that works this week.
//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
}
}
})
//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
Postman Quick Reference Guide Chai Assertion Library Regex Cheat Sheet Postman: The Complete Guide on Udemy