One of the best features in Djangois the test client that comes with the test framework -> blackbox testing baby, yeah!

Black box flight recorder

Black box flight recorder (Photo credit: edvvc)

Blackbox testing is my favourite kind of testing. Much more useful than unit tests and generally a lot easier to write, maintain and deal with. And that’s because rather than making sure all your functions and deep internals are behaving like they should, you only test what really matters in a web app.

Does the right output happen for this specific input?

Your users don’t care that you have full unit test coverage. They will never see the output of all your internal functions and methods and database drivers and whatnot. All they care is that if they put in X they get the correct Y out.

The two main benefits of blackbox testing for a developer are:

  1. Low maintenance tests – you don’t have to rewrite the whole test suite when refactoring the deep internals of your app
  2. Integration tests – since you’re only matching input to output you are testing your whole chain, everything from routing, to database connections and the business logic of the app itself

Although it’s still prudent to write enough of these tests to check for correctly returning errors, making sure your app doesn’t crash on strange inputs and so on.

It’s also important not to forget unit tests for core business logic. That’s algorithmic stuff, algorithms should have unit tests attached to them. But in general, web apps aren’t algorithms, they’re systems. And systems need blackbox tests.

Doing it in node.js

Ok so blackbox testing is an awesome fit for web apps. But how do you do them?

In django this is simple, when in a test you just say something like

response = self.client.get("/my/url")
self.assertEqual(response.status, 200)

and check the response is what you expected. The test client automagically ensures there is a fake server running, that it takes requests, responds and is then torn down when you’re done testing.

Until recently the only way I knew of doing this with node.js has been to run the server and send it requests. But that’s a bit lame, after all, I want to just run the tests. Of course I’m going to forget to run the server every single time.

Not to mention you have to keep restarting it when you change the code.

Enter Supertest, stage left.

Supertest is a high-level http test library based on superagent, which in turn is a high-level abstraction thingy for doing http requests.

It takes any type of server object and lets you run requests and check for expected results in a very simple manner. If need be, you can even drop down to the low-level API’s for http requests.

A simple (mocha) test might look something like this:

var app = require('../app.js');
describe('GET /users', function(){
  it('respond with json', function(done){
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200, done);

Where app is, for example, an express.js server –  request is our test client. We are just checking that sending a GET request to /user will return some sort of json and that the status code will be 200.

If the server isn’t running when doing that require then supertest will make sure it is bound to an ephemereal port and then brought back down after the tests are done.

Oh and because of superagent you can send any type of request and check for any type of response.


Enhanced by Zemanta

Learned something new? Want to become a better engineer? 💌

Join 9,400+ people just like you already improving their skills.

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖 right away. After that you'll get a thoughtfully written email every week about React, JavaScript,  and lessons learned in my 20 years of writing code for companies ranging from tiny startups to Fortune5 behemoths.

Man, I love your way of writing these newsletters. Often very relatable and funny perspectives about the mundane struggles of a dev. Lightens up my day. ~ Kostas

PS: You should also follow me on twitter 👉 here.
It's where I go to shoot the shit about programming.