如何搭建Mock Server用于iOS和Android应用自动化测试?
Hey Sandra, totally get where you're coming from—setting up a mock server for the first time can feel a bit daunting, but it’s actually pretty straightforward once you break it down into actionable steps. Let’s walk through everything you need to know to get started:
Before picking a tool, nail down exactly what you need to simulate. Ask yourself:
- Which API endpoints do you need to mock? (GET, POST, PUT, DELETE, etc.)
- What specific data scenarios do you need? (e.g., empty responses, error codes like 404/500, edge-case business data like a user with 1000 orders)
- Do you need dynamic responses? (e.g., return different data based on request parameters or headers)
There are tons of options, ranging from zero-config tools to fully customizable frameworks. Here are the most popular ones for mobile app testing:
Quick & easy (no coding required)
- json-server: A super lightweight Node.js tool that turns a JSON file into a REST API in seconds. Perfect for simple, static mock data.
- Postman Mock Server: If you already use Postman for API testing, you can define mock endpoints directly in Postman, set up response rules, and share the mock URL with your app.
- Mockaroo: Generate realistic mock data (like names, emails, addresses) and host it as a simple API, great for populating test databases or simulating large datasets.
Flexible & customizable (for complex scenarios)
- WireMock: A Java-based tool that lets you create detailed stubs—match specific request parameters, headers, or even request bodies, and return dynamic responses. Works great for complex business logic.
- MockServer: Similar to WireMock, but supports multiple languages and has a simple API to configure stubs programmatically.
- Custom lightweight server: If you know Python/Node.js, you can spin up a tiny server with Flask (Python) or Express (Node.js) to fully control every aspect of the mock responses.
Let’s use json-server as a quick example since it’s perfect for getting started:
- Install it via npm:
npm install -g json-server - Create a
db.jsonfile with your mock data:
{ "users": [ { "id": 1, "name": "Test User", "accountStatus": "active", "orders": 5 }, { "id": 2, "name": "Inactive User", "accountStatus": "suspended", "orders": 0 } ], "products": [ { "id": 101, "name": "Premium Subscription", "price": 9.99 } ] }
- Start the server:
json-server --watch db.json --port 3000 - Now you can hit endpoints like
http://localhost:3000/users/1to get the active user data, or POST to/usersto add a new mock user.
You need to redirect your app’s API calls to the mock server instead of the real backend:
- iOS: Configure a test environment in your app where the base API URL is set to your mock server’s address (e.g.,
http://your-computer-local-ip:3000—make sure your phone is on the same Wi-Fi network). Alternatively, use a proxy tool like Charles to forward API requests to the mock server. - Android: For emulators, you can use
http://10.0.2.2:3000to access your computer’s localhost. For physical devices, use your computer’s local IP, same as iOS. You can also build a debug build with the mock URL hardcoded, or use build variants to switch between environments.
To make this seamless for your test suite:
- If you’re using Appium or a similar framework, add setup/teardown steps to start the mock server before tests run and shut it down afterward. For json-server, you can run a Node.js script to start it programmatically.
- Tie specific mock data to test cases: For example, if you’re testing the "inactive user" flow, pre-configure the mock server to return the suspended user data when the test requests that user ID.
- For dynamic scenarios (like testing error handling), update the mock server’s response on the fly during tests (many tools have APIs to modify stubs programmatically).
- Keep your mock data structure identical to the real API’s response format—this ensures your tests are as close to production as possible.
- Record real API responses (using tools like Charles or Postman) and use them as a base for your mock data—saves time and ensures accuracy.
- Use hot-reloading tools (like json-server’s watch mode) so you can update mock data without restarting the server, making debugging faster.
内容的提问来源于stack exchange,提问作者Sandra

