Sunday, July 12, 2020

Test Drive Development (TDD)


Test Drive Development  (TDD)


 

What is TDD :
Write Failing test First and later test should pass as the development progress


Properties of TDD
  1. Red-Green Refractor ie., Write Failing test First and later test should pass as the development progress
  2. Canary Test = Blank test to validate if the environment is working
  3. Description / Console output / Naming should read like a book
  4. Pending tests ie., Only declared but not implemented
  5. Error message from the Unit test should drive the development
  6. Source Code should be separate from the Unit Test (As it does not need to go to production. It can be in the same repo not the same file)
  7. The method under test initially should return below as the development progress : 
    1.   "NULL"
    2.   Change "NULL"  >  Constant
    3.   Constant > "Complex Expression depending on the requirement"
Eg :
assert (mtsToKm(mtrs) == NULL)
def dev (mtrs) : return NULL

assert (mtsToKm(1000) == 1)
def dev (mtrs) : return 1

assert (mtsToKm(1000) == 1)
def dev (mtrs) : mtrs/1000 

Rules of Simple Design:
  1. Pass the tests
  2. Reveal Intention
  3. No Duplication  
  4. Fewest number of elements
Pass the tests :
  1. Always Unit test should come first than we start the actual code.
  2. List all the test initially with only declaration ie.,(only describe and it()) these will be considered as pending tests or todo list.
  3. 1st test in canary test (No test , pass by default)
Reveal Intention.
  1. Meaningful variable and methods names which reveal intention in tests /Actual Code.
  2. Comments are unnecessary if you have meaningful names.
No Duplication
  1. Code should not be duplicated 
  2.  should be < 1  
  3. use functions /methods for repetitions. 
Transformation premises

Pre-Req : 
  • Make Sure all the tests are declared initially
  • And only Canary Test has test definition.

Cyele 1 :
  1. Write a Unit test which sends null / blank as input to the function under test.
  2. Modify the Actual code to handle null / blank
  3. Run the test it should pass.
Cyele 2:
  1. Write a test / Add definition to empty test declaration which sends a simple constant as input to Actual Code
  2. Modify the Actual code to handle that constant either by returning it directly / other means and make sure both the test passes 
  3. Run the test it should pass both the tests.
Cyele 3:
  1. Add definition to next empty test declaration to send little bit complex input.
  2. Modify the Actual code to handle that constant either by using if statements
  3. Run the test it should pass all 3 tests.
Cyele 4:
  1. Add definition to next empty test declaration to send even more  complex input to actual code 
  2. Modify the Actual code to handle that constant either by using better logic and so on.
  3. .....

No comments:

Post a Comment