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
- Arrange all necessary preconditions and inputs.
- Act on the object or method under test.
- Assert that the expected results have occurred.
Pattern to follow for Naming of unittests:
- Given - inputs
- When - under certain conditions
- 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."
- Pushes developer to write a clean and robust code.
- Developers have a clear target in mind and that's very important in agile environment.
- 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.
- 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.
- TDD reduces the chances of accidentally packaging tests with production binaries.
- There are many agile engineering practices that a team could adopts and many of those agile engineering practices are dependent on having unit tests.
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.
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!
Glad you liked it!
https://www.youtube.com/watch?v=EZ05e7EMOLM