Sunday, December 18, 2005

Symfony - Testing made easy

While working on a new project using symfony, I began to setup my test environment. Symfony makes this extremely easy as it has a built in mechanism to run tests that will validate your routing among other things. The one thing that the built in symfony testing currently doesn't do , is allow for testing of form submissions.

No worries though, the symfony test suite allows you to build tests extending SimpleTest's WebTestCase which, has support for form submissions. I needed this functionality and while I could have easily used Selenium, I like being able to run all of my tests using one simple command `symfony test This was extremely useful in testing my authentication routines to make sure that a valid user could login and logout.

Key to any test is test data. Symfony also provides an great way to define test data with data fixtures (note: this can also be used to initialize a new install with necessary default data). You can then specify a batch script in the `batch/` directory and simply run it from the command line.

I wanted to make sure that I used controlled data for my tests, so I set up a separate test database and configured symfony to use this database for my tests. I didn't want to have to create a new load script to load this data for my different databases so, I simply added the ability to specify a command line argument to set what database to populate. Now the environment can be specified like so `php load_data.php test`.


$env ='dev';

if( isset($argv[1]))
$env= $argv[1];

echo "Loading data into $env data base \n";

define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/..'));
define('SF_APP', 'blogs');
define('SF_ENVIRONMENT', $env);
define('SF_DEBUG', true);


$data = new sfPropelData();


Now, thanks to symfony and simpletest, I can run my Unit and Functional tests from a single command which I think is extremely convenient.

technorati tags: ,


At 11:58 PM, Blogger sorna said...

I actually enjoyed reading through this posting.Many thanks.

Symfony Development


Post a Comment

Links to this post:

Create a Link

<< Home