Smart Assertions
SmartAPI's assertion system combines traditional validation with AI-powered suggestions to ensure comprehensive API testing.
bilgi
Coming Soon This feature is currently in development.
Assertion Types
Status Code Assertions
Validate HTTP response status:
| Assertion | Description |
|---|---|
| Equals | Status is exactly 200 |
| In Range | Status is 2xx |
| Not Equals | Status 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 Pattern | Suggested 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:
- Run requests multiple times
- AI learns response patterns
- Better assertion recommendations
Assertion Builder
Visual Builder
Create assertions without code:
| Field | Operator | Value |
|---|---|---|
| status | equals | 200 |
| body.id | exists | - |
| body.name | not empty | - |
| body.items | length >= | 1 |
Common Assertions
Pre-built assertion templates:
| Template | Checks |
|---|---|
| Success Response | Status 2xx, no errors |
| Created Resource | Status 201, has ID |
| Paginated List | Has items, pagination |
| Error Response | Has error message |
| Authenticated | No 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
| Scenario | Expected |
|---|---|
| Missing auth | 401 Unauthorized |
| Invalid input | 400 Bad Request |
| Not found | 404 Not Found |
| Rate limited | 429 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
| Level | What to Test |
|---|---|
| Basic | Status codes |
| Standard | + Body structure |
| Comprehensive | + Values, headers, performance |
Maintenance
- Review assertions after API changes
- Remove obsolete tests
- Keep assertions focused