Changes

Jump to: navigation, search

Real World Mozilla Unit Testing Lab

1,504 bytes added, 19:38, 28 February 2007
Instructions
== Instructions ==
Mozilla [http://developer.mozilla.org/en/docs/Mozilla_automated_testing currently uses ] three unit test methods:
# The first is '''xpcshell''' for C++ unit tests that run on the command line. These are the best method to use whenever possible. Using them effectively requires composing code as stateless functions that return values whenever possible--always a good practice.
For purposes of testing our FirstXpcom component, we will use xpcshell tests. This will allow us to easily write tests to insure the functionality of our class's methods.
You're correct that you find too many * Begin by creating a new directory to hold our tests in the tree :/. We're working on that. browser/components/places/tests have some good examples.
$ cd mozilla/extensions/firstxpcom
$ mkdir test
* Now copy the xpcshell test-harness code into this directory:
$ cd mozilla/extensions/firstxpcom/test
$ cp -r ../../../tools/test-harness/xpcshell-simple/example/* .
* Now create some Unit Tests for your component so you can easily test things as you go (see This will result in the following for more details -- httpfile/directories being copied to '''test'''://developer.mozilla.org/en/docs/Writing_xpcshell-based_unit_tests)
- mkdir mozilla/extensions/weblock/test $ ls- cp mozilla/tools/test-harness/xpcshell-simple/example/ mozilla/extensions/weblock/test- edit mozilla/extensions/weblock/test/ CVS Makefile.in to be: unit
-----------start--------------* The file, '''test/Makefile.in''', needs to be modified:
# Note: DEPTH should be set to the relative path to mozilla/ DEPTH = ../../.. topsrcdir = @top_srcdir@ srcdir = @srcdir@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk # Note: set the test module's name to test_<yourmodule> MODULE = test_firstxpcom # This is a list of directories containing tests to run, separated by spaces. # Most likely, tho, you won't use more than one directory here. XPCSHELL_TESTS = unit include $(topsrcdir)/config/rules.mk # Note: Invoke any additional (non-xpcshell) test programs here. check::
* Your component's Makefile needs to include $(DEPTH)this new test directory. Modify '''mozilla/extensions/configfirstxpcom/autoconfMakefile.mkin''' (change shown in bold):
DEPTH = ../.. topsrcdir = @top_srcdir@ srcdir = @srcdir@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk MODULE = weblockfirstxpcom DIRS = public \ src \ $(NULL) '''# Adding xpcshell unit tests''' '''ifdef ENABLE_TESTS''' '''DIRS += test''' '''endif''' XPI_NAME = firstxpcom # A Unique ID for your extension INSTALL_EXTENSION_ID = firstxpcom@senecac.on.ca # Will create a .xpi in /mozilla/$(MOZ_OBJDIR)/dist/xpi-stage/ XPI_PKGNAME = firstxpcom # install.rdf will tell Firefox how to install our extension and # this says, "copy install.rdf into our extension dir and xpi" DIST_FILES = install.rdf include $(topsrcdir)/config/rules.mk
include $* Recreate your component's Makefile (topsrcdirmake sure you use your own objdir name below)/config/rules.mk:
_TEST_FILES = test_all.sh_UNIT_FILES = head.js \ tail.js \ $ cd mozilla/''objdir'' $(NULL)# Not sure if I need to alter these...libs:: $(_UNIT_FILES) $(INSTALL) $^ $(DIST)/binbuild/autoconf/testmake-harnessmakefile extensions/xpcshell-simplefirstxpcom $ make
libs:: $(_TEST_FILES) $(INSTALL) $^ $(DIST)* Write you JavaScript unit tests for your FirstXpcom component in '''mozilla/extensions/firstxpcom/test/binunit/test_firstxpcom.js''':
function run_test() {
// Create an instance of FirstXpcom to test
try {
const cid = "@senecac.on.ca/firstxpcom;1";
var obj = Components.classes[cid].createInstance();
obj = obj.QueryInterface(Components.interfaces.IFirstXpcom);
} catch (err) {
do_throw(err);
return;
}
// Test Add
var result = obj.add(1, 2);
do_check_eq(3, result);
// Test Name get and set
var name = "New String";
obj.name = name;
do_check_eq(name, obj.name);
return true;
}
----------end------------------- - Add the following to mozilla/extensions/weblock/Makefile.in ifdef ENABLE_TESTSDIRS += testendif - rebuild your Makefiles like so: cd mozilla/objdir../build/autoconf/make-makefile extensions/weblockmake - finally* Finally, run your test suite once (it is empty right now, but this is how you do it)make check
$ cd mozilla/''objdir''/extensions/firstxpcom
$ make check
== Resources ==

Navigation menu