Speakeasy Logo
Skip to Content

Product Updates

How to generate a mock server to test SDKs

Brian Flad

Brian Flad

December 2, 2024 - 6 min read

Product Updates

Testing your SDK against your API can be challenging. During development, you often need to spin up a local server as well as a database and other dependencies, which can slow down the process. You’re also prone to network issues, rate limits, and other external factors that can disrupt development and testing.

Mock servers provide a way to simulate your API’s behavior without needing the actual API to be running. They’re especially useful when you need to test how your SDK handles specific behaviors – like rate limiting or malformed responses – without relying on unpredictable or hard-to-reproduce real-world conditions.

In this guide, we’ll walk through how to generate a mock server using Speakeasy and how to test your SDK against it. We’ll also discuss the advantages of using a mock server during SDK development and compare Speakeasy with other mock server solutions.

What is a mock server?

A mock server is a virtual API endpoint that simulates the behavior of a real API server. It intercepts test HTTP requests and returns predefined responses according to your OpenAPI document. This means you can test your SDK without connecting to a live API.

Think of a mock server as a stand-in actor during a rehearsal. It follows a script (your OpenAPI document) and responds exactly as needed, every time. This consistency is invaluable for ensuring thorough testing across various scenarios, from successful responses to error conditions. It also significantly speeds up the development process by removing the need to wait for real API responses.

Why use mock servers?

In development, you need to verify how your SDK:

  • Manages rate limits
  • Handles network timeouts
  • Processes malformed responses

While Speakeasy-generated SDKs handle these scenarios gracefully, you still need to test them to ensure your SDK behaves as expected.

A mock server allows you to simulate these scenarios reliably, without waiting for them to occur in a real environment, ensuring your SDK is thoroughly tested before it reaches production.

Additionally, mock servers allow you to test your SDK in complete isolation. Instead of depending on external services, you can execute local tests that provide consistent, predictable responses every time.

This stability has significant advantages if your backend experiences intermittent downtime or if you want to minimize the impact of outside variables like network latency or downtime windows. When using Speakeasy, the mock server aligns closely with your OpenAPI document, which means that simulated responses closely mirror the behavior of your existing API.

While generating your SDK, Speakeasy automatically creates a mock server based on your OpenAPI document, eliminating time-consuming manual coding. It even generates a suite of tests that interact with the server, giving you a strong baseline of coverage for your endpoints right out of the box.

As your API evolves, you can regenerate and update both the mock server and the test suite whenever you regenerate your SDK, meaning your simulated mock environment and tests stay in sync with the latest version of your OpenAPI document.

Generating a mock server with Speakeasy

To generate a mock server using Speakeasy, we’ll use our example FastAPI server , which includes an OpenAPI document.

1. Clone the examples repository

Begin by cloning the the Speakeasy examples repository. This will serve as our base project.

Go to the FastAPI example:

2. Set up Speakeasy

If you haven’t installed the Speakeasy CLI yet, follow these steps to get started:

  • macOS (Homebrew):
  • macOS/Linux (Shell Script):
  • Windows (Scoop):

You can verify the installation by running the following command:

Next, authenticate your CLI with the Speakeasy platform:

For more detailed installation instructions, refer to the getting started guide.

3. Generate the SDK

In the cloned repository, use the Speakeasy CLI to generate an SDK from the OpenAPI document.

Answer the prompts to configure the SDK generation process. The resulting SDK will be saved in the /sdk directory.

4. Modify the gen.yaml file

Navigate to the generated SDK directory and locate the .speakeasy folder.

The gen.yaml file in the .speakeasy folder contains the configuration for the SDK generation process. We’ll modify this file to enable mock server generation.

Open the gen.yaml file in your text editor:

Add the tests configuration to the gen.yaml file and set generateNewTests to true to enable mock server generation and create test files.

5. Generate the mock server

Next, regenerate the SDK with the updated configuration. Run the following command from a terminal in your SDK directory:

A new /tests directory will be created in the ./sdk folder, containing the mock server and test files.

5. Run the mock server

Navigate to the mock server directory:

Start the mock server:

You now have a fully operational mock server to simulate API responses and test your SDK without relying on the actual API.

For more information on running the mock server, read the generated README.md file in the tests/mockserver directory.

Testing against the mock server

Let’s look at how you can unit test your SDK against the mock server.

The generated SDK includes test files that you can use to validate the behavior of your SDK.

To run the tests, you’ll need to install pytest and a few other modules listed in the pyproject.toml of the generated SDK.

Open a new terminal window and install the dependencies. It’s best to install in a virtual environment.

Navigate to the tests directory in the SDK and run the tests:

The generated tests will be run against the mock server, validating that your SDK behaves as expected.

Comparison with other mock server solutions

Several other mock server solutions are available, each with its own set of features and limitations.

  • Prism : An open-source API mocking tool that uses OpenAPI documents to generate mock servers.
  • MockServer : A Java-based tool for mocking HTTP/HTTPS requests.
  • Postman : An API development platform that includes mocking capabilities.

The table below compares Speakeasy with these mock server solutions.

Comparison with other mock server solutions

Feature
Primary use case
Speakeasy
Automated SDK generation and management
Prism
API mocking and validation based on OpenAPI documents
MockServer
HTTP/HTTPS request-response mocking and testing
Postman
API development, testing, and collaboration
SDK generation
Speakeasy
✅ Yes (automated SDK generation)
Prism
❌ No
MockServer
❌ No
Postman
❌ No
Test generation
Speakeasy
✅ Yes
Prism
❌ No
MockServer
❌ No
Postman
✅ Yes (pre-saved tests)
Supported languages
Speakeasy
8+ languages, including Python, JavaScript, TypeScript, Java, and Go.
Prism
Language-agnostic
MockServer
Java only
Postman
Language-agnostic
Testing types
Speakeasy
SDK testing, unit testing, contract testing
Prism
Contract testing, API prototyping
MockServer
Integration, functional, and system testing
Postman
Functional, integration, and regression testing
Mocking support
Speakeasy
✅ Yes, built-in for SDKs
Prism
✅ Yes (dynamic/static examples)
MockServer
✅ Yes (complex mocking with templates)
Postman
✅ Yes (mock servers with predefined responses)
Request validation
Speakeasy
✅ Built-in for SDKs (validates requests against OpenAPI documents)
Prism
✅ Yes
MockServer
✅ Yes
Postman
⚠️ Limited (manually set up via tests)
Response generation
Speakeasy
✅ Handles SDK responses
Prism
✅ Static and dynamic responses
MockServer
✅ Templated responses
Postman
⚠️ Static responses
OpenAPI support
Speakeasy
✅ Yes, creates mock expectations based on your OpenAPI document
Prism
✅ Yes
MockServer
✅ Yes
Postman
⚠️ Partial (import/export specs)

Why choose Speakeasy for test generation and mocking?

Speakeasy simplifies two key parts of your SDK development workflow: test generation and mocking. Here’s how:

  1. Immediate test suite Speakeasy generates a suite of tests when you generate your SDK. Each test aligns with a specific API endpoint, so you can quickly validate both regular and edge-case scenarios without writing boilerplate code.

  2. Automatic mock server With one command, Speakeasy can start a mock server based on your OpenAPI document. You won’t have to set up separate testing environments or craft mock responses by hand, making local testing easier.

  3. Isolated and repeatable testing Because tests run against a mock server, you aren’t affected by network issues or external service downtime. The environment remains consistent, so test results are reliable and easy to reproduce.

  4. Comprehensive endpoint coverage When Speakeasy generates tests alongside a mock server, it covers every endpoint in your API document. This helps you catch issues early and maintain higher test coverage.

  5. Easy regeneration As your API changes, update your OpenAPI document. Speakeasy regenerates the mock server and tests, ensuring everything remains in sync with minimal effort on your part.

By handling test generation and mocking, Speakeasy frees you from having to constantly devise tests and maintain testing environments through your development cycle.

Next steps

For a deeper dive into testing, take a look at our guide to API contract test generation, which covers automated testing and validating API contracts.

Last updated on

Organize your
dev universe,

faster and easier.

Try Speakeasy Now