![]() |
Some help for Unit Testing, part I - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Some help for Unit Testing, part I (/showthread.php?tid=7406) |
Some help for Unit Testing, part I - El Forum - 04-08-2008 [eluser]t'mo[/eluser] I've been on an agile software development team for nearly three years where we try to never write a line of production code without first having unit tests. This holds true for our main work (Java), but I've seen the practice of TDD usefully extended to other realms (even Perl!). With this in mind, and even though I'm finding using CI at home on personal projects very satisfying, I'm not happy with CI's unit testing capabilities. I don't know how to remedy the entire situation, but I offer this as at least a little bit of help; use it if you like, tell me where it could be improved, or show me a better way. My pain started with the repetition in using CI's unit test class. I repeated "$this->load->library('unit_test')" in every test class, always created an "index()" method that would invoke each test, always called "$this->unit->report()", etc. That irritation festered until I came up with this: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); So, using that controller, I create a subclass that looks like so: Code: <?php The key points in the concrete test class are: 1. the functions that have tests you want run are prefixed with the word "test" and are automatically run by the super-class (MY_TestController) 2. some of the extra test stuff (remembering to call "report()", etc.) is abstracted out where it doesn't get in your way of seeing what it is you're trying to test "But you're not calling 'report()'!" Yes, I forgot I also did away with that. I didn't like the default report format -- too verbose. Instead I came up with this view ('view/test/results.php'), which seems simpler, and has the benefit of a handy "red/green bar" available from other, more traditional unit testing tools: Code: <html> So, what does that leave me in the end? A relatively quick, painless way to see my test results, in a format that's not too obtrusive. I just hit each test controller's URL, and with no more than a glance up at the monitor ("Anything red?"), I'm done. Code: http://localhost/app_root/test/themodeltest After all, the whole concept of unit testing is useful only if it's able to give us quick feedback about the quality of our code. :-) |