Skip to main content
Version: 1.0.6

Smart Assertions

SmartAPI's assertion system combines traditional validation with AI-powered suggestions to ensure comprehensive API testing.

Coming Soon

This feature is currently in development.

Assertion Types​

Status Code Assertions​

Validate HTTP response status:

AssertionDescription
EqualsStatus is exactly 200
In RangeStatus is 2xx
Not EqualsStatus is not 500
// Exact match
pm.test('Status is 200', () => {
pm.response.to.have.status(200);
});

// Range check
pm.test('Success status', () => {
pm.expect(pm.response.code).to.be.within(200, 299);
});

Body Assertions​

JSON Assertions​

// Property exists
pm.test('Has user ID', () => {
pm.expect(pm.response.json()).to.have.property('id');
});

// Value check
pm.test('User is active', () => {
pm.expect(pm.response.json().status).to.equal('active');
});

// Array length
pm.test('Returns 10 items', () => {
pm.expect(pm.response.json().items).to.have.lengthOf(10);
});

// Nested property
pm.test('Has address city', () => {
pm.expect(pm.response.json().user.address.city).to.exist;
});

Schema Validation​

const schema = {
type: 'object',
required: ['id', 'email', 'name'],
properties: {
id: { type: 'integer' },
email: { type: 'string', format: 'email' },
name: { type: 'string', minLength: 1 },
},
};

pm.test('Valid schema', () => {
pm.response.to.have.jsonSchema(schema);
});

Header Assertions​

// Header exists
pm.test('Has content-type', () => {
pm.response.to.have.header('Content-Type');
});

// Header value
pm.test('Returns JSON', () => {
pm.expect(pm.response.headers.get('Content-Type')).to.include('application/json');
});

Performance Assertions​

// Response time
pm.test('Fast response', () => {
pm.expect(pm.response.responseTime).to.be.below(500);
});

// Response size
pm.test('Response under 1MB', () => {
pm.expect(pm.response.size().body).to.be.below(1024 * 1024);
});

AI-Powered Assertions​

Smart Suggestions​

SmartAPI analyzes your API responses and suggests assertions:

Response PatternSuggested Assertion
{"id": 123}Check ID is integer
{"email": "..."}Validate email format
{"created_at": "..."}Check date format
{"items": [...]}Validate array structure

Auto-Generated Tests​

Based on response analysis:

// AI-generated assertions
pm.test('ID is positive integer', () => {
pm.expect(pm.response.json().id).to.be.above(0);
});

pm.test('Email format is valid', () => {
const email = pm.response.json().email;
pm.expect(email).to.match(/^[\w-]+@[\w-]+\.\w+$/);
});

pm.test('Timestamp is ISO 8601', () => {
const date = pm.response.json().created_at;
pm.expect(new Date(date).toISOString()).to.equal(date);
});

Learning Mode​

Enable learning to improve suggestions:

  1. Run requests multiple times
  2. AI learns response patterns
  3. Better assertion recommendations

Assertion Builder​

Visual Builder​

Create assertions without code:

FieldOperatorValue
statusequals200
body.idexists-
body.namenot empty-
body.itemslength >=1

Common Assertions​

Pre-built assertion templates:

TemplateChecks
Success ResponseStatus 2xx, no errors
Created ResourceStatus 201, has ID
Paginated ListHas items, pagination
Error ResponseHas error message
AuthenticatedNo 401/403

Chained Assertions​

Multiple Conditions​

pm.test('Valid user response', () => {
const user = pm.response.json();

pm.expect(user).to.have.property('id');
pm.expect(user).to.have.property('email');
pm.expect(user).to.have.property('name');
pm.expect(user.status).to.equal('active');
pm.expect(user.roles).to.be.an('array');
});

Conditional Assertions​

pm.test('Appropriate response', () => {
if (pm.response.code === 200) {
pm.expect(pm.response.json()).to.have.property('data');
} else if (pm.response.code === 404) {
pm.expect(pm.response.json()).to.have.property('error');
}
});

Error Testing​

Negative Test Cases​

ScenarioExpected
Missing auth401 Unauthorized
Invalid input400 Bad Request
Not found404 Not Found
Rate limited429 Too Many Requests
pm.test('Invalid request returns 400', () => {
pm.response.to.have.status(400);
pm.expect(pm.response.json().error).to.exist;
});

Error Message Validation​

pm.test('Helpful error message', () => {
const error = pm.response.json();

pm.expect(error).to.have.property('code');
pm.expect(error).to.have.property('message');
pm.expect(error.message).to.be.a('string').and.not.empty;
});

Best Practices​

Assertion Guidelines​

  • Test both success and error cases
  • Validate structure and values
  • Include performance checks
  • Use descriptive test names

Coverage​

LevelWhat to Test
BasicStatus codes
Standard+ Body structure
Comprehensive+ Values, headers, performance

Maintenance​

  • Review assertions after API changes
  • Remove obsolete tests
  • Keep assertions focused