5
votes

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?

2
Mocking the service allows you to return a variety of responses, especially ones difficult to test (such as errors).Nigel Ren
@NigelRen You should answer with more detail and how to mock the serviceI'll-Be-Back
If you had some code, it may help, don't know what to answer. Try looking through guzzle3.readthedocs.io/testing/unit-testing.html.Nigel Ren
Do you like fast tests? Do you like slow, unreliable tests?localheinz
@localheinz reliable testsI'll-Be-Back

2 Answers

4
votes

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.

1
votes

You should mock external API calls. Your tests are intended to test YOUR code not the external API.

Note: Ultimately if you DO use the external API with your tests you will likely be connecting to a test version of the API (different to the live version of the API that your production environment would connect to) so you're not really ensuring the consistency of the API response anyway.