Extension:MWUnit/Getting started

This tutorial assumes you have MWUnit installed and working. In this tutorial, you will learn how to write and run a simple unit test for the fictional template Template:AddTen. The documentation on how to install MWUnit can be found on this page.

TutorialEdit

Creating a testEdit

We will create a new template, Template:AddTen. This template adds 10 to the first argument, or 10 to 0 if no argument is given. 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 appropriately call Test:AddTen. Here is what these two pages may look like:

Template:AddTenEdit

<noinclude>
This is the "AddTen" template.

{{AddTen|12}} -> 22
{{AddTen|0}}  -> 10
{{AddTen}}    -> 10
{{AddTen|a}}  -> Expression error: Unrecognized word "foo".
</noinclude>
<includeonly>
{{#expr: {{{1|0}}} + 10 }}
</includeonly>

Test:AddTenEdit

<!-- This test tests Template:AddTen -->

<testcase name="testAddTenToZero" group="AddTen tests">
<!-- This test tests whether giving 0 to the template returns 10 -->
{{#assert_equals: {{AddTen|0}} | 10 }}
</testcase>

<testcase name="testAddTenToNothing" group="AddTen tests">
<!-- This test tests whether giving nothing to the template returns 10 -->
{{#assert_equals: {{AddTen}} | 10 }}
</testcase>

<testcase name="testAddTenToString" group="AddTen tests">
<!-- This test tests whether giving a string to the template returns an error -->
{{#assert_error: {{AddTen|foobar}} }}
</testcase>

<testcase name="testAddTenToNegativeTen" group="AddTen tests">
<!-- This test tests whether giving a negative number to the template works -->
{{#assert_equals: {{AddTen|-10}} | 0 }}
</testcase>

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 test tests whether the first parameter can be left empty, and so on.

As you can see, each test has the same "group" attribute. Grouping similar tests together make it easier to execute them in bulk, since we can run every test in a certain group. Tests in the same group are not required to be on the same page.

Running testsEdit

There are several ways to run a test. The most obvious way is to click the "Run tests" button in the sidebar on a test page. Clicking this button will run all tests on the current test page. If you are using a skin that hides this button, the shortcut Alt+Shift+a can also be used.

Another way to run a test is to go to the special page Special:UnitTests.

See alsoEdit