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. In Fruits/Fruits.php main file add:

This will pick all test files automatically.

In tests/AppleTest.php add: Make sure that your test file ends in, or else phpunit will not detect it outside of the complete unit test framework.

To make easier to run test, you could create a Makefile something like this:

Then you can run make or Make SomeFileTest.php (but not multiple files at once).

Alternative if you don't mind typing long paths, just do: which is the format that Jenkins uses.