Assertion Syntax
Assertions are written on lines that start with the//# prefix, followed by a three-part expression:
Components
- Key - What part of the response to check (e.g.,
status,header["name"],body) - Operator - How to compare (e.g.,
==,!=,contains) - Value - The expected value to compare against
Assertion Keys
Status Code
Check the HTTP status code of the response:Status Assertions
Check for Error
Headers
Validate response headers using bracket notation with the header name:Header Assertions
Header names in assertions are case-sensitive in the brackets, but HTTP headers themselves are case-insensitive.
Response Body
Check the response body content:Body Assertions
For JSON responses, HTTPSpec currently supports whole-body assertions. JSON path support (e.g.,
body.user.name) is planned for future versions.Assertion Operators
HTTPSpec supports multiple comparison operators. Here’s a quick overview:Operator Reference
| Operator | Aliases | Description |
|---|---|---|
== | equal | Exact equality |
!= | - | Not equal |
contains | - | Contains substring |
not_contains | - | Does not contain substring |
starts_with | - | Starts with prefix |
ends_with | - | Ends with suffix |
matches_regex | - | Matches regular expression (planned) |
not_matches_regex | - | Does not match regex (planned) |
Multiple Assertions
You can include multiple assertions for a single request. All assertions must pass for the test to succeed:Multiple Assertions
Real-World Examples
Successful API Test
User API Test
Testing Error Responses
Error Handling
Mixed Assertions from Test Suite
From the actual HTTPSpec test files:test.httpspec
Assertion Placement
Assertions should appear after the request body (if any) and before the next request:Proper Placement
Comments vs Assertions
Regular comments use# or //, while assertions use //#:
Comments and Assertions
Next Steps
Assertion Types
Complete reference of all assertion operators with examples
Variables
Use variables in assertions for dynamic testing