Using Mochitests in a XULRunner Application

Mochitest is an automated testing framework built on top of the MochiKit JavaScript libraries. It's just one of the automated regression testing frameworks used by Mozilla. Tests report success or failure to the test harness using JavaScript function calls. Mochitest test scripts can be used to test XUL chrome and web content. When testing XULRunner based applications, you will typically be testing XUL chrome.

The Mochitest framework is part of the Mozilla build system, therefore, the simplest way to integrate it into your XUL application is to build your application using the build system. Since Firefox is the currently the primary consumer of the Mochitest system, we'll need to modify and replace some of the parts.

Basic Requirements

Because Mochitest is built into the Mozilla build system, there is not much required to integrate it with your application. As stated in Creating XULRunner applications with the Mozilla build system, you can use a full XULRunner build or a XULRunner SDK to build your application. Mochitest is supported in both scenarios as well. The Mochitest article has information on writing test scripts. You can also find details on using JavaScript test functions and the difference between content and chrome tests.

Mochitest has changed somewhat between Mozilla 1.9.0 and Mozilla 1.9.1 development cycles. The basics are the same for each release. The variable used to determine if tests are to be created during the build changed:

Mozilla 1.9.0: MOZ_MOCHITEST
Mozilla 1.9.1: ENABLE_TESTS 

Enabling Mochitests

Let's assume you have a XUL application pulled into a folder called xulapp. To make the example a bit more realistic, links are provided to files used to enable Mochitests for the XUL Explorer application.

Enable test support to top-level file

  • xulapp/ - Tell the Mozilla build system about the test harness folders. The section below is the relevant part.
tier_testharness_dirs += \
  testing/mochitest \
  xulapp/tests \

Add test runner script and support harness and overlay to the application

These files can be added without making any changes. They are the minimum needed to host the test environment in your application.

Add individual test scripts

Test scripts can be placed anywhere in your source tree. Make sure your makefiles are setup to copy the test scripts into the testing folder.

  • xulapp/chrome/ - Tell the build system to look in the xulapp/chrome/test folder to find our test scripts. The section below is the relevant part.
DIRS += tests