FANDOM


Unit testing in MediaWiki for its JavaScript code base is performed using the QUnit JavaScript Testing framework..



The unit tests in core are located in the tests/qunit directory[1]. Tests are organized into a directory structure that matches the directory structure of the code that they are testing. For example: The unit tests for file resources/mediawiki.util/mediawiki.util.js can be found in tests/qunit/suites/resources/mediawiki.util/mediawiki.util.test.js.



Extensions add their QUnit tests on the ResourceLoaderTestModules hook.



Running the unit tests Edit

File:SpecialJavaScriptTest-QUnit MW1.19-r110313 Chrome.png



Unit tests are run from the browser. To do so:

  1. Set $wgEnableJavaScriptTest to true.
  1. Go to Template:Blue on your wiki and select the framework of choice, or go to Template:Blue directly 
  1. No step 3, tests are being run now!



With some effort you can also run Qunit tests from the command-line in a headless browser like PhantomJS. The VisualEditor extension uses node.js to do this.



Options Edit

File:Screenshot-MediaWiki QUnit CompletnessTest.png



Available skins
On top is a drop down  menu to switch active Skin. Selecting a skin will refresh the page and set the chosen skin via the useskin query parameter to index.php.
noglobals
Core URL parameter of QUnit[2]
notrycatch
Core URL parameter of QUnit[2]
debug
Toggle debug mode of ResourceLoader
completenesstest
Whether or not to load jQuery QUnit CompletenessTest.
mwlogenv
When enabled, environment setup and teardown will be logged to the console.
Module
Select a module from the list to immediately run it.



You can supply a query string parameter filter=string to the URL and QUnit will only run modules matching string (case-insensitive); prepend ! to the string to exclude modules matching string.



How to help? Edit

Run tests before committing Edit

Make it a habit to run unit tests before committing your submitting your changes to Gerrit. Any problems that you didn't cause? See if you can find a cause and let the committer know by leaving a comment in Gerrit.



Write unit tests Edit

Always provide unit tests for new functionality. We're also looking to expand our unit test coverage of already existing modules in the MediaWiki JavaScript library. Run the CompletenessTest and find out which methods don't have unit tests yet and write one.



Write a unit test Edit

TODO: Incomplete



  • The file should be named after the module (or file) it is testing.



QUnit Edit

  • Inside the test suite file should be one, and one exactly, call to QUnit's module(). The string passed to module() must match the filename (but without the .test.js part). This is to enable sectioning in the QUnit output as well as for TestSwarm to allow distributed and asynchronous testing of each module separately.



How to register a test suite Edit

MediaWiki core Edit

Core test suites need to be added to the registration of the mediawiki.tests.qunit.suites module. Do so in QUnitTestResources.php (in ./tests/qunit/).



So if you created a test suite for mediawiki.foobar, then that test suite should be stored in ./tests/qunit/suites/resources/mediawiki/mediawiki.foobar.test. Then you would add that file path to the "scripts" array. And, unless already, add the mediawiki.foobar module as a dependency (since that module must be loaded before the test suite can run).

'mediawiki.tests.qunit.suites' => array(
 
<span class="Apple-tab-span" style="white-space:pre"> </span>'scripts' => array(
 
<span class="Apple-tab-span" style="white-space:pre"> </span>'tests/qunit/suites/resources/mediawiki/mediawiki.test.js',
 
+<span class="Apple-tab-span" style="white-space:pre"> </span>'tests/qunit/suites/resources/mediawiki/mediawiki.foobar.test.js',
 
<span class="Apple-tab-span" style="white-space:pre"> </span>'tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js',
 
<span class="Apple-tab-span" style="white-space:pre"> </span>'tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js',
 
<span class="Apple-tab-span" style="white-space:pre"> </span>),
 
<span class="Apple-tab-span" style="white-space:pre"> </span>'dependencies' => array(
 
<span class="Apple-tab-span" style="white-space:pre"> </span>'mediawiki',
 
+<span class="Apple-tab-span" style="white-space:pre"> </span>'mediawiki.foobar',
 
<span class="Apple-tab-span" style="white-space:pre"> </span>'mediawiki.user',
 
<span class="Apple-tab-span" style="white-space:pre"> </span>'mediawiki.util',



Extensions Edit

Extensions need to register their tests suits as a module. See Manual:Hooks/ResourceLoaderTestModules for more details.



Tips Edit

  • The tests should work in all language environments, but sometimes may fail if $wgLanguageCode is not "en". If your wiki's language is not "en" and tests fail mysteriously, try changing $wgLanguageCode to "en" and running the tests, and then try with your language again.



See also Edit



Notes Edit

  1. Before Template:R , unit tests were located in the resources/test directory.
  2. 2.0 2.1 http://docs.jquery.com/QUnit#URL_Parameters

 

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.