Categories
Programming

A Brief Rant about TDD

success ln / failure dr
Credit: Flickr / StockMonkeys.com

I’ve been giving this talk about unit testing UI code lately and of course when you talk about testing, TDD (test driven development) keeps coming up.

The question ranges from “have you embraced TDD as the One True Way Of Testing, and if not why not because you’re doing it wrong” and “I have heard that TDD is the One True Way Of Testing, but I don’t unit test at all and I’m overwhelmed”.

Note that neither of these are actually questions, but I digress.

When it comes to my own workflow, I don’t do strict TDD (gasp!)

  • I don’t debug; I write tests.
  • When I have something that is inputs in -> outputs out I quite often write my tests first. In practise very little application code is like this.
  • Testing is largely an architectural problem, and I architect my code such that it is testable, usually writing my tests concurrently.
    • I don’t consider my code to be “done” until it is tested.
    • A lot of my testing involves testing UI code, e.g. I test that the right thing happens when a button is tapped. I guess technically I could stub out the view element and write tests for it, in practise I tend to complete my train of thought and write the view and then test it.

Here’s my answer to the second none-question: I don’t believe in telling people to TDD. I believe in testing your code. That’s it. I’m happy to share my workflow, and techniques I use (buy my workshop!) but people should do what works for them.

I do think though that TDD is something that you work up to (some point of), not what you start with. Because:

  • When you start writing tests you’ll probably find more bugs in your test code than your actual code. This kind of undermines the idea of the tests as the spec.
  • It’s super intimidating. You’re almost certainly going to discover that however you have been writing your code it’s not in a way that is testable. You’re going to need to refine your tests and your code together.
  • The idea that something has to be done “The Right Way” rather than “At All” is a huge impediment to starting. If you want to write more tests don’t wait until you have a new class that you can do TDD on, set aside an hour, choose something simple and get going.

TL;DR: I don’t really TDD. I don’t care if you TDD. I do think everyone should test their code, though.