Getting Started with Postman BDD API Testing

Ganesh Hegde
Jan 24 · 3 min read

Postman BDD is one of the great and easy way to write API Tests. Earlier postman BDD was supported by third-party .js libraries, however, the third-party library is now deprecated as Postman came up with its own BDD.

Let's get started with basics

Postman BDD test starts with pm.test(), the syntax looks like below.

pm.test("Response should be 200", () => {
// your Assertions
});

Variables in Postman

Global Variables

//Set Global Variable
pm.globals.set('VariableName', VALUE);
//Get Global Variable
pm.globals.get('VariableName');
//Clear Single Global Variable
pm.globals.unset('VariableName');
//Clear All Global Variable
pm.globals.clear();

Local Variables

//Set Value to Local Variable
pm.variables.set('VariableName', 'Value');
//Get Value from Local Variable
pm.pm.variable.get(‘VariableName’);
pm.variables.set('VariableName', 'Value');

Environment Variable

//Set Environment Variable
pm.environment.set('envVariable', VALUE);
//Get Environment Variable
pm.environment.get('envVariable');
//Remove Single Environment Variable
pm.environment.unset('envVariable');
//Remove All Environment Variable
pm.environment.clear();
Example:
pm.environment.set('myName', 'Ganesh');

Working with a Response Data

You can create a Get Request using postman, Once you send the request you will get either text, xml, Json as Response Body. Below example illustrates how to use the response data

//JSON Response
var response = pm.response.json();
//Text Response
var response = pm.response.text()
//XML Response
var jsonObject = xml2Json(responseBody);
//Choosing First Response from array of JSON
var userdata= pm.response.json()[0];

Assertions in Postman

Though postman supports many assertions libraries most popular one is chai assertions library.

Click here to know more about chai assertion

Asserting for Equal

//Asserting Equality in your API Test
pm.test('Your Test Name', function() {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
}

Asserting Datatype

//Asserting Data Type
pm.test('Your Test Name', function() {
var jsonData = pm.response.json();
pm.expect(response.id).to.be.a(‘number’);
pm.expect(response.name).to.be.a(‘string’);
pm.expect(response.isDeleted).to.be.a(‘boolean’);
}

Asserting Length

//Asserting Length
pm.expect(pm.response.json().length).to.be.greaterThan(3401);

Asserting Status code and Response

// Asserting Status and Responsepm.response.to.have.status(200);
pm.response.to.be.not.empty;
pm.response.to.be.json;

Asserting OR status code

// Asserting Either this or that Status
pm.expect(pm.response.code).to.be.oneOf([201,202]);

Asserting Response Time

// Asserting Response Time
pm.expect(pm.response.responseTime).to.be.below(9);

Matchers

pm.response.to.have.body("OK");
pm.response.to.have.body('{"success"=true}');
pm.expect(pm.response.text()).to.include('Order placed.');

Header Verification

pm.response.to.have.header(X-Cache');

Verify Header Value

pm.expect(pm.response.headers.get('X-Cache')).to.eql('HIT');

Verify cookies

pm.expect(pm.cookies.has('sessionId')).to.be.true;

Verify Cookie value

pm.expect(pm.cookies.get('sessionId')).to.eql(’asdf3333sdf');

You might also like …

Ganesh Hegde

Written by

https://www.linkedin.com/in/ganeshsirsi/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade