Skip to main content

Requestly uses AI to automatically generate test scripts directly. It eliminates the need to manually write JavaScript test code, making it effortless to validate API responses, assert fields, verify schemas, and catch edge cases with just a few clicks. Simply describe what you want to test using natural language, and the AI generates ready-to-run test cases for to your current API request and response.
AI-Powered Testing: Powered by language models, the AI Test Cases Generator analyzes your API response payload and generates production-ready test scripts in seconds.

Getting Started with AI Test Cases Generator

Step 1: Send Your API Request

The AI Test Generator needs your latest API response to analyse the payload and generate relevant tests.
  1. Open any request in the API Client
  2. Click the Send button to execute the request
  3. Wait for the response to be received

Step 2: Open Post Request Script

Once you have a response:
  1. Navigate to the Scripts section
  2. Click on the Post-response tab
  3. Look for the Generate Tests button and click it
The AI Test Generator dialog will open, ready for your instructions.

Step 3: Enter Your Natural Language Instruction

Describe what you want the tests to validate using plain English. The AI understands context and will generate appropriate test cases based on your instruction and the response payload. Example Instructions:
  • “Generate test cases”
  • “Test that status is 200 and user.email is valid”
  • “Add a schema test for the user list”
  • “Validate pagination fields like page, limit, and total”
  • “Check that success=true and items array is not empty”
  • “Ensure the response has id, name, and email fields”
  • “Verify that all timestamps are valid ISO 8601 dates”
  • “Test for missing required fields in the response”
When ready, click the Generate button.

Step 4: Review the Output

Once the AI finishes generating, it displays a git-style diff view showing exactly what will change in your Post-response script:
  • Red lines show what will be removed
  • Green lines show what will be added
  • No-color lines show unchanged content
This visual representation helps you verify that the generated tests match your expectations.

Step 5: Accept, Edit or Cancel

You have three options after reviewing the generated tests: Click Accept to replace your full Post-response script with the generated tests. The new tests will be immediately available for execution. Click Edit Instruction to refine your original instruction and regenerate. This is useful if:
  • The generated tests are incomplete
  • You want to add more validation points
  • You need different assertions
Click Close to simply close the popover without accepting to discard all changes. Your existing Post-response script will remain unchanged.

What the AI Test Generator Creates

The AI Test Generator creates comprehensive test scripts using Requestly’s rq.test and rq.expect APIs with Chai.js assertions.

Example Generated Tests

For a user API endpoint returning:
{
  "status": 200,
  "success": true,
  "data": {
    "id": 123,
    "name": "John Doe",
    "email": "john@example.com",
    "role": "admin"
  }
}
With instruction: “Test that status is 200, success is true, and user object has required fields” The AI generates:
rq.test("Status is 200", () => {
  rq.response.to.have.status(200);
});

rq.test("Success is true", () => {
  rq.response.to.have.jsonBody("success", true);
});

rq.test("User object has id", () => {
  rq.response.to.have.jsonBody("data.id");
});

rq.test("User object has name", () => {
  rq.response.to.have.jsonBody("data.name");
});

rq.test("User object has email", () => {
  rq.response.to.have.jsonBody("data.email");
});

rq.test("User object has role", () => {
  rq.response.to.have.jsonBody("data.role");
});

Advanced Test Scenarios

The AI Test Generator can handle complex scenarios:

Schema Validation

Instruction: “Validate the entire response structure against a schema” Generates schema validation tests that ensure the response conforms to the expected data structure.
rq.test("Response has valid schema", () => {
  rq.response.to.have.jsonSchema({
    type: "object",
    required: ["status", "success", "data"],
    properties: {
      status: { type: "number" },
      success: { type: "boolean" },
      data: { type: "object" }
    }
  });
});

Tips for Better AI-Generated Tests

  1. Be Specific: Clearly state what you want to test. Instead of “test the response,” say “test that status is 200 and email is a valid email address”
  2. Reference Field Names: Mention specific fields you want validated. The AI understands JSON paths like “user.email” or “data.items[0].status”
  3. Use Examples: Phrases like “similar to POST requests” or “like production data” help the AI understand context
  4. Combine Multiple Concerns: You can ask for multiple validations in one instruction: “Validate status is 200, success is true, and items array is not empty”
  5. Iterative Refinement: Use the “Edit Instruction” feature to progressively enhance your tests

Current Limitations

  • AI Test Generator currently generates Post-response scripts only
  • Requires an actual API response to analyze.
  • Best results when the response has a well-structured JSON format

Combining AI-Generated and Manual Tests

You don’t need to rely entirely on AI-generated tests. You can:
  1. Start with AI: Let the AI generate basic validation tests
  2. Add Custom Logic: Manually add additional test cases or pre-request scripts
  3. Refine Iteratively: Use the Edit Instruction feature to improve generated tests over time
  4. Mix Approaches: Combine AI-generated assertions with custom JavaScript logic

Running and Debugging AI-Generated Tests

After accepting the AI-generated tests, you can:
  • Run Tests: Click the Send button again to execute the request and run all tests
  • View Results: Check the test results in the Test Results panel
  • Debug: Use console.log() in the script to debug test execution
  • Modify: Edit the generated code directly if you need fine-grained control
See Writing Tests for more information about executing and debugging tests.
  • Writing Tests - Learn about manual test writing and all available assertion methods
  • Scripts - Understand Pre-request and Post-response scripts
  • Collection Runner - Run multiple requests and tests in sequence
  • RQ API Reference - Full documentation of the rq object and its methods