I'm a writer, programmer, web developer, and entrepreneur. Preona is my current startup that began its life as the team developing Twitulater. Our goal is to create a set of applications for the emerging Synaptic Web, which would rank real-time information streams in near real time, all along reading its user behaviour and understanding how to intelligently react to it. Swizec is a DZone MVB and is not an employee of DZone and has posted 66 posts at DZone. You can read more from them at their website. View Full User Profile

Blackbox Testing node.js Apps

08.21.2012
| 1682 views |
  • submit to reddit

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

Blackbox testing is my favourite kind of testing. It is 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){
    request(app)
      .get('/user')
      .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.

Awesome.

Published at DZone with permission of Swizec Teller, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)