Developement-Driven Tests

TDD is one of the best practices that symfony promotes.

The main reason that many PHP developers don’t invest earlier into testing solutions is that TDD deals with practice, not knowledge of a particular language or framework.

Learning a test framework and how to write unit and functional tests is quite easy. Build an automated testing solutions is pretty easy too. It just takes some time.

The most difficult part is to learn how to set up good habits in your daily job, which includes :

  • Writing relevant tests
  • Writing testable code
  • Running tests automatically

It’s pretty easy to miss one of these steps during development and fall into the evil counterpart of Test-Driven Development methodology :  Development-Driven Tests, whose workflow can be compared to the old-fashioned and non-agile Waterfall model.

Here are some practices to avoid if you don’t want to fall into DDT :

  • Writing tests after the code is finished

If you write the tests after the code, you are likely to treat testing as a constraining process. Your are pretty sure that your class is working, so why should you write test it now right ?

At that point, you will probably want to test only working functionalities, make sure that every test you write passes and that nothing fails at any moment. After all, your code is finished so you don’t want to push it too further and hurt it. That’s Development-Driven Test !

The purpose of TDD is exactly the opposite : Put the code under extreme conditions (edge cases and invalid data) and make sure that failures are handled correctly. Only then should you assert that the class is working under normal conditions.

Tests must be written before any functional 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");
  • Updating tests after fixing a bug

When a bug is reported, the first thing to do is to reproduce it in the test environment. Once the bug is clearly identified and reproduced in isolation, the test suite must fail explicitly :

$t->diag('Testing class::method() with new invalid data');
$input_data = "%data";
$t->is(class::method($input_data), true, "'%' should be handled");

Now, you can modify the code in order to make the test suite pass again.

Testing in the real world

To bring TDD in your daily development is definitely not an easy task. Fortunately, the symfony platform provides a complete toolkit to help you dive into testing :

  • environments
  • bundled test framework (lime)
  • tasks and test harnesses for automated test suites

  1. This is useful post. Personally, I don’t think it is possible to engage in TDD when one is first learning the Symfony framework (or any new technology). It takes a certain confidence to write tests that fail.

    But once one knows the framework, yes, tests are the best way to both prevent errors and document the software for future programmers who will have to learn the system.

  2. nicolas.martin

    Off course, testing comes when you own the technical background of the technology your are using.
    I’m using the symfony framework for one year and a half and I can say that, like any new language or framework, that the learning curve is pretty flat.
    But when it comes to practices or methodology and especially TDD, which I am currently experiencing, the adoption curve is more steepy, even with the best testing tools in hands.

  3. I’m a symfony user for several months and I really enjoyed using the unit and functional tests internal frameworks, that help me to save a lot of time when I’m developping. That’s a really good feature for professional who want to add more quality to their projects.

  1. 1 Developement-Driven Tests |

    […] original post here: Developement-Driven Tests Related ArticlesBookmarksTags Les tests unitaires … Read the original: Les tests […]

  2. 2 Closer To The Ideal » Blog Archive » 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.” […]

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: