A colleague recently asked my advice about how to start incorporating Test-Driven Development (TDD) into his project. I don’t consider myself very advanced in this topic, but long ago I recognized the tremendous value of this development approach and the benefits it provides for writing good and clean software, therefore I started practicing it and forming my own learning path. Rather than jumping right into a demo, I decided to provide a set of initial pointers that will allow any developer to better understand and apply this development paradigm, even before writing a line of code.
What to learn before writing unit tests?
Here are some of the topics along with useful resources that will allow developers better understand and apply the TDD approach:
- Understand how to write testable code
- What are SOLID principles? How to write clean code? How to use interfaces?
- Recommended book: Adaptive Code via C#: Agile coding with design patterns and SOLID principles (Developer Reference)
- Learn the art of refactoring
- Recommended: Martin Fowler’s Refactoring.com
- Understand the TDD philosophy
- What is Red->Green->Refactor workflow?
- Try and choose a unit test framework that works for you and learn the syntax
- Define a naming convention for your tests
- Setting a naming convention is very important, since the number of test cases will grow exponentially as you write more code, and you want a quick way to identify what scenarios are being tested and what is breaking, just by glancing over the list of tests.
- For test libraries, I like to use:
- For test methods, I like to use:
- Practice, practice, practice
- Exercism.com – I found this amazing site offering several simple exercises that must be solved using a TDD approach and can be implemented in multiple languages. Great resource not only for learning TDD, but also for keeping programming and logical skills sharp and learning a new language once in a while. It is also great to be able to view how other programmers implement the same solution, as well as providing and receiving feedback. I took the personal challenge of solving a couple of exercises every week.
Once you are very familiar with the above topics, you could try expanding into more adavanced scenarios:
- Learn how to use mocks and fakes
- Integrate unit tests into the Continuous Integration build
- Get test coverage reports
I hope this post can be helpful to those developers wanting to incorporate TDD into their development approach for the first time. It only touches the surface, but hopefully it can serve as a starting point for future craftsmen, willing to put the extra effort in order to write clean, efficient and maintainable code.
Please let me know if you’d like to expand on any topic or add new ones to this list.
- Adaptive Code via C#: Agile coding with design patterns and SOLID principles (Developer Reference)
- Martin Fowler’s Refactoring.com