Extension:MWUnit/Fixtures and global state

From mediawiki.org
Jump to navigation Jump to search

A big part of writing tests is writing the code that sets up the world in a known state. This known state is called the fixture of the test.

In our Foobar test, our fixture was a simple variable defined using the Variables extension. Most of the time, though, the fixture of a test is much more complex and the amount of code grows accordingly. It would be very tedious if the fixture had to be set up for each and every test.

MWUnit supports sharing the fixture between multiple tests. Each test on the same test page shares the same fixture. The fixture for a test page is created in the <setup> function and destroyed in the <teardown> function. These functions are ran for every test case on the test page. A large part of the fixture is cleaned up after each test case, so in many cases a <teardown> function is not required. Persistent data, such as database entries, are not automatically cleaned up.

<setup>
{{#vardefine: foobar | boofar }}
</setup>

<testcase name="testFoobar" group="example tests">
{{#assert_equals: {{#var: foobar }} | boofar }}
</testcase>

...

The global state can be accessed and changed in a test case, but this change will not affect other test cases. The setUp and tearDown functions are run for every test case. It is important to note that a state changing function will be run multiple times.

PHP Global state[edit]

If the configuration variable $wgMWUnitBackupGlobals is set to true (default), all global PHP variables will backed-up before and restored after each test case. This way, changes made to global variables in one test, do not affect other tests. Global objects are not backed up.

See also[edit]