Extension:MWUnit/Writing tests
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:
- The test for a template
Foobar
goes on the pageTest:Foobar
; - The tests are between
<testcase>
and</testcase>
tags; - The name of a test typically starts with "test", and is written in camel case or snake case;
- 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 --> <setup> {{#vardefine: foobar | boofar }} </setup> <testcase name="testFoobarIsBoofar" group="Foobar tests"> {{#assert_equals: {{#var: foobar }} | boofar }} </testcase> <testcase name="testIsBoofar" group="Foobar tests"> {{#assert_equals: {{Echo| {{#var: foobar}} }} | {{#var: foobar}} }} </testcase> <testcase name="testEchoFunctionality" group="Foobar tests"> {{#assert_is_numeric: {{Echo|10 }} }} {{#assert_greater_than: {{Echo|100 }} | 10 }} {{#assert_that: {{Echo|true }} }} </testcase>
mwunit-createtests
user right is required to edit pages in the Test
namespace.Testing in user context
[edit]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.
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> <testcase name="testCanonicalContext" group="test" context="user" user="Foobar"> <!-- Run this test as the user "Foobar" --> </testcase>
See also
[edit]- Extension:MWUnit/Getting started - Getting started with MWUnit
- Extension:MWUnit/Running tests - Documentation on running tests
- Extension:MWUnit/Organizing tests - Documentation on how to organize tests
- Extension:MWUnit/Assertions - List of available assertions
- Extension:MWUnit/Annotations - List of available test annotations
- Manual:Unit testing - Manual page about unit testing