Test Drive Development (TDD)
What is TDD :
Write Failing test First and later test should pass as the development progress
Properties of TDD
- Red-Green Refractor ie., Write Failing test First and later test should pass as the development progress
- Canary Test = Blank test to validate if the environment is working
- Description / Console output / Naming should read like a book
- Pending tests ie., Only declared but not implemented
- Error message from the Unit test should drive the development
- 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)
- The method under test initially should return below as the development progress :
- "NULL"
- Change "NULL" > Constant
- 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:
- Pass the tests
- Reveal Intention
- No Duplication
- Fewest number of elements
- Always Unit test should come first than we start the actual code.
- List all the test initially with only declaration ie.,(only describe and it()) these will be considered as pending tests or todo list.
- 1st test in canary test (No test , pass by default)
Reveal Intention.
- Meaningful variable and methods names which reveal intention in tests /Actual Code.
- Comments are unnecessary if you have meaningful names.
- Code should not be duplicated
- should be < 1
- 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 :
- Write a Unit test which sends null / blank as input to the function under test.
- Modify the Actual code to handle null / blank
- Run the test it should pass.
Cyele 2:
- Write a test / Add definition to empty test declaration which sends a simple constant as input to Actual Code
- Modify the Actual code to handle that constant either by returning it directly / other means and make sure both the test passes
- Run the test it should pass both the tests.
Cyele 3:
- Add definition to next empty test declaration to send little bit complex input.
- Modify the Actual code to handle that constant either by using if statements
- Run the test it should pass all 3 tests.
Cyele 4:
- Add definition to next empty test declaration to send even more complex input to actual code
- Modify the Actual code to handle that constant either by using better logic and so on.
- .....
No comments:
Post a Comment