Unit testing

HyperTest uses the EcmaUnit library to allow unit-testing your JavaScript APIs. Unit tests are written in a style similar to JUnit and the Python unittest library, which means that you write special classes that contain test functions called test cases. The tests on these test cases will be executed by a test runner and for each method the library will display whether it was called successfully, or whether an error occurred (either in executed code, or raised by one of the special assert methods available to the test case).

Example

function ExampleTestCase() {
    /* an example unit test */
    this.name = 'ExampleTestCase';

    // this method will be executed before *every* test
    // method, there's also a tearDown counterpart that
    // is not used in this example
    this.setUp = function() {
        function Foo() {
            this.returnfoo = function() {
              return 'foo';
            };
            this.throwfoo = function() {
              throw('foo');
            };
        };
        this.foo = new Foo();
    };

    // an example of a successful assertEquals call
    this.testAssertEquals = function() {
        this.assertEquals(this.foo.returnfoo(), 'foo');
    };

    // an example of a successful assertThrows call (to
    // test whether some call results in an exception)
    this.testAssertThrows = function() {
        this.assertThrows(this.foo.throwfoo, 'foo');
    };
};

// each test case has to subclass from TestCase, which provides
// the .assert* methods and some stubs
ExampleTestCase.prototype = new TestCase;