By netmilk (ntmlk, email@example.com) on 17 Oct 2013
As a follow up for my previous blogpost which is introducing Dredd—the tool for testing APIs from API Blueprint, I’ve prepared a simple example application in Express.js to demonstrate how to write a blueprint, a backend, test it and let it be tested forever in the CI.
Note I’m using Express.js for the backend but Dredd, API Blueprint and Apiary do not depend on any specific programming language. Dredd interacts with the backend app only through the HTTP layer. That means the process as described below can be applied on any backend platform and framework in any language.
Let’s move on – it’s super-easy!
Start thinking about your API, create an Apiary project with a sample blueprint and save it. You can use my example blueprint. Do not forget to invite others to join the discussion. Enjoy generated documentation and play with the mock.
Bootstrap application environment
npm install express coffee-script mongous
Create empty Express application in file
express = require 'express' app = express() app.listen 3000 console.log('Listening on port 3000');
Add a test script to
#!/bin/sh ./node_modules/coffee-script/bin/coffee app.coffee & sleep 5 PID=$! dredd apiary.apib http://localhost:3000/ RESULT=$? kill -9 $PID exit $RESULT
Install Dredd globally
npm install -g dredd
See the test fail
$ ./scripts/test $ echo $? 1
Run the test again and see it pass
$ ./scripts/test $ echo $? 0
And finally – the holy grail! When you plug it into a Continuous Integration system any change of the API Blueprint in your GitHub repository will update the API documentation generated by Apiary and run the test harness. At this point you have always up-to-date API documentation. And what is even better – it is absolutely free of charge!
Add a Travis dotfile to
language: node_js node_js: - 0.8 - 0.10 before_install: - npm install -g dredd script: ./scripts/test services: - mongodb before_script: - mongo mydb_test --eval 'db.addUser("test", "test");'
We strongly appreciate any feedback in Dredd’s GitHub issues or directly through our support.
Dredd works with several Continuous Integration Services. I’ve tested it with Travis-CI, Circle-CI, Codeship, Semaphore, Teamcity, Jenkins, Atlassian Bamboo and probably many others.