Manual:PHP unit testing/Writing unit tests for extensions

The recommended bootstrapping method for extensions is to include your tests in the  directory of your extension (alternatively, you can use the UnitTestsList hook). Via either of these methods 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
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   comment above each test function if you wish to further isolate your tests.

Single test file
If you are using vagrant and get an error message: "Cannot access the database: Unknown database", try adding  to the command line.

Create a custom test suite
runs with a default test suite defined in. You can alter that test suite by doing the following:  Copy the  file to  . Edit the suite.extensions.xml file: Save the file. Run the tests with the custom suite configuration.   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.
 * remove any unwanted  from the   node.
 * add or make sure a  node exists with the following:

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

Exclude groups
You can then exclude specific test groups by adding them to the,   node in  : Tests are considered part of a group when an  comment is placed above the class or function.
 * covers any test that has not been given an  comment.
 * phpunit also considers an  comment as an   comment, but   is preferred.

Add your tests to Jenkins
MediaWiki maintains a Jenkins server that automates the software building process. This includes automatically running MediaWiki tests as software is committed to the MediaWiki code repository. If your extension exists in that repo, you can add your extension’s test to the Jenkins build process.

1. Clone the  repo and create a new git branch. 2. Open the  file in the project 3. Locate the  section 4. Find your extension under the  or add it 5. Commit the changes 6. Submit the changes to gerrit for code review 7. Once the change has been merged, your extension’s tests will automatically run when you commit changes to the MediaWiki code repo.

Extension dependencies
If your extension requires other extensions to be present in order to work properly, add them as dependencies: