Extension:MWUnit/Writing tests

From mediawiki.org

This page documents how to write tests for MWUnit. The example below shows how to write a test that exercises a specific template. The example introduces basic concepts and conventions for writing tests with MWUnit:

  1. The test for a template Foobar goes on the page Test:Foobar;
  2. The tests are between <testcase> and </testcase> tags;
  3. The name of a test typically starts with "test", and is written in camel case or snake case;
  4. Inside the tests are a number of assertions, such as {{#assert_equals}};
<!-- This test covers the template "Echo". -->
<!-- Note: This test makes use of the extension "Variables" to show how global state works in tests -->

{{#vardefine: boofar | foobar }} <!-- This statement is ignored, since it is not in any tag MWUnit recognizes -->

{{#vardefine: foobar | boofar }}

<testcase name="testFoobarIsBoofar" group="Foobar tests">
{{#assert_equals: {{#var: foobar }} | boofar }}

<testcase name="testIsBoofar" group="Foobar tests">
{{#assert_equals: {{Echo| {{#var: foobar}} }} | {{#var: foobar}} }}

<testcase name="testEchoFunctionality" group="Foobar tests">
{{#assert_is_numeric: {{Echo|10 }} }} 
{{#assert_greater_than: {{Echo|100 }} | 10 }}
{{#assert_that: {{Echo|true }} }}
The mwunit-createtests user right is required to edit pages in the Test namespace.

Testing in user context[edit]

Further information: Extension:MWUnit/Contexts

By default, tests are run in a canonical context. This means the parser is initialized with an anonymous user and the content language. This is done to make the output of the test more consistent across users and languages.

There is a multitude of ways to get information about the current user. Therefore it is possible some extensions will still output user-specific information even when running in a canonical context. This is outside of MWUnit's control.

You can choose to run a specific test in user context. A test run in user context will initialize the parser with the current logged in user or the user specified in the @user annotation and the language that user has set. This can be useful for testing templates that depend on which user is logged in or which language is selected.

The context of a test can be set by using the context annotation, as shown in this example:

<testcase name="testUserContext" group="test" context="user">
<!-- Run this test as the current logged in user -->

<testcase name="testCanonicalContext" group="test" context="user" user="Foobar">
<!-- Run this test as the user "Foobar" -->

See also[edit]