Manual:PHP unit testing/Writing unit tests for extensions

The recommended bootstrapping method for extensions is to use the UnitTestsList hook. By registering your tests with this hook you can use the mediaWiki core bootstrap support for things like autoloading classes, global functions etc. Unless your code is truly a standalone library, it's NOT recommended to include a few random MediaWiki core files with custom bootstrapping and attempt to make your tests stand alone.

To exclusively run your extensions tests, duplicate the suite.xml file in tests/phpunit/suite.xml to suite.extensions.xml only include the "extensions" testsuite as a child of testsuites. Then run the test with the command:. Do not call  directly, as it will not load the MediaWiki configuration!

To test a single file you can pass the test file as a parameter to.

Code
Let's assume our extension is named Fruits. We register a callback for the UnitTestsList hook:

This will each of the test files automatically.

Then, on to the actual tests. Let's say our Fruits extension features a class called  which we want to write unit tests for independent of how the class is integrated. Name the test class and file after the source file it is written for. So for class  we write a class named  : Make sure that the file name ends in, or else phpunit will be unable to discover it (note this is separate from the   we used earlier, it is the same principle, but it being registered doesn't mean it will run, PHPUnit has its own class discovery as well. The class and file has to end in "Test").

To run the tests: