When should automated tests mock external services, and when should they use real ones?
What the interviewer wants to hear
- Mock to test error cases, edge data and for speed and stability
- Real integrations needed to catch contract and configuration problems
- Balance with contract tests
- Keep a small number of true end-to-end tests
- Risk of mocks drifting from reality
Model answer
Mocks are great when I need control and stability. Third-party services like payment gateways, SMS or maps can be slow, rate-limited or unavailable in test environments, and they rarely let me trigger errors like timeouts or a declined card. With mocks, using network interception in the UI or tools like WireMock for services, I can test those cases deterministically and fast. The risk is that mocks drift away from how the real service behaves, so tests pass while production breaks. To balance this, I keep a small number of tests that hit the real sandbox, run contract tests that verify our assumptions about the provider's API, and update mocks from recorded real responses. For our own internal services, I prefer real integration in a test environment for critical flows, and mocks for everything else.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Automation Test Engineer questions
- How would you design a test automation framework from scratch for a web app?
- A test fails randomly in CI about 1 in 20 runs. Walk me through how you find and fix the cause.
- How do you manage test data in an automated suite that runs in parallel on shared environments?
- How do you make automated test results useful to developers and managers?
- How do you handle authentication in a large UI test suite without making every test log in through the UI?
- Your suite needs to run in parallel across many workers. What do you change to make that safe?
- Describe how you would set up automated tests in a CI/CD pipeline for a web application.
- You are asked to load test a new checkout API before a big sale. How do you plan and run it?
- How would you approach test automation for a mobile app on Android and iOS?