Login / Status
developer.Resource
Home . Documentation . Document Library . Extension Manuals
Sponsors
hosted by punkt.deTYPO3 and Open Source MagazineAOE Media

Chapter 1. PHPUnit

Extension Key: phpunit

Copyright 2007 Kasper Ligaard <ligaard@daimi.au.dk>

This document is published under the Open Content License

available from http://www.opencontent.org/opl.shtml

The content of this document is related to TYPO3

- a GNU/GPL CMS/Framework available from www.typo3.com

1.1. Introduction

What does it do?

PHPUnit is a TYPO3 extension which provides

  1.  the PHPUnit unit-testing framework by Sebastian Bergmann

  2.  a back-end module to run unit tests for extensions of a TYPO3 installation.

Screen shot

The screen shot below shows a test run of the (sample) tests that comes with the phpunit extensions. There are two failing tests, with descriptions of each failure:

Preliminary instructions

These are basic requirements for using this extension:

  1. Make sure you are running PHP 5.2.1 or higher.

  2. Use the Extension Manager to download and install phpunit.

  3. In order to create a unit test, you need to create a directory “tests” in your extension directory and add some PHP files ending with _testcase.php containing a class with functions whose names are prefixed with test_.

  4. If you need to store data for fixtures, create a sub directory tests/fixtures/

  5. Have a look in the phpunit extensions tests/ directory for a template, tx_phpunit_test_testcase.php.

  6. Consult the PHPUnit documentation for information on writing tests, available functions etc.

Using the phpunit extension with the pear extension

As of version 3.1.13, phpunit checks if the pear extension is loaded and if it is, then it can use a PHPUnit installed through the pear manager module. You should use version PEAR extension version 3.2.5 or later.

If you can not use this method nor the built-in PHPUnit, then read next section.

Using another version of PHPUnit than the version provided with the extension

As of version 3.1.9 of the phpunit extension it is possible use any PHPUnit version 3.x with phpunit. If you already have PHPUnit installed through PEAR, then you can use that version instead; or you could run ‘bleeding edge’ SVN directly from the phpunit.de Subversion repository.

Using another PHPUnit version is easy if you remember these three points:

  1. Use the absolute path.

  2. Remember the trailing slash in the absolute path.

  3. The path should point to the directory before ‘PHPUnit’ directory. Example: If you have the Framework.php-file installed in c:\xampp\php\PEAR\PHPUnit, then you should point to the directory c:\xampp\php\PEAR\

You can verify the includepath from within the phpunit extension via the ‘About phpunit BE’ entry in the upper right corner of the back-end module.

When you are all set, then you just write the path in the extension conf in the extension manager for phpunit.

Writing tests

The phpunit extension scans all installed extension directories for a ‘tests’ directory. So put your tests in a sub-directory of your extensions root called ‘tests’ and phpunit will automatically detect them.

Now you are ready to write tests. Writing tests is fun but takes some time. I hope using this extension will motivate you to keep on writing good, solid tests :-)

If you are new to unit-testing, the read the PHPUnit pocket guide, http://www.phpunit.de/pocket_guide/3.2/en/ .