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; because pages are parsed from top to bottom, tests that are above the 'fixture' would not have the same global state as tests below it. The fixture is cleaned up for each test case. This means the page is parsed again for every test case.

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



...

The global state can be accessed and changed in a test case, but this change will not affect other test cases. The global state is rebuilt for every test case. This is done by reparsing the entire page. It is important to note that state changing function will be run multiple times.

Global state
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.