loading...

What's your take on writing tests for third-party services or integrations?

david_ojeda profile image David Ojeda ใƒป1 min read

Let's say you have an application that uses Stripe.

Stripe gives you a dev environment- own API key, own dashboard, own data, all separated from your Stripe's live data.

Do you write tests that interact with this dev environment or do you mock the responses or something alike?

I work on an app that has many integration tests communicating with test/dev/sandbox environments, and they sometimes fail due to errors on their services, not on our own. Now we're discussing if should we mock the responses instead.

Discussion

markdown guide
 

The prevailing wisdom here is to record actual responses from the third party and use them to mock responses in your integration tests. You shouldn't be testing third party APIs in your integration tests.

You may want to separately ping the API with monitoring software to ensure it's up, but the wisdom is you trust the third party to act the way it says. If you can't trust them, you should have vetted the service better before choosing it.

 

Thanks for the advice! We already ping the service, but I thought it would be a good idea to also test the specific endpoint responses themselves.

I'll change the tests to mock the response instead and see how it goes

 

Thanks for the story! Mocking seems the way for a predictable testing suite.

In relation to the live assertions, maybe I can still run the tests that interact with the sandbox environment in a separate test suite that is outside the golden path of deployments and, as you said, have some alert to check when things go south with the integration.

 

I think it's not one or the other. I totally do both. We have integrated 5 different payment providers at work. We have tests with mocked responses to make sure that OUR code works. But we also have tests against the test environments of each provider, to make sure that (for whatever reason) the responses (or the APIs) have not changed.
I think both approaches are important, but if you habe enough trust in your thirdparty systems, mocked responses should do the trick.

 

The Paypal sandbox is a giant pain. I've been there. Maybe they will get it right some day...