Unittest

1. Introduction

PHPUnit is a framework to build unit tests and run them automatically from the commandline. Single tests up to whole test suites (a bunch of tests, organized through a configuration file) are possible and will improve the stability and maintainability of every application. It is a fundamentally part of a continues development, to run existing tests more then once to check if changes had influence on parts of the system you did not thought of. There is a lot more you can to with PHPUnit, but that's the core function.


2. Installation

The requirement to run unit tests with the Adventure PHP Framework is the PHPUnit framework. It is freely available through several sources. The following section explains two ways to get the phpunit framework.

2. Installation via PEAR

A convenient way to get the phpunit framework is PEAR. The PEAR CLI provides an easy way to install and update your packages. Most all-in-one solutions like XAMPP provide at least the PEAR installer, if not already have pre-installed some PEAR packages. In the following sections it will be assumed a pear environment is correctly installed, you can get more information under PEAR-Installation.

The installation of the PHPUnit-Framework now proceeds easily and quickly. Therefor call the command line and enter the following commands:

Shell
pear config-set auto_discover 1 pear install pear.phpunit.de/File_Iterator pear install pear.phpunit.de/Text_Template pear install pear.phpunit.de/PHP_CodeCoverage pear install pear.phpunit.de/PHP_Timer pear install pear.phpunit.de/PHPUnit_MockObject pear install pear.phpunit.de/PHPUnit

The following packages are optional:

Shell
pear install phpunit/DbUnit pear install phpunit/PHPUnit_Selenium pear install phpunit/PHPUnit_Story pear install phpunit/PHPUnit_TestListener_DBUS pear install phpunit/PHPUnit_TestListener_XHProf pear install phpunit/PHPUnit_TicketListener_Fogbugz pear install phpunit/PHPUnit_TicketListener_GitHub pear install phpunit/PHPUnit_TicketListener_GoogleCode pear install phpunit/PHPUnit_TicketListener_Trac pear install phpunit/PHP_Invoker

2. Installation via GIT

The second possibility to get the phpunit framework is via GIT. You get more information under the official introduction. If you have a git client installed, you can use the following commands on the command line:

Shell
mkdir phpunit && cd phpunit git clone git://github.com/sebastianbergmann/phpunit.git git clone git://github.com/sebastianbergmann/dbunit.git git clone git://github.com/sebastianbergmann/php-file-iterator.git git clone git://github.com/sebastianbergmann/php-text-template.git git clone git://github.com/sebastianbergmann/php-code-coverage.git git clone git://github.com/sebastianbergmann/php-token-stream.git git clone git://github.com/sebastianbergmann/php-timer.git git clone git://github.com/sebastianbergmann/phpunit-mock-objects.git git clone git://github.com/sebastianbergmann/phpunit-selenium.git git clone git://github.com/sebastianbergmann/phpunit-story.git git clone git://github.com/sebastianbergmann/php-invoker.git

3. Configuraton

If you have the PHPUnit framework installed, there is not much to do to finally bring the unit tests on the Adventure PHP Framework to work.

First you change directory to tests. There are two files, which has to be copied and renamed:

Shell
cp phpunit.dist.xml phpunit.xml cp phpunit.dist phpunit

The file "phpunit" is the script to call on commandline to invoke the phpunit test application. Before you can use it correctly, you need to open the file and change the fist constant called PHPUNIT_INCLUDE_PATH, which has the path to the shared pear library. On Windows based systems you may change the second constant PHP_BIN_FILENAME to "php.exe".

4. Execution

Now you may call the script phpunit in the directory ../branches/php5/1.15/tests/ from the commandline with the following command:

Shell
php phpunit

The script automatically checks for the default configuration file called "phpunit.xml" which we have already existing in the current directory.
So if everything works correctly the script should call the described test suites from the configuration file.

5. Custom Unit Tests

If you want to run you own tests or extend the existing, all you have to do is adding it to the configuration file, and of course write the test.

In order to configure PHPStorm for development and execution of APF UnitTests please follow the instructions of the wiki article Setup UnitTest-Umgebung in PHPStorm (German).

Comments

Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.