HTTP Request Tests
Overview
The Tests tab in Martini's HTTP Request Editor lets you write JavaScript-based test scripts that run automatically after each response is received.
Using the martini.test() function with Chai's expect or assert styles, you can assert status codes, validate headers, and verify response body content without leaving Martini Designer.
What You Will Learn
- How to write test scripts using
martini.test()with Chai'sexpectandassertstyles - How test execution works after a request is sent
When To Use This
Use this when you need to:
- Verify that an API endpoint responds as expected
- Check that responses include required headers or metadata
- Validate the structure or content of response data
- Create repeatable API tests directly within Martini Designer
Prerequisites
- Martini Designer installed and running
- At least one HTTP request created and opened in the HTTP Request Editor
Writing HTTP Request Test Scripts
The Tests tab is a JavaScript editor where you define assertions that run automatically each time a request is sent. Tests execute after the response is received, giving you immediate feedback on whether the API behaved as expected.
Getting Started with HTTP Request Tests
To write and run a test script:
- Open the Tests tab in the HTTP Request Editor.
- Write one or more
martini.test()blocks in the editor. Each block defines a test case with a name and one or more assertions. - Click the Send button next to the URL field.
Expected result: All tests run automatically after the response is received; results appear in the Response Panel.
Generate tests with AI
In the Tests editor, click Ask AI and enter a prompt to generate test scripts automatically based on your request and expected response. After generation, review and edit the scripts directly in the editor before sending your request to run them.
Example: Validating Status Code, Content Type, and Body Fields
The following script validates the status code, content type, and specific fields in a JSON response body.
Assume the API returns this response body:
1 2 3 4 5 6 | |
Test script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Expected result: Martini runs all three tests and reports a pass or fail for each in the Response Panel.
How HTTP Request Tests Work
HTTP Request Tests run in a sandboxed JavaScript environment immediately after a response is received.
Each martini.test() block executes independently, so a failure in one test does not prevent other tests from running.
The execution flow is:
- You send the request from the HTTP Request Editor.
- Martini receives the response and populates the
martini.responseobject. - All
martini.test()blocks in the Tests editor execute in the order they are defined. - Results — including pass/fail status and any assertion error messages — are displayed in the Response Panel.
Only the expect and assert styles from the Chai assertion library are supported.
HTTP Request Tests Benefits and Use Cases
The Tests tab removes the need for an external test runner when validating API behavior inside Martini. Writing tests alongside the request configuration keeps assertions close to the context that produced them, making it faster to spot and fix regressions as you develop.
Common use cases include:
- Contract testing — Confirm that an API returns the expected shape and field values for a given input.
- Regression checks — Rerun saved tests after changes to verify nothing broke.
- Status validation — Assert specific status codes for success and error scenarios.
- Body content validation — Verify that returned fields contain expected values or match patterns.
Troubleshooting HTTP Request Test Scripts
| Problem | Detection | Cause | Fix |
|---|---|---|---|
| Test fails with "expected X to equal Y" | Assertion error shown in Response Panel | Actual response value does not match the asserted value | Check the actual response in the Response Panel and update the assertion to match the correct expected value |
| Tests do not run after sending | No test results appear in Response Panel | The Tests tab is empty or the request was not sent | Confirm the Tests editor contains at least one martini.test() block and that you clicked Send |
| All tests pass but output is wrong | Tests pass, but API behavior is unexpected | Assertions are too broad (for example, only checking the status code) | Add assertions that verify the specific fields and values your use case requires |
Helpful Resources
- HTTP Request Editor — Overview of all tabs in the HTTP Request Editor
- HTTP Response Panel — How to view and interpret response data and test results
- Creating Requests — How to create and save HTTP requests in Martini's HTTP Client
- Community Q&A: Martini Community
Have a Question? Post or search it here.