Codementor Events

Test Code before Development (TDD).

Published Aug 04, 2018Last updated Jan 31, 2019
Test Code before Development (TDD).

TDD - Test Driven Development is a Development-Testing methodology that helps developers to achieve Speed, Robustness and Quality with its specifically structured mechanisms. It is a software development approach where a dev write unittests before the application code.

Joe Birch, a Senior Android Engineer at Buffer:
"Following TDD has not only allowed us to write simpler, more understandable code but has allowed us to think more about what we are implementing before actually implementing it. This extra space to think, allows us to understand and test for the different logical paths before getting caught up in implementation details. This, in turn, helps to reduce development time by creating something less error-prone from the start."

If we unittest but don’t refactor, then we will find (some) bugs earlier. If we unit test and refactor then we will prevent bugs—fewer mistakes will be made, so less to detect. So let’s measure those two outcomes as our primary measures.

Pattern for Arranging and formatting code in UnitTest methods:

Follow - 3 A's

  1. Arrange all necessary preconditions and inputs.
  2. Act on the object or method under test.
  3. Assert that the expected results have occurred.

Pattern to follow for Naming of unittests:

  1. Given - inputs
  2. When - under certain conditions
  3. Then - output generated

Below are some examples:
a. test_func_raises_zero_division_error_for_divisor_as_zero
b. test_is_adult_false_age_less_than_18
c. test_fail_to_admit_form_if_mandatory_fields_missing

5 major benefits of Test Driven Development:

"A tested code is always given preference to go out on Production. And through writting unit-tests a developer can reduce it's development effort to re-work if in case there is a new feature enhancement."

  1. Pushes developer to write a clean and robust code.
  2. Developers have a clear target in mind and that's very important in agile environment.
  3. Future enhancements would be much easier, as developer would know what to change & where to change, and yes test new code at the same time.
  4. Code functionality can be easily understood as let's say a developer's job is filled in by another dev then the new dev can easily understand the code functionality by going through the unit tests.
  5. TDD reduces the chances of accidentally packaging tests with production binaries.
  6. There are many agile engineering practices that a team could adopts and many of those agile engineering practices are dependent on having unit tests.

References:

  1. Metrics: Are We Doing TDD
  2. Clean Code by Robert C Martin
  3. Sample unittest Cases using pytests
  4. Why Use Test-Driven Development?
  5. Principles for Test-Driven Development
Discover and read more posts from Abhishake Gupta
get started
post commentsBe the first to share your opinion
Saban Durna
6 years ago

I think the most important method of TDD is Red-Green-Refactor. Because our brains always busy with a lot of hard problems and we usually delay some important things like refactoring. TDD strict us to refactor codes on every task. As a result, thanks to TDD we are writing more robust code.

Anupam Maurya
6 years ago

Yes, it is good practice to write pseudo code, before writing actual code. It gives us more logic area to think. And makes implementation more simple and output as testable stuff!

Abhishake Gupta
6 years ago

Glad you liked it!

EllisGL
6 years ago
Show more replies