SMW Unit Test Development
What Links Here
Tests are commonly divided into a manual (without using any tool or automated script) and an automated scripted test approach.
If you want to run some manual tests (either as scripted or exploratory test procedure) then you just have to:
- Download a related branch using
composer require "mediawiki/semantic-media-wiki:dev-foo(where
foorefers to the branch name) or in case you want to test the current master, use
dev-masteras version together with the
minimum-stability: devflag so that the branch/master can be fetched without any stability limitations.
composer dump-autoloadto ensure that all registered classes are correctly initialized before starting any test procedure.
Automated testing (PHPUnit)
For the automated approach, Semantic MediaWiki relies on PHPUnit as scripted testing methodology. Scripted tests are used to verify that an expected behaviour occurs for codified requirements on the given conditions.
- Unit test refers to a script that verifies results for a unit, module, or class against an expected outcome in an isolated environment
- Integration test (or functional test) normally combines multiple components into a single process and verifies the results in a semi-production like environment (including DB access, sample data etc.)
- System test (and its individual modules) is treated as "black-box" to observe behaviour as a whole rather than its units
- Verify that PHUnit is installed and in case it is not, use
composer require phpunit/phpunit:~4.8 --update-with-dependenciesto add the package
- Verify that your MediaWiki installation comes with its test files and folders (e.g.
/myMediawikiFolder/tests) in order for Semantic MediaWiki to have access to registered MW-core classes. If the
testsfolder is missing then you may follow the release source to download the missing files.
composer phpunitfrom the Semantic MediaWiki base directory (e.g.
/extensions/SemanticMediaWiki) using a standard command line tool which should output something like:
$ composer phpunit Semantic MediaWiki: 2.5.0-alpha (SMWSQLStore3, sqlite) MediaWiki: 1.28.0-alpha (Extension vendor autoloader) Site language: en Execution time: 2015-01-01 01:00 Xdebug: Disabled (or not installed) PHPUnit 4.8.27 by Sebastian Bergmann and contributors. Runtime: PHP 5.6.8 Configuration: /home/travis/build/SemanticMediaWiki/mw/extensions/SemanticMediaWiki/phpunit.xml.dist ............................................................. 61 / 4069 ( 1%) ............................................................. 122 / 4069 ( 2%)
Writing meaningful tests isn't difficult but requires some diligence on how to setup a test and its environment. One simple rule is to avoid the use of hidden expectations or inheritance as remedy for the "less code is good code" aesthetics. Allow the code to be readable and if possible follow the arrange, act, assert pattern.
For a short introduction on "How to write a test for Semantic MediaWiki", have a look at this video.
The use of
MediaWikiTestCase is discouraged (as its binds tests and the test
environment to MediaWiki) and it is best to rely on
and where a MW database connection is required, use the
QueryPrinterTestCasebase class for all query and result printers
Integration tests are vital to confirm the behaviour of a component from an integrative perspective that occurs through an interplay with its surroundings.
Those tests don't replace unit tests, they complement them to verify that an expected outcome does actually occur in combination with MediaWiki and other services.
Integration tests can help reduce the recurrence of regressions or bugs, given that a developers follows a simple process:
- Make a conjecture or hypothesis about the cause of the bug or regression
- Find a minimal test case (using wiki text at this point should make it much easier to replicate a deviated behaviour)
- Write a
JSONtest and have it fail
- Apply a fix
- Run the test again and then run all other integration tests to ensure nothing else was altered by accidentally introducing another regression not directly related to the one that has been fixed
SMW\Tests\Integration\ hosts most of the tests that target the validation of
reciprocity with MediaWiki and/or other services such as:
- Triple-stores (necessary for the
- Extensions (
Some details about the integration test environment can be found here.
JSONScript integration tests
One best practice approach in Semantic MediaWiki is to write integration tests as
JSONScript to allow non-developers to review and understand the setup and
requirements of its test scenarios.
JSON format was introduced as abstraction layer to lower the barrier of
understanding of what is being tested by using the wikitext markup to help design
test cases quicker without the need to learn how
PHPUnit or internal
A detailed description of the
JSONScript together with a list of available test
files can be found here.
For details, please have a look at the benchmark guide document.
- Writing testable code
- Code coverage in a nutshell
- Test Doubles (mocks, stubs etc.) and how to write them