Welcome Guest, Not a member yet? Register   Sign In
Some help for Unit Testing, part II
#1

[eluser]t'mo[/eluser]
(continued from Part I)

Last time I shared how I made a Controller subclass that had some helpful methods for simplifying use of CI's Unit Testing class. The primary benefit was that you could point your browser to your test class and see your test results nicely formatted in the browser:

Code:
http://localhost/path/to/app/test/something

One downside, though, is that you have to remember all the "somethings" you've written tests for and point your browser at each of them. What I really wanted was a way to see *all* the results of *all* my test classes together.

Enter the command line. Following the lead from an entry in the wiki (cron scripts), I saw it would be possible, with a little modification, to create a version of "index.php" that would invoke an arbitrary path into my application. I created a copy of index.php called "cmdline.php" and added one line to set the path into my app:

Code:
...
$_SERVER['PATH_INFO'] = $argv[1]; // <-- My new addition

// next line is where CI gets its start
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;
?&gt;

For example, I run my "Product" model's test class from the command line like this:

Code:
$ php cmdline.php /test/products

From here it was just a short step to producing a shell script that would execute all my tests:

Code:
#!/bin/sh

for f in `ls controllers/test`; do
  g=/test/`echo $f | sed 's/.php//'`
  php cmdline.php $g
done

While running my test suite on the command line, I don't want to see HTML output - it's too hard to parse visually. I hinted at this in part I, but didn't explain. You may recall this snippet from MY_TestController::index:

Code:
if ($_SERVER['SCRIPT_NAME'] == 'cmdline.php') { // YES! this is the line; pay attention, grasshopper
      $this->load->view('test/simpleResults', $data);
    }
    else ...

So, I just load a different view, depending on where I'm running the test from. The simplified view says either "OK" or "Failed" for each test (plus a handy 'print_r' on the failed response):

Code:
=========================================================
Unit test results for &lt;?=$modelname."\n"?&gt;
=========================================================
&lt;?php
foreach ($results as $result) {
  if ($result['Result'] == 'Passed') {
    print "OK: " . $result['Test Name'] . "\n";
  }
  else {
    print "Failed: ";
    print_r($result);
  }
}
?&gt;

Quick, easy, mostly painless, eh? Well, unless you're on Windows...then I don't know what you'll do for a "shell". ;-)

---

I originally planned on stopping the "unit testing help" series here, but questions in another thread about "how to test the front end", plus some neat stuff I saw at work the other day in Ruby, have prompted me to look at how I might do automated acceptance testing with CI...so there will be more to come.


Messages In This Thread
Some help for Unit Testing, part II - by El Forum - 04-14-2008, 12:23 AM
Some help for Unit Testing, part II - by El Forum - 04-14-2008, 06:53 AM
Some help for Unit Testing, part II - by El Forum - 04-14-2008, 10:28 AM
Some help for Unit Testing, part II - by El Forum - 04-15-2008, 12:08 AM
Some help for Unit Testing, part II - by El Forum - 04-15-2008, 08:10 PM
Some help for Unit Testing, part II - by El Forum - 04-20-2008, 12:33 PM
Some help for Unit Testing, part II - by El Forum - 01-17-2009, 10:42 PM
Some help for Unit Testing, part II - by El Forum - 01-17-2009, 10:49 PM
Some help for Unit Testing, part II - by El Forum - 01-18-2009, 04:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB