Necko:UnitTests
Overview
Unit tests for the networking library live under netwerk/test/unit/. The test files are javascript files that are executed using xpcshell (via the xpcshell test harness).
Unit tests are files named with the prefix test_. Code may be shared across unit tests by adding it to a file named with the prefix head_.
Basics
Each test file must define a function named run_test that will be called to execute the test. Any exception thrown from this function causes the test to fail. If the function runs to completely without throwing an exception then the test succeeds.
However, for tests that require asynchronous operations, completion of the test can be deferred by calling do_test_pending, which must be paired with a call to do_test_finished when the asynchronous operation completes.
When the test is run, a log file is generated. That log file will contain the output from the test, plus any error messages with call stacks.
Functions
do_throw(messageText)- Call this function to report an error and exit the test. The argument is a string that will be reported in the test's log file.
do_check_eq(a, b)- Call this function to assert that two objects are equal. If not equal, an exception is logged and the test case is halted.
do_check_neq(a, b)- Call this function to assert that two objects are not equal. If equal, an exception is logged and the test case is halted.
do_timeout(delay, expr)- Call this function to schedule a timeout. The given expression will be evaluated after the specified delay (in milliseconds).
do_test_pending()- Delay exit of the test until
do_test_finished()is called.do_test_pending()may be called multiple times, anddo_test_finished()must be paired with each before the unit test will exit.
do_test_finished()- Call this function to inform the test framework that an asychronous operation has completed. If all asynchronous operations have completed, then the unit test will exit.