PHPUnit in Kohana 2.3.4

I wanted to add unit test support to a current Kohana 2.3.4 project I’m working on.  Unfortunately, most of the posts I found on how to integrate unit testing into Kohana were for version 3.x, not 2.3.x.  The few hits I did find for the 2.x branch had dead links, or weren’t entirely sufficient to get me to the point where I could run tests.  Luckily this wasn’t my first time using PHPUnit, so I went ahead and tried to see what it would take to get PHPUnit working directly w/out having to enlist Kohana’s unittest module.  Suprisingly, it was pretty straight forward.

The first thing  did was install PHPUnit (I’m working on Ubuntu, hence the use of apt-get):

sudo apt-get install phpunit

Second, I created the following 2 directories to house all the test related code:

  • application/tests
  • application/tests/classes

From here, the work begins.  You’ll need 3 more things to get to the point where you can write tests:

  1. a phpunit.xml file
  2. a simple php test file
  3. a PHPUnit specific Bootstrap file

The phpunit.xml file is the config file for PHPUnit.  Create the file in the application/tests directory w/this content, be sure to add the path to your index.php file:

<!--?xml version="1.0" encoding="UTF-8"?-->



Next, add a simple test file.  Create application/tests/classes/BaseTest.php w/the following content:


require_once 'PHPUnit/Framework.php';

class BaseTest extends PHPUnit_Framework_TestCase
    public function testBase()
        $this->assertEquals(1, 1);

That’s obviously not a worthy test for a real application, but it’ll serve as an indicator that the setup is correct when we try to run tests.

Next, cd to your application/tests directory and run phpunit.  You’ll probably get a bunch of HTML output, but at the very bottom you should see a message that reads: OK (1 test, 2 assertions). If so, then you’re ready to start adding your own tests.

Now, the 3rd item I listed above is to get rid of all the HTML output I just mentioned.  There are 2 reasons you’ll want to do this:

  1. To rid of the output nuisance when running tests from the command line
  2. To avoid problems when running unit tests from an IDE.  I was happy to find PHPUnit support in NetBeans, but while tests ran fine from the command line, running those tests from an IDE always returned the message No Tests Executed.  It wasn’t until I removed the HTML output that NetBeans was able to find my tests and parse the results.

So for item 3, we’ll need to do 3 things:

  1. copy system/core/Bootstrap.php as application/tests/BootstrapPHPUnit.php.
  2. comment out these 3 calls in that file:

  3. In your index.php file, you’ll need to route to either the core Bootstrap file, or the PHPUnit specific Bootstrap file depending on whether PHPUnit is running your app vs. a real user.  For that, I replaced:
    require SYSPATH.'core/Bootstrap'.EXT;


    if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') && empty($_SERVER['SERVER_NAME']))
        require APPPATH.'tests/BootstrapPHPUnit'.EXT;
        require SYSPATH.'core/Bootstrap'.EXT;

Once that’s done, you should be able to run phpunit again from the command line and not have to put up with the HTML output.

This entry was posted in PHP. Bookmark the permalink.

15 Responses to PHPUnit in Kohana 2.3.4

  1. Pingback: Tweets that mention PHPUnit in Kohana 2.3.4 | SolutionFactor --

  2. Federico says:

    I know this post is old but isnt your code actually for Kohana v3.x?

    • mzarate says:

      No, or at least not that I’m aware of. The apps I work on, including the one I setup unit testing for above, are all stuck on the 2.X branch of Kohana.

      Just curious, but what in particular made you ask that?

      • wwhite says:

        I think the mention of Kohana3, because of the ‘classes’ folder in applications. Kohana 3 has application/classes/Controller etc.

  3. wwhite says:

    Thanks, works perfectly. I had to comment out the //require_once “PHPUnit/Framework.php”; in the BaseTest class.

  4. Pingback: 7 Best Practices That We Find Aren’t Practicied | SolutionFactor

  5. Pingback: Automated Testing: Adding PHPUnit Support With NetBeans | SolutionFactor

  6. Sunchaser says:

    I tried to follow your tutorial but when i launch phpunit i get this error:

    Errore in C:/www/me-dogma/system/core/Kohana.php linea: 157
    Cannot modify header information – headers already sent by (output started at

    At Kohana.php, line 157 I have:

    // Send default text/html UTF-8 header
    header(‘Content-Type: text/html; charset=UTF-8’);

    What can i do to fix this? thanks

    • Sunchaser says:

      (If i comment that line, it works, but i guess I cannot comment it.)

      • mzarate says:

        Hmm, sounds like you’re getting HTML output when you run tests, which I did touch on a little bit in the post.

        Since the post is so old I should probably ask: are you sure you’re using Kohana 2.3.4, and not a newer version?

        If you’re on 2.3.4, the best thing to do is upgrade. 🙂 If you’re on a newer version, than the post is likely outdated for your version.

        BUT, for the sake of wanting to help you trouble shoot, let’s start here: can you try verifying which bootstrap file is being hit when you run tests?

        I mentioned two near the end of the post: BootstrapPHPUnit vs. the standard Bootstrap file Kohana uses by default. The tests will need to hit the BootstrapPHPUnit file.

        And you’re right, if you comment out that line, you’ll likely hit problems since it’s inside a core Kohana file.

      • Sunchaser says:

        Apologies, I had a space between the <?php tag.

        Works perfectly, thanks

        • Sunchaser says:

          I have a huge base code (a game), rewriting it for kohana 3 is out of question… 🙂

          I could take it into consideration but the documentation is very poor. If I have to migrate the game I will take into consideration another framework.

          • mzarate says:

            Heh, most of my use of Kohana was also for game development.

            And I agree, if you migrate, another framework is probably a better option. We came to the same conclusion regarding Kohana 3 (specifically wrt documentation).

  7. Yaroslav says:

    How to implement that in particular module? Like:

Leave a Reply

Your email address will not be published. Required fields are marked *