Starting with unit testing and looking for pointers

I’d like to hear some advice on how to best start with learning test driven development and unit testing. I have no practical experience with this, only some theory here and there. Now I’m starting a new php project which has a potential for being developed and expanded long-term so I think this is a good opportunity to start using TDD in practice.

What steps would you suggest to take to start learning TDD? Any ideas, tutorials, books? I have found The Grumpy Programmer’s PHPUnit Cookbook which sounds promising, do you think it’s worth getting it?

To be honest, I don’t want to spend a huge amount of time learning now before I get to work, because I have work to get done, so I want to take it step by step spending a small amount of time each day as I realise mastering TDD can take quite a long time. That’s why I’m looking for any helpers so that I don’t have to make all the mistakes I could avoid by reading someone’s good advice.

Currently, I’m developing with the use of a framework of my own that I have got used to over time and I’m very comfortable with so I’d like to carry on using it - it has its simple ORM with auto-generated classes from database schemata and it generates all initial controllers, actions and forms for editing data in the admin area. I realise some parts of the code are not well suited for unit testing but I can always make changes to the code if really necessary. I hope I can focus now on making most of my new code testable, I’m not striving now on making all of my framework testable since I’d probably have to spend at least a month or more on learning and modifications while the project would show no real progress.

HI Lemon Juice,

A good book that get’s it going pretty quick is ‘Guide to PHP Design Patterns’ - ISBN# 0-9735898-2-5. Although the language examples are getting outdated this book captures the essence of TDD.

Here is some general advice:

  1. Use a testing framework that has good documentation and support for Mock Objects and Partial Mocks
  2. If you embrace TDD use it for the entire code-base not just pieces and don’t abandon it during times of schedule stress.
  3. Learn how to use Test Suites. This enables you to group testable parts of your application. You can for instance run all code that covers the entire app. This is especially useful at later dates when you have to refactor the code and you don’t remember all the dependencies involved. You can feel confident in making changes to the code as your steps using TDD for the refactored code will either pass - and not break any other part of the application or will fail and you’ll know precisely what part(s) of the application are affected and allow you to make better refactoring choices.
  4. Don’t put too much into your starting ‘failed’ tests.
  5. Initially write the most basic code possible to ‘pass’ the test - this may even be very ugly code
  6. Refactor the code into something better and repeat the cycle

I’m sure that if you looking to embrace TDD you’ve already researched it. I’ve been using TDD for 6 years now and I can say with certainty that I write better more flexible code using TDD than not.


I agree with much of what ServerStorm posted above.

It’s particularly important to refactor your code if it’s difficult to test at any point. By refactoring difficult code into code that is easier to test, I’ve found that my finished code ends up much more flexible, neater and easier to understand. I’ve also found that by testing early, it means I become a client to my own code early, which means I get a real feel for how it will be used in practice, and as a result of this my apis tend to come out much more focused in the end. You also end up with a suite of tests that give you much greater confidence that your code is working and new changes haven’t broken anything. It’s also great for other developers on your team, because the unit tests serve as a form of documentation for them too.

As serverstorm said above, it’s important to treat your tests as integral to the project - your tests shouldn’t be an after thought. I’ve found that my code quality has increased as a result of treating my unit tests as as important as my production code. Good luck!

Thanks, I’ll have a look at it.

I am thinking about using PHPUnit because it seems to be most popular and I thought it would be good to learn something that is a kind of ‘industry standard’. There’s also PHPUnit integration with Netbeans, which I hope can help me. I haven’t yet gone as far as comparing Mock Objects and Partial Mocks support in various testing frameworks - will PHPUnit be good enough in this respect?

Thanks ServerStorm and aaarrrggh for your other input, much appreciated!