Manual:PHP unit testing/Running the tests

From mediawiki.org
Jump to navigation Jump to search
MediaWiki Flower
MediaWiki Flower
« Unit testing
PHPUnit testing

Entrypoints for running the tests[edit]

If you have not already, run composer update in the root of the MediaWiki core repository.

vendor/bin/phpunit[edit]

The standard way to run PHPUnit is with the entrypoint located at vendor/bin/phpunit

Terminal

Composer script[edit]

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

Terminal

Wrapper script[edit]

We use a wrapper script to run integration tests. The wrapper script phpunit.php is located in the tests/phpunit directory and is invoked using the PHP command:

$ cd tests/phpunit
$ php phpunit.php
This documentation is written assuming that you are running tests in a local development environment. Do not run tests on a production system, bad things will happen!

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

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:

class ResourceLoaderFileModuleTest 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 tests/phpunit/$ php phpunit.php --list-groups to print the available test groups. To run a particular test group, pass it to phpunit, for example:

tests/phpunit/$ php phpunit.php --group Editing

Output format[edit]

To see the name of each running test, uses 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.