[CakePHP] Unit Testing

What a fun !! 😀
Today i test one of my Country Model using cakephp component. That why i write down this notes to make sure that i can use several times.

1. Download Simple Test unit testing suite.
2. Uncompress it in my app/vendors directory.
3. Change DEBUG level of 1 in my app/config/core.php file
4. Create a new database connection in app/config/database.php for only testing purpose like;

var $test = array(
‘driver’ => ‘mysql’,
‘persistent’ => true,
‘host’ => ‘localhost’,
‘login’ => ‘root’,
‘password’ => ”,
‘database’ => ‘db_name’,
‘prefix’ => ”,
‘encoding’ => ‘utf8’

5. Fixture : Create a file named country_test_fixture.php in your app/tests/fixtures directory, with the following content

class CountryTestFixture extends CakeTestFixture {

var $name = ‘CountryTest’;

var $import = array(‘model’ => ‘Country’, ‘records’ => true , ‘connection’ => ‘test’);


6. Test Case : Create a file named country.test.php in your app/tests/cases/models directory, with the following contents:



class CountryTest extends Country {
var $name = ‘Country’;
//var $useDbConfig = ‘test_suite’;

class CountryTestCase extends CakeTestCase {

var $fixtures = array( ‘Country_test’ );

function testFindAll() {

$this->CountryTest =& new CountryTest();

$result = $this->CountryTest->findAll(“Country.country_id=1”);

$expected = array(

array (
‘Country’ => array( ‘country_id’ => 1, ‘region_id’ => 2 , ‘country’ => ‘Bangladesh’ ),



$this->assertEqual($result, $expected);



7. Running My Test Case: Point my browser to http://tanveer-noman/myProject/test.php. Click on App Test Cases and find the link to your models/country.test.php. Click on that link.
Waoow!! I got a nice green screen saying that your test succeded.

To know more

cake bake


About Tanveer
RIA (Rich Internet Application) Developer with a wide variety of business applications. Particularly interested in client/server and relational database design. Always interested in migration projects, as well as close interaction with the DB manufacturers.

4 Responses to [CakePHP] Unit Testing

  1. Pingback: links for 2008-07-11 « Richard@Home

  2. Excellent site, keep up the good work

  3. Fabiano says:

    hi! If I need to execute all test in one time how can i do that? ( I think using shell, have you done that ?)

  4. Tanveer says:

    Hi Fabiano,
    Thanks for visiting my weblog.
    If you want to execute all the test in one time then you can create a custom class to do that. And yes, it’s all so possible to make that happen using shell. But I didn’t try that yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: