API tests

Leena
Continuous Delivery
3 min readNov 12, 2017

Almost any app that we work with will have API integrations — either APIs created by us or external APIs. I’ve found few interesting tools to help us test these without as much investment as required for End-to-end tests.

Record and Playback HTTP interactions

Unit tests should mock the external integrations, be it a library or an API. While it makes the independent and fast, writing mocks for every request, response combo might be difficult to manage.

VCR records your test suite’s HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

When the test runs for the first time, it will record the request to a yml file, called cassette file. When run again, it will playback the file rather than making an API call.

The test will fail if it finds any difference in the request [either in the params or headers]. You can also configure what to match too. If you want to re-record the interaction, you can delete the cassette file and run the test. The cassettes are supposed to be checked-in into the source control so that the API calls are mocked when running tests on other machines, especially in Continuous Integration Server.

VCR is written in Ruby, but there are ports to other languages as mentioned on their Github page.

Test services without writing code

Diffy finds potential bugs in your service using running instances of your new code and your old code side by side. The premise for Diffy is that if two implementations of the service return “similar” responses for a sufficiently large and diverse set of requests, then the two implementations can be treated as equivalent and the newer implementation is regression-free.

I found Diffy interesting because:

  • You can test your services without writing any code
  • It is easy to setup and simple to run
  • Helps you to maintain the contract of your services and find potential issues, if there are any

Diffy acts as a proxy and multicasts every request to three different instances of your server, i.e., primary, secondary and candidate. You can keep the primary and secondary as same too. It compares the response from each of these instances and reports differences if there are any.

Diffy in actively maintained and developed @ Twitter and used in production too. Watch the below video if you want to learn more about Diffy.

Test contracts

https://martinfowler.com/articles/consumerDrivenContracts.html

Popularised by Microservices and distributed systems, consumer-driven contract tests help the teams to verify that it is as per the contracts defined. The contracts are defined by both the Consumer and Provider and when the tests are run the tool makes sure that they are indeed in sync.

Pacto is a popular tool and language agnostic. You can use it for testing services written in any language.

In this talk, Pierre Vincent talks about how Consumer-driven contracts testing improved the confidence and collaboration among the teams while designing APIs.

https://testing.googleblog.com/2015/04/just-say-no-to-more-end-to-end-tests.html

Most of us are familiar with the above-recommended testing pyramid. Unit tests help one to cover issues at the unit level, and integration tests help to make sure that the integration among the components or third-party libraries works as expected.

End-to-end tests are flaky and tough to maintain. And it may cost more than they’re worth. And the tools mentioned above help us to test the API integrations without having to rely on the end-to-end tests.

Having high coverage with Unit tests is essential. In this article, Mike Bland talks about the importance of Unit-testing culture and how it can avoid the bugs like Goto fail and heartbleed.

--

--

Leena
Continuous Delivery

Co-founder/CTO @ PracticeNow, Bangalore, India. A strong believer of lean principles, an evangelist and practitioner of Continuous delivery