TDD is an iterative software development process where you first write the test with the idea that it must fail.
This is a different approach to the traditional development where you write the application functionality first and then write test cases. The major benefit of this approach is that the code becomes thoroughly unit tested.
Why use TDD?
- TDD can eliminate duplication of code and also disciplines the developer to focus his mind on delivering what is absolutely necessary. This means the system you develop only does what it is supposed to do because you first write test cases for the business requirements and then write the required functionality to satisfy the test cases and no more.
- These unit tests can be repeatedly run to alert the development team immediately if someone breaks any existing functionality. All the unit tests can be run overnight as part of the deployment process and test results can be emailed to the development team.
- TDD ensures that code becomes thoroughly unit tested. It is not possible to write thorough unit tests if you leave it to the end due to deadline pressures, lack of motivation etc.
- TDD complements design by contract methodology and gets the developer thinking in terms of pre and post conditions as well as exceptions.
- When using TDD, tests drive your code and to some extent they assist you in validating your design at an earlier stage.
- TDD also helps you refactor your code with confidence that if it breaks the business logic it gets picked up when you run your unit tests next time.
- TDD promotes design to interface not implementation design concept. For example: when your code has to take input from an external source or device which is not present at the time of writing your unit tests, you need to create an interface, which takes input from another source in order for your tests to work.
Approach
- Write a Failing Test - Understand the (user) requirements/story well enough to write a test for what you expect. (the test should fail initially - hence it being “Red”)
- Make the (failing) Test Pass - Write (only) the code you need to make the (failing) test pass, while ensuring your existing/previous tests all still pass (no regressions).
- Refactor the code you wrote - if you have time to tidy up the code you wrote to make it simpler (for your future self or colleagues to understand) before you need to ship the current feature, do it.
Related
Other Pages in the Notes Category
- December 26, 2021: On Aging
- December 18, 2021: Xbox Game Pass Space Hog
- July 06, 2021: No-nos
- July 06, 2021: Coaches' rules for life
- April 22, 2021: Barrels and Ammunitions
- April 10, 2021: Cocktail Class
- September 04, 2019: High Standards
- August 26, 2019: Business Strategy
- July 16, 2019: Organisational Culture
- June 22, 2019: Threat Modeling
- June 22, 2019: Object Oriented Programming
- June 22, 2019: Load Balancing
- June 22, 2019: Introduction
- June 22, 2019: Highly Effective Managers
- June 22, 2019: Design Patterns
- June 10, 2019: Organisational Design
- January 06, 2019: Performance Management
- January 06, 2019: Decision Making
- January 06, 2019: Conflict Management
- October 11, 2018: Psychological Safety
- October 10, 2018: Radical Candor
- October 10, 2018: Objectives and Key Results
- September 16, 2018: Fluency
- September 15, 2018: Tactical and Strategic
- September 14, 2018: Precise Estimates
- September 14, 2018: Coaching
- September 13, 2018: Turkish
- September 11, 2018: Mindmapping STAR
- September 10, 2018: Design Thinking
- September 08, 2018: Undone Work
- September 04, 2018: Product Market Fit
- September 04, 2018: Idea to Launch
- September 04, 2018: Virtues
- September 03, 2018: System Design
- September 03, 2018: SOLID
- September 03, 2018: CAP
- September 03, 2018: ACID