Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@
{
"group": "Scripts & Tests",
"pages": [

{
"group": "Pre-request & Post-response Scripts",
"pages": [
"general/api-client/scripts",
"general/api-client/import-packages-into-your-scripts",
"general/api-client/tests",

"general/api-client/ai-test-generator"
]
},
"general/api-client/import-packages-into-your-scripts",
"general/api-client/tests",
{
"group": "API Reference (rq)",
"pages": [
Expand Down
211 changes: 211 additions & 0 deletions general/api-client/ai-test-generator.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
---
title: Generate Test Cases using AI
label: Generate Test Cases using AI
slug: generate-test-cases-using-AI
description: >-
Learn how to automatically generate Post-response test scripts using AI in Requestly. Use natural language instructions to validate API responses, assert fields, verify schemas, and catch edge cases without writing JavaScript manually.

Check warning on line 6 in general/api-client/ai-test-generator.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/ai-test-generator.mdx#L6

Did you really mean 'Requestly'?
visibility: PUBLIC
---

***

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.

Check warning on line 12 in general/api-client/ai-test-generator.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/ai-test-generator.mdx#L12

Did you really mean 'Requestly'?

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.

<Tip>
**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.
</Tip>


## 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

<img src="/images/api-client/ai/test-case-generator/step-1-send-your-api-request.png" align="center" fullwidth="true" />

### 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.

<img src="/images/api-client/ai/test-case-generator/step-2-open-post-request-script.png" align="center" fullwidth="true" />


### 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"

<img src="/images/api-client/ai/test-case-generator/step-3-enter-your-natural-language-instruction.png" align="center" fullwidth="true" />

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.

<img src="/images/api-client/ai/test-case-generator/step-4-review-the-output.png" align="center" fullwidth="true" />


### 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`](/general/api-client/rq-api-reference/rq-test) and [`rq.expect`](/general/api-client/rq-api-reference/rq-expect) APIs with [Chai.js assertions](/general/api-client/tests#example-tests).

Check warning on line 93 in general/api-client/ai-test-generator.mdx

View check run for this annotation

Mintlify / Mintlify Validation (requestly) - vale-spellcheck

general/api-client/ai-test-generator.mdx#L93

Did you really mean 'Requestly's'?

### Example Generated Tests

For a user API endpoint returning:

```json
{
"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:

```javascript
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.

```javascript
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](/general/api-client/tests) for more information about executing and debugging tests.


## Related Topics

- [Writing Tests](/general/api-client/tests) - Learn about manual test writing and all available assertion methods
- [Scripts](/general/api-client/scripts) - Understand Pre-request and Post-response scripts
- [Collection Runner](/general/api-client/collection-runner) - Run multiple requests and tests in sequence
- [RQ API Reference](/general/api-client/rq-api-reference) - Full documentation of the `rq` object and its methods
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.