Extension:MWUnit/Contexts
Tests can be run in a number of different 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 consistent across users and languages.
However, sometimes it is easier or necessary to run a test in the context of an existing user. For instance, because the template makes use of the MyVariables extension and the output of the template is dependent on who is viewing it. For this reason, tests can be run in a "user" context. This can achieved by using the "@context" annotation:
<testcase name="foo" group="bar" context="user"> ... </testcase>
When this annotation is present, the test is run as the current user. This means the parser is initialized with the User object of the current user.
Running a test as another user
editmwunit-mock-user
right and that the $wgMWUnitAllowRunningTestAsOtherUser
is set to true
It is also possible to run a test as if another user is logged in. This can be achieved through the "@user" annotation:
<testcase name="foo" group="bar" context="user" user="Xxmarijnw"> ... </testcase>
The above snippet would run the test case as if the user Xxmarijnw
were running it in "user" context. When running a test as another user, the $wgUser
global is temporarily replaced, the RequestContext is altered and the Parser is initialized with the mocked user's User object.
mwunit-mock-user
right since they can simply run those parser functions via a mocked test case.See also
edit- Extension:MWUnit/Getting started - Getting started with MWUnit
- Extension:MWUnit/Running tests - Documentation on running tests
- Extension:MWUnit/Writing tests - Documentation on writing tests