Extension:MWUnit/Getting started

This tutorial assumes you have MWUnit installed and working. In this tutorial, you will learn how to write a simple unit test and how to run that test.

The documentation on how to install MWUnit can be found on this page.

Creating a test
We will create a new template, which we will call. This template adds  to the first argument. It returns an error if and only if the first argument is not a number. This template makes use of the ParserFunctions extension.

We will also create the associated test page for this template, which we will call.

Here is what the two pages might look like:

Template:AddTen
This is the "AddTen" template.

-> 22 -> 10    -> 10  -> Expression error: Unrecognized word "foo".

Test:AddTen








The test page we have created contains four test cases. Each test case should test exactly one distinct thing of the template that is being tested. In this case, the first test case tests if adding 10 to 0 results in 10, the second tests whether the first parameter can be left empty, and so on.

These test cases all share the same global state, defined at the top of the test page. This is what we call a fixture. This test does not need global state, so nothing is defined at the top of the page.

As you can see, each test has the same "group" attribute. Grouping similar tests together makes it easier to execute them in bulk, since we can easily run every test in a certain group. Tests in the same group are not required to be in the same fixture. This means we can have two tests in the same group, but on two different test pages. When we execute the group of tests, both pages are executed separately and do not share global state.

Running tests
There are several ways to run a test. The most obvious way, is to click on the "Run unit tests" button in the side bar of a test page. Clicking this button will run all tests on the current test page. If you are using a skin that does not utilize the sidebar, the shortcut  can also be used.

Another way to run a test, is to go to the special page. This special page contains two forms, one for running tests in a group, and one for running an individual test. You must choose the group or test you want to execute, and click. Depending on the number of tests and the complexity of the test, it might take some time for the results to be shown. Please be patient.