It's still in the early stages but I wanted to let people know I've
built a new unit testing framework for MooTools.  It requires MooTools
1.2+.  It's great for testing little projects because it defaults to
pretty printing test results right to the console.  At the moment it
works under FF3, Safari 3, and IE8 (with support for more coming
soon).  It can also support fully automated testing of a large code
base with rich HTML output which is what we plan on using it for on
the ShiftSpace project (http://www.shiftspace.org).

1. Unit tests are just MooTools classes.
2. Automatically outputs to console, support for rich HTML output.
3. Support for assertThrows.  This is the most important feature in my
opinion.  This encourages plugin developers to throw semantically
meaningful exceptions for users of their plugins.  Your plugin then
becomes self documenting by directing users of incorrect usage.  This
one of the most painful parts of JavaScript programming- the
ubiquitous lack of meaningful error information.  SSUnit provides the
custom SSException class which knows how to print itself in a readable
way.
4. Handles AJAX requests via this.startAsync(), which returns a
fixture hook object.  When your AJAX request is complete you can
complete the test via this.endAsync(fixtureHook).
5. Document tests from within the fixture via this.doc(docString).

Interested in knowing what people think.  There's a lot of
improvements to be made.

http://github.com/swannodette/ssunit/tree/master

Example
=============================================================

var TestCaseTestDivideByZeroException = new Class({
  name: 'TestCaseTestDivideByZeroException',
  Extends: SSException,
  Implements: SSExceptionPrinter
});

function TestCaseDivide(x, y)
{
  if(y == 0) throw new TestCaseTestDivideByZeroException(new Error(),
"Oops division by zero!");
}

var SSDefaultTest = new Class({

  name: 'SSDefaultTest',

  Extends: SSUnitTest.TestCase,

  testAdd: function()
  {
    this.doc("Test adding two values and assert correct sum.");
    this.assertEqual(2+3, 5);
  },

  testDivisionByZero: function()
  {
    this.doc("Test that division by zero throws an exception.");
    this.assertThrows(TestCaseTestDivideByZeroException,
TestCaseDivide, [5, 0]);
  },

  testSubtract: function()
  {
    this.doc("Test substraction and assert result.");
    var x = 5;
    this.assertNotEqual(x-2, 5);
  },

  testShouldFail: function()
  {
    this.doc("This test should fail. Oops.");
    var x = 5;
    this.assertNotEqual(x, 5);
  },

  testMultiply: function()
  {
    this.doc("Test multiplying two values and assert result.");
    this.assert((3*3) == 9);
  }

});

// instantiate the test and collect it
new SSDefaultTest();

// run all tests
SSUnitTest.main();

Reply via email to