Setting up Kohana 3.2 unittest module

From 3.0 version Kohana has an official unittest module. There’s a great manual from Lysender how to configure it. But some things from this manual didn’t work for me so here’s the way how I’ve configured unittest module.


First, you need PHPUnit 3.5 installed. It’s important. Not 3.6 (or even 3.7). If you’ve already PHPUnit 3.6 installed here’s a manual how to downgrade (for MacOSX, but for other systems it works in similar way).


1. Enable the unittest module

You can do it in application/bootstrap.php file.

    'unittest'   => MODPATH.'unittest',   // Unit testing

2. Directory tree

Create necessary files. Directory should look like


3. sampletest.php

Create sample test as follows

<?php defined('SYSPATH') or die('No direct access allowed!');
class SampleTest extends Kohana_UnitTest_TestCase
    public function test_add()
        $this->assertEquals(2, 1+1);

4. phpunit.xml

The common phpunit.xml config looks as follows

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="../../index.php">
        <testsuite name="Kohana Tests">

Testsuite name “Kohana Tests” doesn’t work for me. I’ve got the error

PHPUnit_Framework_Exception [ 0 ]: Neither "Kohana Tests.php" nor "Kohana Tests.php" could be opened. ~ /usr/share/pear/PHPUnit/Util/Skeleton/Test.php [ 102 ]

From this error I got that testsuite name must be like Kohana Testcas class, i.e. “Kohana_Unittest_TestCase”.

If you don’t want to configure you index.php to supress headers use custom bootstrap instead of it. In fact you can use bootsrtap file offered by unittest module.

And if you don’t change file name to path to your tests, Kohana will run all the tests no matter what. So the final working configuration:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="../../modules/unittest/bootstrap.php">
        <testsuite name="Kohana_Unittest_TestCase">

5. Suppressing headers

If you’re using index.php as bootstrap file you must somehow suppress headers in order to not output html. So you should edit last lines in index.php as follows:

if (Kohana::$environment !== Kohana::TESTING)
    echo Request::factory()

6. Run tests

Change to application/tests in CLI and run phpunit command. You output should look like

application/tests [11.07.2012 18:22:47] phpunit
PHPUnit 3.5.15 by Sebastian Bergmann.


Time: 0 seconds, Memory: 5,75Mb

OK (1 test, 1 assertion)

P. S. You can get next error

ErrorException [ 8 ]: Undefined index: SERVER_NAME ~ SYSPATH/classes/kohana/url.php [ 79 ]

This happens because of using $_SERVER['SERVER_NAME'] in some functions which not working in CLI. So be sure not using functions like url::base() or Request::url() while testing.

Posted By admin

    One Response to “Setting up Kohana 3.2 unittest module”

  1. seyfer says:

    Thank’s. That was usefull

Leave a Reply

You must be logged in to post a comment.