From Joomla! Documentation
Revision as of 15:29, 4 March 2008 by Instance
Unit testing is an essential part of a good Quality Control program. For more information, visit the Wikipedia article on unit testing.
Unit Testing in Open Source
Open source projects, with multiple developers working in parallel around the world, can greatly benefit from unit testing. The main benefits are:
- Unit tests help highlight cases where seemingly minor changes cause unexpected breakage.
- Unit tests help clearly specify how a class should behave.
- Unit tests can expose design flaws very early in development.
- Unit tests make great examples. They are a great place for developers to learn how to use the code.
Unit Testing in Joomla!
Unit testing capabilities in Joomla are still at an early stage. The intention is to define more standards for developing tests, and then to expand the scope of available tests
The SVN repository contains code under the /testing path. /testing/trunk contains code based on the SimpleTest framework. In early December 2007, the development team elected to move to the PHPUnit framework.
Work on using PHPUnit has been done in /testing/branches/2007-12-17. Some new tests have been added, many old tests from the SimpleTest days are completely broken.
At this point, PHPUnit based tests only run in a command line environment.
- There is no longer any need to patch the main code to enable unit tests.
- Basic techniques for mock objects are defined.
- Strategies for dealing with local configuration is not yet complete, but there is a plan.
- Files of the form class-sequence-type-test.php, for example JObject-0000-class-test.php use PHPUnit.
- The JDate tests present a good example of a data-driven test, but they won't run on the current 1.5 code base (there are some proposed API changes as a result of unit test development).
- Previously functional tests, such as JFTP, haven't been moved to the PHPUnit environment yet.
Running Unit Tests
Test files follow the form class-sequence-type-test.php, for example JObject-0000-class-test.php. For tests that are not class based, the first element refers to the object being tested. An example of this is the e-mail cloaking plugin test, which is called emailcloak-0000-mode1-test.php.
Joomla unit tests use a customized test "runner". Every test directory should have a "runtests.php" file. Runtests has a few options, mostly to allow the selection of specific tests. The command line options are:
e.g. --class-filter /JDate/ Selects only tests that have a class part that matches the regular expression.
Turns on additional debugging output.
Prints information on options and exits.
Selects only tests that have a sequence part that matches the regular expression.
Selects only tests that have a test part that matches the regular expression.
TODO: Expand command line parsing to add other features of the PHPUnit framework, such as output formats, code metrics reports, etc.