Welcome Guest, Not a member yet? Register   Sign In
Unit Testing with phpUnit (models+controllers+fixtures) - want it?
#1

[eluser]rafsoaken[/eluser]
Hi all,
Despite CI's obvious greatness some may find it lacks easy Unit Testing (no sight of them in 1.6, besides that puny Test class). I am currently cooking up a small wrapper lib to use phpUnit 3.2, yaml (db)fixtures with Spyc, and yet to come some sort of test scaffolding.
At the current stage a test case looks like this:
Code:
include_once dirname(__FILE__).'/../CIUnit.php';
set_controller('main'); //one of my app controllers

class testMainController extends CIUnit_TestCase{

    function setUp(){
      // load fixtures into test database
      // eg.: application/tests/fixtures/thomas_posts_fixt.yml
      // with the convention that there is a db table 'thomas_posts'
      $this->dbfixt('thomas_posts', 'thomas_media', 'thomas_posts_media');
    }
    
    //let's test the first page
    public function testDisplay(){
      //$this->CI always refers to your current controller
      $this->CI->index();

      //fetch the controllers output
      $outputted = output();
      //and peak at the variables assigned the view displayed in $this->CI->index();
      $assigned_vars = viewvars();

      //test view for php errors
      $this->assertFalse(preg_match('/(error|notice)/i', $outputted);
      
      //correctly assigned values?
      //compare to fixtures
      $this->assertEquals($this->thomas_posts_fixt['first'], $assigned_vars['posts'][0]);
    }

    //lets test a model too!
    function testSomeModel(){
      $this->CI->load->model('Post_model');
      //retrieve a post
      $post = $this->CI->Post_model->post(1);

      //and check its text field
      $this->assertEquals('desc of first entry', $post['text']);
      $post = $this->CI->Post_model->post(-22);
      $this->assertFalse($post);

      //or compare it directly to the fixture
      $this->assertEquals($this->thomas_posts_fixt['first'], $post);
    }
}

So, anyone interested in this? I really would like to know as the code is good enough for internal use but would need some lovin' to make it available to others...
Besides, are there any syntax requests you find useful?, or other needs that should be catered (like eg. it should play nice with your simpletest testcases, css selectors, etc.)
Cheers rafsoaken
#2

[eluser]John Fuller[/eluser]
Sure, why not polish it up and release it?

There is also a SimpleTest library which Ciscoheat released which works pretty well. I don't know how SimpleTest compares with phpUnit though.
#3

[eluser]Unknown[/eluser]
rafsoaken,

I just found your post because I'm interested enough in this sort of ability to start working on it myself. I really like that you're adding classic functional tests (testing the controllers and output); I'm currently most interested in the unit testing aspect and am trying to figure out how to wrap the models for testing...

My thought was to create a custom include so I can still use the phpunit command from the command line; what's your approach?

Adam
#4

[eluser]Unknown[/eluser]
Can you gave me this test framework code~~
#5

[eluser]rafsoaken[/eluser]
Here we are, the first release CIUnit, unit testing with phpUnit for Codeigniter. We are at v0.10, hooray!
Download it here: fooStack v0.10 for Codeigniter

It's called "fooStack", consisting of a drop-in replacment of the Codeigniter.php file (actually it only defines the CIUnit version constant and per default extends the codeigniter Loader and Output class - use 'extends fooLoader' and 'fooOutput' to extend them), and all the classes needed (eg for parsing yaml and generating fixtures)

Tests of CIUnit itself are of course included and should assure you that your setup of fooStack works just fine.

I included a short description on how to set it up in the fooStack.html file, please drop me a line or post here if you start using it.

cheers
rafsoaken
#6

[eluser]Jamongkad[/eluser]
Oh awesome up until recently I thought I was the only one who thought CI's unit testing suite was lacking. You just spared me the effort of porting SimpleTest to CI. Thanks!
#7

[eluser]dsloan[/eluser]
Certainly looks like a great project. However, I'm having a problem getting up and running with testing my application.

See my post on the problem at http://ellislab.com/forums/viewthread/87635/
#8

[eluser]rafsoaken[/eluser]
The new version v0.12 of CIUnit is available, hooray!

Some minor small bugs have been fixed, and CIUnit now is very stable for CodeIgniter versions 1.62, 1.63 and the upcoming 1.70 release (tested against trunk).
Get this beauty here: fooStack/CIUnit v0.12 for Codeigniter

Additionally the project is now also available from a public mercurial repository: fooStack/CIUnit bitbucket repository

Happy testing!
Clemens
#9

[eluser]Thorsten[/eluser]
nevermind Smile
#10

[eluser]rafsoaken[/eluser]
The repository (mercurial) is now to be found at: http://www.bitbucket.org/rafsoaken/ciunit/overview/

Check it out !




Theme © iAndrew 2016 - Forum software by © MyBB