WikiUnit (source) is a script that runs testcases for other userscripts and gadgets.

Installation

edit
For English Wikipedia only
Add the following line to your common.js:
importScript("User:Evad37/WikiUnit.js"); // [[User:Evad37/WikiUnit]]
For other wikis
Add the following line to your common.js on another wiki, or to your global.js on Meta:
mw.loader.load("//en.wikipedia.org/w/index.php?title=User:Evad37/WikiUnit.js&action=raw&ctype=text/javascript"); // [[w:User:Evad37/WikiUnit]]

Usage

edit

For scripts which have a /testcases.js subpage, this WikiUnit will run the test on that page when you edit the script and show preview or changes. Those buttons are relabeled "Show preview & tests" and "Show changes & tests". The tests will be run against the unsaved code in the editor textbox.

For running unit tests against the current (saved) code, the script adds a "Run tests" tab on both the script page and the /testcases.js subpage.

WikiUnit works for scripts in both MediaWiki namespace and User namespace. When testcases are in another user's userspace, you will be asked for confirmation before running testcases. Check that the testcases page does not contain nefarious code that could compromise your account before proceeding.

Writing tests

edit

WikiUnit loads QUnit to run unit tests. Tests are defined on a /testcases.js subpage of a javascript page. The /testcases.js script contains QUnit.tests, perhaps grouped into QUnit.modules. See the QUnit cookbook and documentation for more details.

Testcases execute in the same scope as the script they test, and can access any functions or variables declared in the scripts outer scope. Alternatively properties can be added to the `window` object, which can be accessed by both the testcases and the script being tested – but be careful to use unique names that are unlikely to conflict with other scripts.

Gadgets which need to load ResourceLoader dependencies should specify those depenedencies in a comment in the top of the script, like this:

/* wikiunit dependencies=mediawiki.api,mediawiki.util,oojs-ui-core,oojs-ui-windows */

Notices

edit

The following notices are suggested to be placed at (or near) the top of the script and testcases pages, to inform users about the testcases and how to run them.

For the script:

/*
 * ###############################################################################
 * #                                                                             #
 * #  This script has unit tests on its /testcases.js subpage. Please install    #
 * #  the WikiUnit script [[User:Evad37/WikiUnit]] and run tests before saving.  #
 * #                                                                             #
 * ###############################################################################
 */
(For gadgets, this should placed in the initial comment, under the GLOBAL GADGET FILE warning box)

For the /testcases.js subpage:

/* Unit tests for SCRIPT_NAME_HERE.js
 * -----------------------------------------------------------------------------
 * To run these tests, you need to install the WikiUnit script. Add the line
 *  importScript("User:Evad37/WikiUnit.js");
 * to your common.js. See [[User:Evad37/WikiUnit]] for further information.
 * -----------------------------------------------------------------------------
 */

Examples

edit