TESTING
Test Scripts
JavaScript that runs after the response is received — assert values, extract data, set environment variables, and log debug information.
Purpose
Test scripts are JavaScript that runs after the HTTP response is received. Use them to assert response values, extract data into variables, and log debug information.
Writing Scripts
Open the Scripts tab and write JavaScript in the test script editor (below the pre-request editor).
request (read-only) and response objectsResponse Object
The response object is read-only and provides full access to the HTTP response:
| Property | Type | Description |
|---|---|---|
| response.status | number | HTTP status code (200, 404, etc.) |
| response.statusText | string | Status text ("OK", "Not Found") |
| response.headers | Record<string, string> | Response headers |
| response.body | string | Raw response body text |
| response.json() | any | Parsed JSON (throws if not valid JSON) |
| response.time | number | Response time in milliseconds |
| response.size | number | Response body size in bytes |
Assertions
Use console.assert(condition, message) to create test assertions. Failed assertions appear as test failures in the Results tab.
Example assertions
// Check status code
console.assert(response.status === 200, 'Expected 200');
// Check response body
const data = response.json();
console.assert(data.items.length > 0, 'Should have items');
console.assert(data.total !== undefined, 'Should have total count');
Setting Variables
Extract values from the response and save them to the active environment for use in subsequent requests.
Extract and save a token
environment.set('token', response.json().access_token);
Console Output
console.log() messages appear in the Test Results tab as "Script Logs". Useful for debugging script logic.
Debug logging
console.log('Status:', response.status);
console.log('Body length:', response.body.length);
console.log('Headers:', JSON.stringify(response.headers));
Output is limited to string values. Objects are automatically stringified.