Entrypoints for running the tests

edit

We use composer to run PHPUnit tests. If you have not already, run composer update in the root of the MediaWiki core repository.

All tests

edit

The entry point for running all tests at once is the composer phpunit:entrypoint command, that must be run in the MediaWiki directory:

$ composer phpunit:entrypoint -- path/to/my/test

Error "Cannot access the database: Unknown database"

edit

If you get this error you may need to set the environment variable PHPUNIT_WIKI, e.g:

$ PHPUNIT_WIKI=wiki composer phpunit:entrypoint -- path/to/my/test

The "--filter" option does not exist

edit

This and similar errors can occur if you omit the -- when invoking the Composer script. Make sure to always use the double dash when running the Composer script, to separate Composer and PHPUnit arguments.

Integration tests

edit

These are tests that are contained in tests/phpunit/integration folders.

$ composer phpunit:integration -- path/to/my/test

Unit tests

edit

These are tests that are contained in tests/phpunit/unit folders.

Run only the pure "unit" tests for MediaWiki core and all extensions and skins:

Terminal

To run specific pure "unit" tests only, use the PHPUnit entrypoint located at vendor/bin/phpunit

Terminal
This documentation assumes that you are running tests in a local development environment. Do not run tests on a real website, bad things will happen!

Linter and code style

edit

To check PHP code style in a file you can run:

composer lint ./some/path/SomeFile.php
composer phpcs ./some/path/SomeFile.php

To run auto-fixes execute PHPCBF (PHP Code Beautifier and Fixer) like so:

vendor/bin/phpcbf ./some/path/SomeFile.php

All of these commands take a file or directory as a parameter. If you modified a few files in different folders, you can temporarily add your commands to the scripts section in composer.json (just to run the commands quicker).

Select by directory or file

edit

Running all tests from all core components and installed extensions may take a while. During development you may want to focus on just a particular test suite or directory of test suites. Pass the directory or file to the phpunit command.

For example, if you're working on a patch that modifies includes/ResourceLoader, with a test covering it under unit/includes/ResourceLoader/, then you might run the tests like so:

Terminal

To run tests for a specific extension or skin:

Terminal

Select by test name

edit

If you're debugging a specific test case and are adding breakpoints or var_dump statements, it may be useful to filter even more closely than by the file, so as to skip past all other invocations of the same underlying code. To do this, use the --filter option to match substrings of the test name. For example, given a test suite like the following:

namespace MediaWiki\Tests\ResourceLoader;

class FileModuleTest extends MediaWikiIntegrationTestCase {
	protected function setUp(): void {
		parent::setUp();
		// ...
	}

	public function testLessCompiler() {
		// ...
	}

	public function testGetScriptPackageFiles() {
		// ...
	}

	public function testGetVersionHash() {
		// ...
	}
}

You can combine the directory selection with --filter to run only the second testGetScriptPackageFiles test case, like so:

Terminal


Select by group

edit

Each test case can be grouped using the @group annotation in the test case file. The group names may look obscure at first, we will describe them later on in the Writing unit tests chapter.

To only run tests in a particular group, use the --group option.

This may be slow and will be silent for a few seconds before the tests start, because it still requires PHPUnit to load and data-provide all test suites (including extensions) before the group filter is applied. For fast results, select by directory instead.
Terminal

To find all available groups, run $ composer phpunit:entrypoint -- --list-groups to print the available test groups. To run a particular test group, pass it to phpunit, for example:

$ composer phpunit:entrypoint -- --group Editing

Output format

edit

To see the name of each running test, use the TAP format by passing the --tap option to PHPUnit:

Terminal

This format is great to filter the output for non passing test. For example with the grep command:

Terminal

phpunit also provide a kind of checklist that give out a great output for people not familiar with tests or shells: the testdox format:

Terminal

An unchecked box ([ ]), means the test failed, such as the one named « RC ns filter association » above.

How it works

edit

Database

edit

Integration tests that involve a database are automatically run against a temporary clone of your current wiki's database (the one configured in LocalSettings.php).

Because of this, it is important that the MediaWiki installation being tested has an up to date and correct database, or error messages (such as "TestUser.php: Can't create user on real database") will result.