Saturday, May 5, 2012

Front-End Release Management: Unit Testing

This is a small part of a series of "Front-End Release Management" blogposts I'll be writing.

Unit testing allows your developers to protect pieces of functionality in the application from misbehaviours. It is extremely important on groundwork (analytics and frameworks).

Before diving into the tools that we're using at RESAAS to achieve continuous integration with our JavaScript unit tests, let's go over some of the core goals:

  • Easy to write tests
  • Fast to execute tests
  • Easy to integrate into Jenkins (CI)

Our front-end engineers are extremely talented and experienced with JavaScript, CSS, HTML5 -- that said, if we introduce a tool, we need to keep the toolchain as streamlined as possible and in alignment with their skill-sets. JavaScript is natural to any software engineer. It is fast (based on V8, Chakra, and Nitro), it is easy to understand, and it is very easy to pick up if you're coming from a designer background or from a purely back-end background.

In part of the testing initiatives, we investigated different test frameworks, such as, qunit and Jasmine BDD. In the end, we decided on Jasmine BDD partly because of its expressiveness (using a business-centric lexicon/vocabulary). Here's an example of what Jasmine BDD has to offer in regards to readability and ease of use in writing a few specs:

As you can see above, things are simple. Jasmine BDD is so simple that it's rather easy to start doing targeted code generation (partly because of its nesting describes). For example, our analytics library called Arbiter has roughly 700 generated specs to ensure that event tracking behaves the way it is supposed to depending on the user interactions being specified.

In continuous integration, we've hooked up our roughly 2,000 unit tests to Jenkins. The guts of the test runner is just a C# test class, which talks directly to the Chutzpah executable. Chutzpah then runs all the specs and returns the output to the C# Test class, which ensures that all tests pass. Additionally, Chutzpah is very easy-to-use within Visual Studio. It lets you right click a folder of specs and executes them and streams the results to the Visual Studio console. Below is a diagram of how it works.

It's that easy. We've benchmarked our specs and so far we're seeing a few thousand unit tests run in less than 10 seconds. At the start of the blogpost, I outlined "fast to execute tests" as part of our core design goals. Ultimately, development and testing tools need to be fast and the faster we can react to misbehaviours in the code, the faster we can fix it.

Good luck and have fun,
Jaime Bueza

Jaime Bueza is a software engineer in Vancouver, British Columbia, Canada. He has developed web applications for Nintendo, Starbucks, Bacardi, Nike, Electronic Arts, Ritchie Brothers, Kiwi Collections, Cox Communications and Microsoft. When he's not developing useful software that constantly evolves with business requirements, he's writing open source software and helping developers in the Windows Azure NodeJS community.