Extension:MWUnit/Fixtures and global state

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. Usually, the fixture is defined at the top of the test page. This way, all tests do indeed share the same fixture. It is technically possible to create the fixture in between two tests, but this is highly discouraged. The fixture is cleaned up for each test page.

A fixture can be defined by adding some global code to the test page:



...

Global state
Tests have access to global state, because that is how fixtures work in MWUnit. However, this also means that tests can change or create new global state. This can lead to unexpected behaviour:





It is recommended not to change or create global state from a test. Instead, you should use a fixture to create the world (global state) your tests will run in.

If the configuration variable  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.