Getting Started with Postman BDD API Testing

Ganesh Hegde
2 min readJan 24, 2019

--

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');

If you find this article is helpful please post it on LinkedIn tagging my name.

👉 Please encourage me to write more articles Buy me a Coffee

--

--

Responses (2)