Advanced API Assertions
Learn advanced assertion techniques to create comprehensive and robust API validations in Maeris.
Overview
Advanced assertions allow you to create complex validations using scripts, custom logic, JSON path expressions, and sophisticated comparison operators. These techniques enable you to validate complex scenarios that basic assertions cannot handle.
JSON Path Assertions
JSON Path allows you to extract and validate specific values from complex nested JSON structures.
Common JSON Path Expressions
Assertion Examples
Script-Based Assertions
For complex validation logic, you can write custom scripts using JavaScript to perform sophisticated checks.
Script Assertion Examples
Schema Validation
Validate that responses match a JSON schema, ensuring data structure and types are correct.
Schema Validation Example
Assert that the response matches this schema to ensure data integrity.
Conditional Assertions
Create assertions that only run under certain conditions, allowing for flexible validation logic.
Conditional Examples
Array and Collection Assertions
Validate arrays and collections with advanced checks like sorting, uniqueness, and element validation.
Performance Assertions
Validate API performance characteristics beyond simple response time checks.
- Assert response time is within acceptable range
- Assert response size is reasonable
- Assert number of redirects is minimal
- Assert connection time is fast
- Compare response times across requests
Best Practices
- Start Simple: Use basic assertions when possible, only use advanced techniques when needed
- Document Complex Logic: Add comments to explain complex assertion logic
- Reuse Assertions: Create reusable assertion scripts for common patterns
- Test Assertions: Verify that your assertions work correctly before relying on them
- Handle Edge Cases: Consider null values, empty arrays, and missing fields
- Performance: Keep assertion scripts efficient to avoid slowing down tests
- Error Messages: Use descriptive error messages in custom assertions
Next Steps
- API Test Results and Analysis - Understand assertion results
- API Performance Metrics - Monitor API performance
- API Assertions and Validations - Review basic assertions