FireUnit: JavaScript Unit Testing Extension


In my work with the Firebug team over the past couple months I’ve been working with Jan Odvarko on a way to provide some form of unit testing that we can build off of. The result of my work is a new Firefox/Firebug extension called FireUnit.

FireUnit provides a simple JavaScript API for doing simple test logging and viewing within a new tab of Firebug.

For example, here’s some of the API that you can use (we’re starting with the basics and looking to expand with more methods, later).

  1. // Simple true-like/false-like testing
  2. fireunit.ok( true, "I'm going to pass!" );
  3. fireunit.ok( false, "I'm going to fail!" );
  4.  
  5. // Compare two strings - shows a diff of the
  6. // results if they're different
  7. fireunit.compare(
  8.   "The lazy fox jumped over the log.",
  9.   "The lazy brown fox jumped the log.",
  10.   "Are these two strings the same?"
  11. );
  12.  
  13. // Compare a string using a regular expression
  14. fireunit.reCompare(
  15.   /The .* fox jumped the log./,
  16.   "The lazy brown fox jumped the log.",
  17.   "Compare a string using a RegExp."
  18. );
  19.  
  20. // Display the total results
  21. fireunit.testDone();

The results will appear in a ‘Test’ tab in Firebug (which must be installed in order for Fireunit to work). Each of the results can be expanded to show additional information including a full stack trace of where the test ran and a comparison with a diff.

FireUnit also provides a couple methods for simulating native browser events:

  1. // You can also simulate browser events
  2. var input = document.getElementsByTagName("input")[0];
  3. fireunit.mouseDown( input );
  4. fireunit.click( input );
  5. fireunit.focus( input );
  6. fireunit.key( input, "a" );

And a way of running a batch of test files (each of which would contain a number of individual tests).

  1. // Or run multiple pages of tests:
  2. fireunit.runTests("test2.html", "test3.html");
  3.  
  4. // Place at the end of every test file in order to continue
  5. fireunit.testDone();

We’ve been using this test runner to run a number of Firebug tests, especially ones that are network based.

Depending on the suite it’s pretty easy to adapt existing test suites to display their results in FireUnit.

Running the jQuery selector test suite that has the following snippet added:

  1. if ( typeof fireunit === "object" ) {
  2.     QUnit.log = fireunit.ok;
  3.     QUnit.done = fireunit.testDone;
  4. }

Yields the following results (which are completely navigable):

If you want to get started using FireUnit you can head on over to the Fireunit.org site and download the latest extension.

You can also grab the source off of the repository on Github.

Jan has also written a blog post detailing a little bit more about what we’re using FireUnit for in the Firebug Working Group.

This is still a very early release of our work – there’s obviously a ton of room left to grow – so feedback is expected and appreciated.

Posted: December 18th, 2008


If you particularly enjoy my work, I appreciate donations given with Gittip.

44 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

Ukiyo-e Database and Search

Ukiyo-e.org

Japanese woodblock print database and search engine.


John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.