Test Driven Development (TDD) is great and it really helps you build better, higher quality systems. Unit Tests FTW! However, when TDD meets the real world, there is a clash and many times developers just don’t write unit tests. This is bad, but it can be better if the right steps are taken. Writing code that is testable is the key; whether you write the unit tests now or later. Testability FTW!
Testability is when you write code using the same best practices (short methods, use interfaces, clean code, etc.) that lend to being able to write unit tests to get good code coverage. While it can take quite a bit of practice to be able to design class / interface structures and method signatures that are more easily unit testable, it is a key to building more maintainable systems in the long run. Refactoring code to allow for testability is hard; it can be really hard. As long as your code is unit testable in the first place you can always write unit tests at any time in the future.
Once you’ve written nice, testable code, you can write unit tests with good code coverage at any time. Many developers sacrifice those unit tests to meet client / user deadlines and eventually get to production. Instead of creating a huge pile of technical debt that needs to be refactored in addition to have unit tests written, just make it testable in the first place. Technical debt is bad, and writing non-testable systems will kill the code and make it a bad kind of legacy before you even ship to production. Always strive to write code that is unit testable, so you can always go back and add unit tests at any time.
Now that you have unit testable code written and likely deployed to production, you will still have bugs reported from either UAT testing or PROD users. When a bug comes in, you’ll be able to easily write a unit test to expose the bug since you wrote the code to be unit testable in the first place. Then you can fix the bug and verify that the bug never occurs again. You can even take a few minutes to add more unit tests while you’re in there to increase the code coverage percentage to help with future testing, maintenance and bug fixes.
Testable code is the most important part of Test Driven Development (TDD). Unit Test help verify bugs are fixed and future changes don’t break existing functionality, but in order for this all to work properly the code must first be written in a manner that lends itself to being testable.
Go write testable code. Always!
- 10 Oct 2020
- 01 Jul 2020
- 06 Apr 2020
- 11 Mar 2020
- 15 Feb 2020
Recent on Build5Nines.com
- 04 Dec 2020
Latest Cloud News: Apple on K8s, IoT, Microsoft Pluton and more! (November 20, 2020 Build5Nines Weekly)20 Nov 2020
Latest Cloud News: .NET 5 Released, Apple Silicon M1 CPU, and more! (November 12, 2020 Build5Nines Weekly)12 Nov 2020
- 11 Nov 2020
Latest Cloud News: Kubernetes, Terraform, Teams Multi-Login and more! (November 5, 2020 Build5Nines Weekly)05 Nov 2020
This site is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. We also participates in affiliate programs with Udemy, and other sites. This site is compensated for referring traffic and business to these companies.