Nigel Basel shared the lessons he has learned doing Test Driven Development (TDD) for the last 8 years.
The stats show that TDD is still a minority sport with the average results polling in at 35% or so. Nigel mentioned that for a solid, quality base you needed TDD, refactoring and Continuous Integration (CI) to co-exist and none of these independently will result in a high quality codebase. For Nigel the best results come about from the creation and running of acceptance tests and unit tests in a tight cycle.
The usual way of developing by writing the code first and then hitting debugger is error prone and TDD offers an escape from this tedious cycle by encouraging developers to instead make it fail, make it work and then make it better through the cycle of getting failing tests to pass and then refactoring the code.
A good test should capture the business functionality not whether a certain algorithm works.
TDD is an aid to helping you get there, its not a replacement for classic design skills such as SOLID or DRY. It’s also no substitute for thinking!
With TDD the design of the system is captured in runnable form.
Some people do not use TDD because they think it will take too much time. Empirical evidence suggests TDD actually takes a similar amount of time. Writing tests first focus you on the task at hand. It also helps you recover from distractions much quicker as a failing test will be an instant reminder of what you have left to do. Much more time is spent fixing defects found post development. In Nigel’s own experience of product development TDD has saved him a huge amount of time.
Defect density is lower using TDD but TDD is really more about design than testing. You end up with low coupled and highly cohesive software.
When estimating effort for functionality ensure you cover the effort of writing your tests together with the code.
Nigel mentions Mike Feather’s assertion that code without tests is legacy code. The best approach, Nigel suggests is to use Mike Feather’s concept of Test Coverings when tackling legacy code that does not have unit tests. The goal is to detect changes to the current system in order to make it safe to refactor and extend. Most Test Coverings would be integration tests rather than unit tests.
Tests need continual buy-in from the development team or the tests ‘grow mould’ as the system changes but the tests lag behind.
For User Interface (UI) tests timing is a big factor. While coded UI tests are possible they are often brittle.
There are upfront costs to TDD but the benefits far outweigh the costs. Nigel suggests those new to TDD start small as TDD is about one test at a time. A great way of introducing TDD is to try it on a greenfield project or even a tool that you are building that will interact with the system. Find the testing framework that is right for you. A refactoring tool will also make life a lot easier as it takes a lot of the pain out of the refactoring process. The right toolset will really speed things up. And if you really want to speed things up Nigel recommends coming along to the next code retreat!
Often developers will shy away from TDD because the extra work will only reap benefits in the future. But Nigel thinks the benefits are worth it. And the benefits aren’t all technical, it will also improve the developers quality of life with days being full of instant gratification. The steps of writing a test, seeing it fail and then making it work instill confidence in the developer for the quality of code, however Nigel points out they need to be the right tests! Another thing to bear in mind that there is nothing too small to test, TDDing your code will always be worth it.
Refactoring isn’t just for the code itself, its also for the tests. Tests are also a great form of documentation especially when it comes to providing documentation for APIs.TDD has simple rules but applying them in the real world is not easy. Focus on the test first, not the code. For Nigel a good unit test is readable, quick to run, cohesive, documents usage and its results are deterministic.
I’m not sure if TDD is like teenage sex but everyone should be doing it, TDD that is.