Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit - fake api response or real response connecting to service?

I have created a few methods in a service class to connect to external service/provider via Guzzle using API POST Request.

I like to use phpunit for testing - should I use fake the HTTP Json response without connecting to a service or should it connect to a service to get real response from service?

like image 819
I'll-Be-Back Avatar asked Sep 03 '17 15:09

I'll-Be-Back


1 Answers

A common tenet in testing is "Don't mock what you don't own". Mocking those API-calls makes your tests less reliable and will give you a false sense of security as you will likely get false positives.

For example when the API unexpectedly introduces a breaking change your tests will be green and once you deploy to production you will finally notice that something is wrong. This is probably what you want your tests to catch.

When you test against the real API you will get a sense of reliability. Do your tests often fail due to service outages or timeouts? If so you can introduce measure like a retry-mechanism, circuit breakers or decoupling the API calls from the rest of your application.

When you mock the API all you can tell is that your code behaves according to a (possibly outdated) specification of the service. That is ok, but not nearly as useful as relying on the actual service.

What you could do is use groups to run those tests separately. This will make it easy to include/exclude these possibly slow and sometimes flaky tests from your remaining tests. This also helps around rate limiting, e.g. by only running those tests on critical branches.

like image 129
dbrumann Avatar answered Sep 24 '22 02:09

dbrumann