Skip to main content
The rq.test object allows you to write tests in your post-response scripts to validate API responses and ensure your APIs are working as expected. Tests help you automate quality assurance and catch issues early in development.

Properties and Methods

rq.test(name, function)

Creates a test with a given name and a test function. The test function should contain assertions using rq.expect. Parameters:
  • name (string): The name of the test (will be displayed in test results)
  • function (function, optional): A function containing test assertions. If you leave it out, the test is reported as passed. If the function takes a parameter, the test is asynchronous.
Example:

rq.test.index()

Returns how many tests the script has started so far, skipped tests included.

Asynchronous Tests

A test that checks the result of a timer or a callback can wait for it. Give the test function a parameter, usually named done, and call it when the test has finished. Pass an error to done to fail the test.
  • The test is reported when done is called. A test that never calls done is not reported.
  • An error thrown before done is called fails the test straight away.
  • A test that does not take a parameter is reported as soon as its function returns, so assertions in its callbacks do not count towards it.

Writing Tests

Tests are typically written in post-response scripts to validate the API response after it’s received.

Basic Test Example

Multiple Assertions in One Test

You can include multiple assertions within a single test:

Common Test Patterns

Validate Status Code

Validate Response Structure

Validate Response Time

Conditional Tests

For more assertion examples and patterns, see the rq.expect documentation and Chai.js official documentation.

Best Practices

  1. Use Descriptive Test Names: Make test names clear and specific
  2. One Concept Per Test: Each test should validate one logical concept
  3. Test Expected Behavior: Focus on what should happen
  4. Handle Different Response Codes: Write tests for both success and error scenarios
  5. Store Values for Later Use: Combine tests with variable storage
Example:

Test Results

Test Results in Requestly Test results are displayed in the Requestly interface, showing:
  • ✅ Passed tests (green)
  • ❌ Failed tests (red)
  • Test execution time
  • Assertion details for failed tests