Manual:PHP unit testing/Running the unit tests

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


First run[edit]

Make sure you have installed PHPUnit.

Unit tests are run from the command line as there is no graphical user interface as of January 2012. Although phpunit provide a bootstrapping facility, we use a wrapper script. It makes sure the MediaWiki framework is properly initialized using your LocalSettings.php file, then directly call PHPUnit API. Invoking phpunit as-is will simply not work.

The wrapper script phpunit.php is located in the tests/phpunit directory and is invoked using the PHP interpreter:

$ cd tests/phpunit
$ php phpunit.php

The wrapper script is itself a MediaWiki maintenance script, and will pass all arguments to PHPUnit. To get a list of possible arguments have a look at phpunit --help.

On Windows-family operating systems, run the run-tests.bat batch file

Test database[edit]

That the test harness is used for unit and integration tests, and the latter often require a database to test against. This is done by cloning the database (the one that's referenced in LocalSettings.php) to temporary tables and running the tests against those.

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.

Using default test groups[edit]

To make it easier to newcomers, tests/phpunit in core has a makefile providing some sane targets that are easier to remember than some long shell commands.

  • Entering make target runs sub-groups of tests.
  • Entering make help prints the list of targets and their descriptions.

You will obviously need the make utility. The sub-group names may look obscure to you right now, we will describe them later on in the writing unit tests section.

For instance, to run all tests that do not require a database connection, specify the make target databaseless:

Tournesol.png mediawiki.demo.wmflabs.org - TTY

As said above, a list of other targets is always available using the special help target (make help).

Running other sets of tests[edit]

Running group of tests might be long and tedious, specifically when you are only altering a very specific part of the MediaWiki codebase. For example, you might be implementing some IPv6 functionality in the includes/libs/IP.php file. In the test suite, that file code is covered by includes/libs/IPTest.php which you can pass as an argument to our PHPUnit wrapper.

Below, "wiki" is the name of the database to use, which is required as of 1.29, but you may have to omit it in older versions of MediaWiki.

Running the individual tests from includes/libs/IPTest.php looks like:

Tournesol.png mediawiki.demo.wmflabs.org - TTY

To see the name of each running test, uses the format available in PHPUnit using --tap:

Tournesol.png mediawiki.demo.wmflabs.org - TTY

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

Tournesol.png mediawiki.demo.wmflabs.org - TTY

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

Tournesol.png mediawiki.demo.wmflabs.org - TTY

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

You can also run tests within a specific directory. For example, to run all of the language-related tests:

php phpunit.php languages/

Running a particular sub-group[edit]

php tests/phpunit/phpunit.php --list-groups prints the available test groups. To run a particular test group, pass it to phpunit, for example:

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