EllisLab text mark
Advanced Search
1 of 2
1
   
Improved CodeIgniter Unit Testing
Posted: 14 October 2009 09:05 AM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2009-10-09
10 posts

But all the business logic is in the controller so i don’t understant wwhy they didn’t made anything to test them. Maybe un future version of CI! thanks.

 Signature 


How to kill a programmer in the shower? Make him read a shampoo bottle:
  -Wash hair
  -Rince
  -Repeat
...

 
Posted: 26 October 2009 05:35 PM   [ # 12 ]   [ Rating: 0 ]
Joined: 2009-10-26
1 posts

Hey all,

I’m trying to set up something similar to Jamie Rumbelow’s implementation of SimpleTest, as written here:

http://jamierumbelow.net/2009/08/11/setting-up-the-perfect-codeigniter-tdd-environment/

However, I can’t seem to get an example working which allows me to test existing models appropriately.  Let’s say I have an existing model created under application/models/user_model.php and then I create a test under tests/models/user_model_test.php…  When I attempt to run the test for this my model, I’m receiving this error:

Fatal error: Class ‘Model’ not found in /var/www/vc/system/application/models/user_model.php

So, obviously, in the test scenario, the code executing in user_model cannot resolve the Model base class.  From this article, I’m gathering that the CodeIgniterUnitTestCase base class attempts to handle this issue, and assuming that simply deriving my test class from CodeIgniterUnitTestCase should be all I need to do to take care of this.  Unfortunately, it doesn’t seem to be working for me.

Has anyone had any success with this?  The bottom line is that I don’t know how to make the unit tests aware of the resources that are normally autoloaded when running a CI app.  If someone could perhaps be able to provide a stub of a test that would work with a model, it would be greatly appreciated.

Thanks in advance!

 
Posted: 26 October 2009 08:45 PM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-24
614 posts

You’ve done a great job here Istvan, I’m impressed! It’s a great solution for simple unit testing and it’s great to see you’re encouraging TDD in the community. How far are you going to develop this? Will it turn into it’s own dedicated testing library or are you trying to keep it as close to CI’s as possible?

The reason why I use SimpleTest is because I get a great range of not only Unit Testing, but also a Web Tester for views. If there could exist a more streamlined solution that ties in with CI’s ethics and code ethos then I’d certainly consider using it as my primary testing solution.

Jamie

 Signature 

THE CODEIGNITER HANDBOOK - A new CI book for everybody!
—-
Freelance Web Developer - @jamierumbelow - http://jamieonsoftware.com

 
Posted: 28 October 2009 01:34 AM   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-04
12 posts

@Jamie
So far I am trying to keep it as close as to CI as possible. But I can’t say no to try to extend the capabilities. It will depend on whether or not the project I am working on would need that kind of extensive testing. I actually made a few tweaks already which I am posting below.

The next thing I might try to do is move from a single controller handling all testing to one that just contains the logic and fetches the test cases in another folder.

 
Posted: 28 October 2009 02:14 AM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-04
12 posts

[DEPRECATED] Click here for latest version
Improved CodeIgniter Unit Testing 1.0.1

Change Log
Version 1.0.1
——————-
- Now overloading the CI Unit Test class
- Removed support for testing controllers due to CI limitation
- SQL errors are now displayed in the test report if a test case fails

For new users
- Download the zip attached to this post.
- Extract the zip on top of your CI install directory if you did not change the path to your application or system folder (replace files as needed). Otherwise, extract to a temporary directory and move files as needed.

That’s it! Now you can open the test.php controller and start adding your own tests. A sample function has been provided to show you how to create your own. After you have written some tests, you can test them by accessing siteurl.com/test/.

Update procedure from 1.0 to 1.0.1
- Download the zip attached to this post.
- Extract the files in a temporary directory
- Copy the file MY_Test_unit.php from application/libraries into your application/libraries folder
- Open your original view unit_test.php and delete the line:

<input type="button" value="Controllers" onclick="window.1ocation='<?=site_url("test/controllers")?>'" /> 

Also check that the CSS location is correctly referenced.
- Open your original test controller test.php and delete the following

case 'controllers'
else if (strripos($method'controller')  === $length 10)
{
    $this
->tests['controllers'][] $method;

- Overwrite your /system/language/english/unit_test_lang.php with the one you extracted

 
Posted: 17 December 2009 09:53 AM   [ # 16 ]   [ Rating: 0 ]
Avatar
Joined: 2009-11-22
11 posts

Hi all,

thanx for the updates. it works really fine for me.  Go go testing time !

 
Posted: 16 January 2010 07:26 AM   [ # 17 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-04
12 posts

Improved CodeIgniter Unit Testing 1.0.2 (Latest version)

Change Log
Version 1.0.2
——————-
Release date: January 16th, 2010

- Added missing array_search_recusrive() function to controller (was accidentally removed in version 1.0.1, thanks for pointing this out khal3d)
- Modified test mapping function and view to be able to easily change the name of the controller
- Modified the view to use site_url()
- Modified the view to show test groups in selection box

For new users
1. Download the zip attached to this post.
2. Extract the zip on top of your CI install directory if you did not change the path to your application or system folder (replace files as needed). Otherwise, extract to a temporary directory and move files as needed.
3. Optionally also follow the second step (2) in the update procedure given below

That’s it! Now you can open the test.php controller and start adding your own tests. A sample function has been provided to show you how to create your own. After you have written some tests, you can test them by accessing siteurl.com/test/.

Update procedure from 1.0.1 to 1.0.2
1. Replace application/views/unit_test.php with new version included in the zip.
2. Cut and paste the function array_search_recursive contained in application/controller/test.php into a new helper (see CI user guide on how to make helpers) and make sure to load it either in the constructor or add it to $autoload[‘helper’] array (in application/config/autoload.php). You could optionally add it to an existing CI helper such as array_helper.php but don’t forget to load it. (I personally added it to array_helper and I am auto loading it.)

 
Posted: 09 December 2010 05:35 AM   [ # 18 ]   [ Rating: 0 ]
Joined: 2010-06-03
23 posts

trying your things out.
but it simply does not work for me. i have an error on line 194. undefined index Result ...
can you help me?

thanks

here my function listed in the test controller

public function user_model(){
            $this
->benchmark->mark('start');
            
$test true;
            
$this->unit->run($testTRUE'my first test with codeigniter');
            
$this->benchmark->mark('end');
            
$this->timings[] $this->benchmark->elapsed_time('start''end');
        
 
Posted: 09 December 2010 06:14 AM   [ # 19 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-04
12 posts

helle,

It seems you might not be using the English language file I provided. If

$lang['ut_result''Result'

is not ‘Result’ that’s the error you would get. It is odd you do not get errors for the other strings though.
The file in question is in system\language\english\unit_test_lang.php

 
Posted: 09 December 2010 06:17 AM   [ # 20 ]   [ Rating: 0 ]
Joined: 2010-06-03
23 posts

ah okay. i see. had now language file in my german lang folder which provides your language stuff.
copied the english one into the german folder => works fine!

 
Posted: 09 December 2010 06:21 AM   [ # 21 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-04
12 posts

Glad it worked. It’s good you reported this, I should be using local language strings instead of hard coding them directly. This should be fixed in the next version and compatibility with CI 2.x as well.

 
Posted: 08 March 2011 07:06 AM   [ # 22 ]   [ Rating: 0 ]
Joined: 2011-03-04
27 posts

As per my view there is no need to test the controller functions for unit testing as it is integration of many functions of models , helpers etc.

So perform unit testing on those function only . If the test passes the test for controller will pass automatically.

We can perform integration testing for controller functions .may be!!!

 
Posted: 14 June 2012 02:44 AM   [ # 23 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-04
12 posts

This project has a new home at https://github.com/istvanp/CodeIgniter-UnitTest.
However it is no longer maintained by me. Send your pull requests though.

 
Posted: 08 August 2013 10:07 AM   [ # 24 ]   [ Rating: 0 ]
Joined: 2013-08-08
1 posts
jegbagus - 16 September 2009 01:22 AM

hi istvanp, thanks for the link..
i fast reading the documentation, and i find that this tools is really interesting.
but can this tools implemented in Codeigniter?
as you know CI controller / model / plugin etc, never use Calls to include, it will generate the right profiling result?
regards

i would like to reccomed to visit this link - something more relevant to your problem: http://myunittesting.com/

 

 
1 of 2
1