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.

Register your tests with UnitTestsList
Let's assume our extension is named Fruits.
 * 1) Create a hook function for onUnitTestsList.
 * 2) Register the hook with UnitTestsList.

Create your tests
Let's say our Fruits extension has a class called.
 * 1) Create a test file in your extension’s tests/phpunit/ directory, named
 * 2) * Make sure that the file name ends in  or else phpunit will not be able to discover it.
 * 3) Create a class in that file named   and extend the MediaWikiTestCase class.
 * 4) The class must contain a   that calls   as its first task.
 * 5) The class should contain a   that calls   as its last task.
 * 6) Each of your test functions should :
 * 7) * be.
 * 8) * begin their function name with.
 * 9) It’s a good idea to add an   comment to the class. This group name can then be referred to later in order to run tests only in this group or exclude them. You can also place an @group comment above each test function if you wish to further isolate your tests.

Create a custom test suite
runs with a default test suite defined in Tests for any extension loaded in   will be run. If you want to isolate tests to only your extension, you can exclude groups of tests within the  file.
 * 1) Edit the suite.extensions.xml file.
 * 2) * remove any any unwanted testsuite from the testsuites node.
 * 3) * alter the extensions testsuite section so that the  and   are present.
 * 4) * save the file.
 * 5) Run the tests with the custom suite configuration.

List groups
You can get a list of test groups available for a specific configuration by running:

Exclude groups
You can then exclude specific groups by adding them to the groups, exclude node in. Note:
 * covers any test that has not been given an  comment.
 * phpunit also considers an  comment as an @group comment, but @group is preferred.