This API sounds like garbage, don’t get me wrong; and because of that, mocking it’s behavior is going to be almost impossible.
This API sounds like garbage, don’t get me wrong; and because of that, mocking it’s behavior is going to be almost impossible.
The API team was an internal team to the company and there was absolutely no communication possible with them, so we kinda had no choice.
If I want to test external APIs I'll do that in a separate set of integration tests which are run as part of a separate system, not as part of my CI for every code commit to my repo.
I mostly use Python, and the APIs I talk to are mostly accessed via the requests or httpx libraries - both of which have excellent libraries for productive mocking:
- https://requests-mock.readthedocs.io/en/latest/pytest.html
I may or may not actually test the implementation of that method.
In short: wrap up your external dependency.
I usually have a switch in our tests: default is to run against local mocks, but the test data also works against the third party sandbox environment. Periodically we revalidate our mocks against the sandbox responses to make sure the other API hasn't drifted.
CI always runs against local mocks, for speed and reliability.
But it can be as easy as using a fake http library and mocking the responses, or using a httptest server: https://onsi.github.io/gomega/#ghttp-testing-http-clients
If the API is complicated and you have to write your own fake server, that might not make sense for small projects.
I think situations like external API hit a fuzzy line between unit/integration. For testing an API unit I would reach for some way to save, re-play, and re-record those interactions just for sanity sake.
To me an integration test would be multi-step behavior, not just testing a specific request does a specific thing but a chain of requests, or verifying side-effects.
This strikes a balance where 99% of the time you are making calls that never leave the process for fast testing, but can validate against the real implementation as needed.
And I do not get the gripe with a production rate limit tbh.