mozdev.org

XulUnit

resources:

The Metaphor

Remember this:

Easy! Is'nt it?

Some Examples

This is a test:

assertEquals("A simple test", 36, myAge);

Tests should be included in Test Cases to be run. This is a Test Case:

  function test_MyTest(){
    var myAge = 35;
    myAge++;
    assertEquals("A simple test", 36, myAge);
    assertNotNull("Another stupid test", "notNullValue");
  }

But, as Test Cases need some more things that just a function (with assertions) we need to instatiate a TestCase object. This will add result attributes and other jingles to our test function:

  myTestCase = new TestCase("This is my Test Case", test_MyTest);

Test Cases should be added to Test Suites in order to be run. This is done by:

  var myTestSuite =  new TestSuite("TestSuite Name");
  myTestSuite.add(myTestCase);

And, in order to run the Suites (and therefore the tests), we need to create a TestRunner object and add our Test Suites:

  var testRunner = new TestRunner();
  testRunner.add(myTestSuite);

A complete example of test construction can be found in myTests.js file (included in the XulUnit Package).

How to Run Test Runner

After creating your tests, test cases, test suites and a test runner object, you simply need to create a xul page, load any js file with your tests and classes to be tested, and call TestRunner launch method (with no arguments).

This will open a new window, run the tests and show the results. Thats all.

For example

Example at XulUnit Package myTest.xul includes the following code:

  <script src="xulunit.js" />
  <script src="myTests.js" />
  <!-- Any other js files with classes -->

This loads XulUnit classes and the user defined tests.

To run then, you can use a simple button...

      <button label="Run My Tests" onclick="testRunner.launch()"/>

You can also call launch method on the onload attribute of your window.

Assert Functions

These are the assert functions included in XulUnit:

function assert(name, toEval) Asserts toEval expression equals true

function assertTrue(name, toEval) Asserts toEval expression equals true.

function assertFalse(name, toEval) Asserts toEval expression equals false.

function assertEquals(name, value, toEval) Asserts toEval expression equals value.

function assertNotEquals(name, value, toEval) Asserts toEval expression does not equals value.

function assertNull(name, toEval) Asserts toEval expression is null.

function assertNotNull(name, toEval) Asserts toEval expression is not null.

function assertUndefined(name, toEval) Asserts toEval expression is undefined.

function assertNotUndefined(name, toEval) Asserts toEval expression is not undefined.

function assertNaN(name, toEval) Asserts toEval expression is Nan (Not a Number).

function assertNotNaN(name, toEval) Asserts toEval expression is not Nan (Not a Number). I.e. it is a number.

function assertRegExp(name, regExp, toEval) To be done.

function assertTypeOf(name, type, toEval) To be done.

Understanding Test Results

Tests can evaluate to any of four results:

Results follow up to cases and suites with the rule: If all passes, I pass. This means that a case PASSES only if all its tests passes. And same aplies to Suites.

If any of the tests in a case fails, the case will throw a FAIL result (eventhough you can see the results for each individual test).

If any of the tests breaks... the case and the suite will break.

Todo

I know you can't believe it. But there are still features to be added to XulUnit (on which I am working hard). The main left are:

The xulunit project can be contacted through the mailing list or the member list.
Copyright © 2000-2017. All rights reserved. Terms of Use & Privacy Policy.