09 Assertions
title: "Assertions with pm.expect" description: "Write assertions in Postman using pm.expect and the Chai assertion library. Learn equality checks, type assertions, array validation, string patterns, schema validation, and custom assertion helpers." weight: 9 date: 2026-06-28 lastmod: 2026-06-28 tags: [api-development, postman] }
Postman's pm.expect uses Chai's assertion library for readable, chainable assertions. It supports equality, type checking, inclusion, pattern matching, deep equality, and schema validation. Assertions determine whether a test passes or fails.
What You'll Learn
- Basic equality and type assertions
- String and regex pattern matching
- Array and object assertions
- Deep equality and property checks
- Schema validation with tv4
Why It Matters
Clear assertions make tests readable and maintainable. pm.expect provides descriptive failure messages that help debug failing tests quickly.
Real-World Use
Postman's own test suite uses pm.expect extensively. API monitoring collections use pm.expect for all validation logic. Enterprise API test suites rely on pm.expect for structured assertions.
flowchart LR
pmexpect[pm.expect()] --> Types[Type Assertions]
pmexpect --> Equality[Equality Assertions]
pmexpect --> Strings[String Assertions]
pmexpect --> Arrays[Array Assertions]
pmexpect --> Objects[Object Assertions]
Types --> be[a / an]
Equality --> equal[equal / eql]
Strings --> match[match / include]
Arrays --> length[length / include]
Objects --> keys[keys / property]
Teacher Mindset
Use specific assertions for specific checks. pm.expect(value).to.equal(expected) for exact matches. pm.expect(value).to.be.a('string') for type checks. Use deep equality (eql) for comparing objects.
Code Examples
// Example 1: Type and equality assertions
const user = pm.response.json();
pm.test('User has correct types', () => {
pm.expect(user.id).to.be.a('string');
pm.expect(user.age).to.be.a('number');
pm.expect(user.isActive).to.be.a('boolean');
pm.expect(user.tags).to.be.an('array');
pm.expect(user.address).to.be.an('object');
});
pm.test('User values are correct', () => {
pm.expect(user.name).to.equal('Alice');
pm.expect(user.age).to.be.above(18);
pm.expect(user.age).to.be.below(120);
pm.expect(user.email).to.not.be.empty;
});
// Example 2: String and array assertions
const body = pm.response.json();
pm.test('Email format is valid', () => {
pm.expect(body.email).to.match(/^[\w.-]+@[\w.-]+\.\w+$/);
});
pm.test('Date is ISO format', () => {
pm.expect(body.createdAt).to.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
});
pm.test('Tags array is valid', () => {
pm.expect(body.tags).to.be.an('array').that.has.lengthOf.at.least(1);
pm.expect(body.tags).to.include('featured');
pm.expect(body.tags).to.not.include('banned');
});
pm.test('Response includes pagination', () => {
pm.expect(body).to.have.all.keys(['items', 'total', 'page', 'limit']);
pm.expect(body.items).to.have.lengthOf.at.most(body.limit);
});
// Example 3: Deep equality and schema validation
// Deep object comparison
pm.test('Created product matches input', () => {
const expected = JSON.parse(pm.request.body.raw);
const actual = pm.response.json();
pm.expect(actual.name).to.equal(expected.name);
pm.expect(actual.price).to.equal(expected.price);
pm.expect(actual).to.include.keys('id', 'createdAt');
});
// Schema validation with tv4
const schema = {
type: 'object',
required: ['id', 'name', 'email'],
properties: {
id: { type: 'string' },
name: { type: 'string', minLength: 1 },
email: { type: 'string', pattern: '^[\\w.-]+@[\\w.-]+\\.\\w+$' }
}
};
pm.test('Response matches schema', () => {
const isValid = tv4.validate(pm.response.json(), schema);
pm.expect(isValid).to.be.true;
});
Common Mistakes
- Using to.equal for object comparison (use to.eql for deep equality)
- Not handling null/undefined values in assertions
- Using magic strings instead of constants or regex patterns
- Overusing negative assertions (not.empty, not.null) without specific checks
- Not validating array element types when iterating
Practice
- Write type assertions for string, number, boolean, and array fields.
- Write regex assertions for email and date formats.
- Write array assertions for length, inclusion, and ordering.
- Write deep object comparison assertions.
- Challenge: Write a JSON schema using tv4 and validate complex nested responses.
FAQ
Mini Project
Write a comprehensive test script for a user API response that validates: all required fields exist with correct types, email matches regex pattern, date is ISO 8601 format, tags array includes expected values, response matches a JSON schema, and nested address object has required properties.
What's Next
Next, you will learn about Chai assertion patterns for advanced Postman testing.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro