Test driven development with Symfony

Nicolas Martin has some useful advice:

Tests must be written before any code. That forces you to “program to an interface, not an implementation.”

Here’s a useful hint to quickly step into test writing :  Uses the lime::todo() method to write the test-suite roadmap in plain english, and write tests step by step afterward :

$t = new lime_test(3, new lime_output_color());

$t->todo("Test if the class::method() with invalid data throws an exception");
$t->todo("Test the class::method2() with missing parameter uses default values");
$t->todo("Test the class::method3() returns an valid array with valid parameter");

I agree with what he is saying, but I also think one has to get comfortable with Symfony before one can engage in TDD. It takes a certain confidence to write tests that fail. It makes no sense to write failing tests when you are new to the framework and nothing seems to work correctly. This last summer, when I was learning Symfony, my questions were very basic: “How do I get this text into the template?” No point writing a test when you are struggling with such basic stuff.

But yes, no doubt, once you know the framework, one should lead with tests.

I also think writing any tests helps make a system more stable, and more sustainable – more ready for the next programmer to come in and understand what is going on. We took over one project (not a Symfony project) and instituted a “tests for all bugs” rule. That is the absolute minimum of testing, only writing tests for bugs, but it has the advantage of making sure that the bugs, once fixed, stay fixed. That, by itself, is a huge step forward for most fast evolving websites.

Leave a Reply