In waiting future version 3.4.0
of PHPUnit that will allow to define our own listeners
into the 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
For examples:
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
(see pear config-show
for detail depending of your installation).
For example: run command below
php path/to/pear/tests/XML_Util/tests/AllTests.php
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. PEAR_TestListener::addError |
PEAR_LOG_ERR |
Logs a failure occurred. PEAR_TestListener::addFailure |
PEAR_LOG_CRIT |
Logs an incomplete test. PEAR_TestListener::addIncompleteTest |
PEAR_LOG_WARNING |
Logs a skipped test. PEAR_TestListener::addSkippedTest |
PEAR_LOG_WARNING |
Logs a test suite started. PEAR_TestListener::startTestSuite |
PEAR_LOG_INFO |
Logs a test suite ended. PEAR_TestListener::endTestSuite |
PEAR_LOG_INFO |
Logs a test started. PEAR_TestListener::startTest |
PEAR_LOG_INFO |
Logs a test ended. PEAR_TestListener::endTest |
PEAR_LOG_INFO |
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 --filter
switch
php path/to/your/UnitTest.php --filter myCurrentTestCaseAdded
A specific group of test cases with --group
switch
php path/to/your/UnitTest.php --group currentWork
All test cases except some groups --exclude-group
switch
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/TestListener.php
and PEAR/TestRunner.php
into 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 PHPUnit_TextUI_TestRunner::run(self::suite());
in 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