“unit testing is a test that validates that individual units of source code are working properly”, that’s what Wikipedia says about unit testing. That’s general knowledge.
But what motivates me even more than the increased software quality is that it saves me development time. This sounds odd as you might believe that TDD means writing more code.
Here’s a real-life example:
For our internal invoicing system I had the task of changing the way we generate invoice numbers. Instead of a global number range each tilllate Ltd. subsidiary should receive it’s own distinct number range. And all invoices must have subsequent numbers to be compliant with the legal regulations. Invoices are being generated by about 100 staffs via a web front end. So I wrote the classes InvoiceNumber and InvoiceNumberFactory.
Testing it manually: Boring!
Now, how do I test them? Without a unit test framework I would have had to test it via the web based front end:
- Create a test customer (= fill out a form with about 30 fields, click save, wait)
- Create a new order (= fill out two forms with each 10 fields, click save, wait)
- Create an invoice (= another form)
- Check if the generated invoice number is correct
- If not, delete all created db entries and start over again
My code would not have worked immediately. I would have had to go through the procedure above over and over again. Each iteration was 120 seconds of clicking and waiting. YAWN.
Speeding it up by automating the test
By using unit tests I was able to avoid this manual labor: I set up my test environment (1 – 3) programatically in my setUp method. I cleaned up everything (step 5) in the tearDown method of my test. To run the test I just called it via the shell:
pluto unit # php -f test_InvoiceNumber.php TestInvoiceNumber OK Test cases run: 1/1, Passes: 20, Failures: 0, Exceptions: 0
The whole set up, testing and tear down process was done programmatically. Time elapsed: 0.54 seconds.
Thats 240 times faster than the manual testing via the web front end.*
And definitely much more fun!
*sure, writing the test is not free. But in the bottom line I believe it save me time.

Pingback: | Webgedanken