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:
- a phpunit.xml file
- a simple php test file
- 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:
<?php require_once 'PHPUnit/Framework.php'; class BaseTest extends PHPUnit_Framework_TestCase { public function testBase() { $this->assertTrue(true); $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:
- To rid of the output nuisance when running tests from the command line
- 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:
- copy system/core/Bootstrap.php as application/tests/BootstrapPHPUnit.php.
- comment out these 3 calls in that file:
Event::run('system.routing'); Benchmark::stop(SYSTEM_BENCHMARK.'_system_initialization'); Event::run('system.execute');
- 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;
with…
if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') && empty($_SERVER['SERVER_NAME'])) { require APPPATH.'tests/BootstrapPHPUnit'.EXT; } else { 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.
Pingback: Tweets that mention PHPUnit in Kohana 2.3.4 | SolutionFactor -- Topsy.com
I know this post is old but isnt your code actually for Kohana v3.x?
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?
I think the mention of Kohana3, because of the ‘classes’ folder in applications. Kohana 3 has application/classes/Controller etc.
Thanks, works perfectly. I had to comment out the //require_once “PHPUnit/Framework.php”; in the BaseTest class.
Glad it helped!
Pingback: 7 Best Practices That We Find Aren’t Practicied | SolutionFactor
Pingback: Automated Testing: Adding PHPUnit Support With NetBeans | SolutionFactor
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
(If i comment that line, it works, but i guess I cannot comment it.)
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.
Apologies, I had a space between the <?php tag.
Works perfectly, thanks
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.
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).
How to implement that in particular module? Like:
+application
+modules
+my_module
+controllers
-libraries
MyLibraryIwantToTest.php
+views