Extension:MWUnit/Extending MWUnit

This page has information about MWUnit's code. It is intended for developers that want to modify it or write code that ties into it. It is not intended for site administrators or end-users.

Defining new assertions
MWUnit provides a mechanism to define new assertions. To define a new assertion, an extension must register the MWUnitGetAssertionClasses hook:

Any assertion class must implement the  interface. This interface has the following methods:


 * getName: Returns the name of this assertion as used in the parser function magic word, without the `assert_` prefix.
 * shouldRegister: Returns true if and only if this assertion should be registered with the Parser. Allows extensions to define conditional assertions.
 * getRequiredArgumentCount: Returns the number of arguments required by the assertion.

These methods define metadata used by the assertion factory to register the assertion class. Additionally, the  method should be implemented. This method has variable arguments and is therefore not included in the interface.

The first argument passed to the  method is the reference to the error message, shown when the assertion fails. This parameter should be set in the function. The next parameters are the parameters passed to the parser call, with the last argument being the optional user-supplied error message. The function should return  if the assertion succeeded,   if the assertion failed or   when the assertion is deemed invalid/risky (i.e. invalid parameters or an exception occurred).

We will be defining the assertion, that only succeeds when the first argument is   and the second argument is. An example implementation of this looks like this:

All hooks

 * MWUnitAfterTestComplete : called after a test has completed, but before the test is added to test results
 * MWUnitBeforeRegisterTestCases : called before test cases is registered; cancels the registration if  is returned
 * MWUnitBeforeRunTestCase : called before a test case is ran
 * MWUnitGetReservedFunctions : called when the list of reserved parser functions is collected; can be used to add or remove items from the list
 * MWUnitGetAssertionClasses : called when the list of assertions classes is retrieved; can be used to add new assertions to MWUnit
 * MWUnitBeforeFirstTest : called before the first test is ran; the tests will not be run if  is returned
 * MWUnitCleanupAfterPage : called during the cleanup of each fixture, after each test case; can be used to reset globals, restore backed up callbacks, etc.