Continuous delivery is difficult and here at Achievers we embody all of it. With several deploys a week, it’s important to stay on top of things and ensure that our API is functioning correctly. Several of our applications, as well as third-party clients, rely on our API. Here’s how we developed a test suite for our platform’s API within three days.
Before diving into libraries, let’s talk about JSON Schemas. A JSON Schema is JSON that can be used to validate the structure of other JSON.
For example, let’s say our API returns this JSON:
A JSON Schema that validates this JSON will look like this:
Using valid responses from our API, we can produce JSON Schemas easily using jsonschema.net, a fantastic tool. JSON Schemas are extremely configurable and are able to handle many different data types and even regular expressions. You can find everything about JSON Schemas over at json-schema.org.
You can then run the tests with mocha: $ mocha test.js
Of course, this is a small snippet of what’s possible.
Combined with Jenkins and a nifty Pipeline Plugin, we’ve now produced a powerful safety net for our production code. We can be confident that our apps receive data in an expected structure with little effort.
CD is a fight that requires constant vigilance. With many small tools like this we can sanity check our endpoints to ensure nothing has gone awry. Of course, even with API tests you shouldn’t ignore the rest of the testing pyramid, but this will get you moving in the right direction.