Getting Started with Testacular - A Tutorial

I've recently discovered the Testacular javascript unit testing test runner. It's built on node.js, and if you're into unit testing (you should be) and streamlining your workflow (you should be), it will really help light a fire under your butt and jack your productivity. This tutorial is for basic installation only. The best place to learn more is to watch the screencast.

If you haven't already, you'll need to install node.js.  You can get it here for the OS of your choice.

Once you have node installed, you just need to follow the simple install instruction on the testacular web site. I'll save you the click though, just do this:

node install -g testacular

 

Ok, it's not THAT simple, but only because you also have the option of installing the unstable build. If you'd like to use qunit, you'll want to do that (at least at the time of this writing). Here's the command:

node install -g testacular@canary

  As of the writing of this post, stable is at 0.4, unstable at 5.8 (stables are always even). If you want stable with qunit support, wait for 0.6. you can check your version once you install with:

testacular --version

  Ok, so the next thing you need to do is set up the config file. You can do this from the command line via:

testacular init

  This will give you a nice setup flow where you can choose to do some stuff. Or you can skip it all and fill in the resulting .js file yourself.  Here are the 3 most important config settings (remember, this is a basics tutorial)

files = [
    QUNIT,
    QUNIT_ADAPTER,
    ..
]

browsers = ['PhantomJS'];

// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;

  The first two files are variables set in testacular which wire up your testing framework. 0.5.8 has OOTB support for jasmine, mocha, and qunit. The rest of the files would be your scripts and their tests. At this point, you'll want to add your files. For example if you had a helloWorld.js and a spec/helloWorld.js file, you would do:

files = [
    QUNIT,
    QUNIT_ADAPTER,
    helloWorld.js,
    spec/helloWorld.js
]

 

If you know your tests will all be in the spec folder, you can also use wildcards:

files = [
    QUNIT,
    QUNIT_ADAPTER,
    helloWorld.js,
    spec/*.js
]

 

Next, you need to choose the browsers you want to run the tests in. You do this by either adding environment paths to the browser application/exe, or by setting symlinks. I've only done this in windows so far, by adding (for example) CHROME_BIN = 'path to chrome.exe' to my local environment variables (you can go to control panel/search and type in 'environment variable' if you aren't sure where to add this).

browsers = ['PhantomJS'];

  One of the best things about testacular is that you can have it watch for file changes, and rerun your tests when it detects a change. This way you have a constant monitor to whether or not you are screwing anything up, and can react immediately without having to launch a browser or type into the command line. Awesome.

// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;

  So, now you have your test runner setup, you have your testing framework ready to go.. all you have to do is start the test server:

testacular start

  Now go in and add some tests, save your files, and watch the magic happen. Testacular can also integrate with build servers like Jenkins (which we use at Linksys), but that's perhaps fodder for another post.

Also of note, Testacular is a part of the awesome Yeomen, which I'll be diving into soon.