Unit testing for CodeIgniter: PHPUnit and CIUnit

I have been looking into building a Continuous Integration Environment on my Mac for my Code Igniter Projects. To achieve this I need to put together some robust tests, I have selected PHPUnit to use as my test framework as I have used this before. I know that Code Igniter has a built in test Lib http://codeigniter.com/user_guide/libraries/unit_testing.html but this is not robust enough and it will not integrate with my Continuous Integration environment, which in this case is Jenkins.

To right tests for your code, you need to be able to load in your controllers and models and in turn for them to load in the rest of the Code Igniter core. It is I’m affried not as easy as just inclueding the classes you want and then instantiating them, you need to have the framework run like a page request. To do this is a bit of a mission, but, being a good little developer rather then reinvention the wheel I fired up Google and started haveing a search around.

I found that someone had started a project called CIUnit, but it seems that the project has gone cold and was not complete. Some more searching turned up a fork of this called My CIUnit. It seemed to be completed – but slow on the updats. I have used it for a while and it seems to do the job just right.

It was easy to install you just download the latest version from: https://bitbucket.org/kenjis/my-ciunit. Then unzip and copy the test folder to the root of you project.

In the test folder there is a folder for each class type; models, controllers, libs ect… There are examples tests in each folde. There is a PHPUnit xml file which you can use to intagrate the tests in to your chosen Continuous Integration enviroment.

That’s it easy to install and simple to use, just how I like it.

About This Post

Leave a Reply