How To Write Tests For External (3rd Party) API Calls with Pytest
Have you ever found yourself trying to test code that integrates with an external 3rd party API, unsure how to move forward?
You’re mocking away but stuck with constant errors, wondering if you’re just testing the mocks instead of the integration.
While unit testing your code is simple, things get trickier when dealing with third-party dependencies.
External APIs are integral to applications, allowing them to exchange data and offer enhanced functionality.
But this comes with the responsibility of ensuring that your code can handle responses reliably — even when the data comes from a service outside your control.
How do you test these interactions to catch issues before they impact users?
Should you connect to the real API or create a controlled environment? Is mocking the right approach, or would a sandbox or fake be better?