Skip to content

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's expect and assert styles
  • 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

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:

  1. Open the Tests tab in the HTTP Request Editor.
  2. Write one or more martini.test() blocks in the editor. Each block defines a test case with a name and one or more assertions.
  3. 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
{
  "id": 1,
  "name": "Alice",
  "email": "alice@example.com",
  "role": "admin"
}

Test script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
martini.test("Status should be 200", () => {
    expect(martini.response.status).to.equal(200);
});

martini.test("Content type should be JSON", () => {
    expect(martini.response.contentType).to.include("application/json");
});

martini.test("Response body should contain expected user data", () => {
    const body = JSON.parse(martini.response.body);
    expect(body).to.have.property("id").that.equals(1);
    expect(body).to.have.property("name").that.equals("Alice");
    expect(body).to.have.property("email").that.equals("alice@example.com");
    expect(body).to.have.property("role").that.is.a("string").and.is.not.empty;
});

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:

  1. You send the request from the HTTP Request Editor.
  2. Martini receives the response and populates the martini.response object.
  3. All martini.test() blocks in the Tests editor execute in the order they are defined.
  4. 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