In waiting future version 3.4.0
of PHPUnit that will allow to define our own listeners
phpunit.xml XML configuration file, we have no way until now,
to run a test suite and log results as we want.
Only standard logging solutions provided with PHPUnit 3.3.x are :
PHPUnit 3.3.16 by Sebastian Bergmann. Usage: phpunit [switches] UnitTest [UnitTest.php] phpunit [switches] <directory> --log-graphviz <file> Log test execution in GraphViz markup. --log-json <file> Log test execution in JSON format. --log-tap <file> Log test execution in TAP format to file. --log-xml <file> Log test execution in XML format to file. --test-db-dsn <dsn> DSN for the test database. --test-db-log-rev <rev> Revision information for database logging. --test-db-prefix ... Prefix that should be stripped from filenames. --test-db-log-info ... Additional information for database logging.
Of course there is a solution, not simple, that will change the contents of
main function available in almost all PEAR packages tests suites.
Lets have a look on basic
main function implementation
with PEAR::XML_Util package.
Code below shows a cut-down version of original XML_Util/tests/AllTests.php (1.2.1)
PHPUnit_TextUI_TestRunner::run accept two parameters, where second ($arguments) is optional.
You can then give values like a list of listeners attached to the test suite to be notifed
and lot more. But you have to add lot of code to do it.
As we will see in next chapter, PEAR_TestListener, allow of course to do this kind of behavior, but add much more.
PEAR_TestListener provides :
With the PHP CLI interpreter run command below
php path/to/UnitTest.php [switches]
where [UnitTest.php] and [switches] are analog to the PHPUnit command
phpunit [switches] UnitTest [UnitTest.php]
See PHPUnit doc about Command-Line Test Runner
php path/to/pear/tests/XML_Util/tests/AllTests.php --help
php path/to/pear/tests/PHP_CompatInfo/tests/AllTests.php --group parseFile
php path/to/pear/tests/HTML_CSS/tests/AllTests.php --configuration path/to/config/file/phpunit.xml
With help of PEAR::Log package you can choose where and what you want to record.
Default behavior will record full test suite results into a text file
you can find in your PEAR test_dir
pear config-show for detail depending of your installation).
For example: run command below
If my PEAR test_dir is equal to C:\wamp\bin\php\php5.2.9-2\tests, then I can find a text file named PEAR_TestRunner_20090606.log into this directory, where 20090606 is execution date in YYYYMMDD format.
PEAR_TestListener uses the features of PEAR::Log (log levels and mask) that allows you to include and/or exclude specific levels of events from being logged.
In a standard test suite, there are 8 events (considered 9 with summary) that will occurred. Here are the corresponding Log levels for each event.
|Kind of event||Log level|
|Logs an error occurred.
|Logs a failure occurred.
|Logs an incomplete test.
|Logs a skipped test.
|Logs a test suite started.
|Logs a test suite ended.
|Logs a test started.
|Logs a test ended.
|Summary of test suite results||PEAR_LOG_NOTICE|
See PHPUnit doc for Composing a Test Suite using XML Configuration
php path/to/your/UnitTest.php --configuration path/to/your/config/phpunit.xml
A specific test case with
php path/to/your/UnitTest.php --filter myCurrentTestCaseAdded
A specific group of test cases with
php path/to/your/UnitTest.php --group currentWork
All test cases except some groups
php path/to/your/UnitTest.php --exclude-group bugs
Using PEAR installer, it's very easy. Just have to run this command :
pear install http://pear.laurent-laville.org/get/PEAR_TestListener-0.3.0b2.tgz
Install by hand :
PEAR/TestRunner.phpinto a directory that is listed in the include_path of your php.ini
If you have test suites organized as PEAR::XML_Util (see in Introduction chapter). In most case it's true with PEAR packages that have PHPUnit 3 compatible test suites.
You have only to change the highlighted yellow line
main function, by these two lines. No more no less !
include_once 'PEAR/TestRunner.php'; PEAR_TestRunner::run(self::suite());
You don't believe it ? No problem test to see the magic dream become a reality :)
I would like to thanks here 3 guys that encouraged me (by their comments)
to improve PEAR_TestListener from what it was (0.2.0) to current result (0.3.0).
In alphabetic order:
Special Thanks to Sebastian Bergmann, author of PHPUnit, without who, this proposal wouldn't have any sense.
PEAR_TestRunner::getLogger() signature changed to allow more flexibility
See Example B3. (modified) Add alternative solution by using XML configuration file with custom test listener
First beta is PHPCodeSniffer ready
Log file name can contain %Y, %M, %D for date and %I for ident.
See Example B2. (modified) Add alternative solution with XML configuration file
Add support of SQLite handler options in phpunit.xml configuration file