Unit Testing 
Ferose Khan J
What is Unit Testing? 
User Acceptance 
Testing 
Integration 
Testing 
Unit Testing
Reality Check
Unit testing is HARD
Developers only write production 
code
Never attempt UT for my code
Why do unit testing? 
• Refactor with peace of mind 
• Know when coding is done 
• Understand the design 
• Living documentation of the code 
• Feel good on seeing those green dots
Steps 
• Setup 
• Create input 
• Call the method 
• Verify output 
• Teardown
Testing Scope 
• Test behavior instead of methods 
• Test one layer at a time
Do’s 
• Runs Fast 
• Fully automated 
• Can be executed in any order 
• Produce consistent result 
• Produce atomic result 
• Write informative message
Don’ts 
• Conditional coding in tests 
• Catch unexpected exceptions in tests 
• Loops inside tests 
• Testing related logic in production code
Conversation master programmer & pupil: 
Pupil: “When can I stop writing tests?” 
Master:“When you stop writing code.” 
Pupil:“When do I stop writing code?” 
Master:“When you become a manager.” 
(The pupil trembled) 
Pupil: “When do I become a manager?” 
Master: “When you stop writing tests.” 
The pupil rushed to write some tests. 
He left skid marks.
“Unit testing is part of the 
design process and not 
part of testing process”
Value of Unit Testing

Value of Unit Testing

  • 1.
  • 2.
    What is UnitTesting? User Acceptance Testing Integration Testing Unit Testing
  • 3.
  • 4.
  • 5.
    Developers only writeproduction code
  • 6.
    Never attempt UTfor my code
  • 7.
    Why do unittesting? • Refactor with peace of mind • Know when coding is done • Understand the design • Living documentation of the code • Feel good on seeing those green dots
  • 8.
    Steps • Setup • Create input • Call the method • Verify output • Teardown
  • 9.
    Testing Scope •Test behavior instead of methods • Test one layer at a time
  • 10.
    Do’s • RunsFast • Fully automated • Can be executed in any order • Produce consistent result • Produce atomic result • Write informative message
  • 11.
    Don’ts • Conditionalcoding in tests • Catch unexpected exceptions in tests • Loops inside tests • Testing related logic in production code
  • 13.
    Conversation master programmer& pupil: Pupil: “When can I stop writing tests?” Master:“When you stop writing code.” Pupil:“When do I stop writing code?” Master:“When you become a manager.” (The pupil trembled) Pupil: “When do I become a manager?” Master: “When you stop writing tests.” The pupil rushed to write some tests. He left skid marks.
  • 14.
    “Unit testing ispart of the design process and not part of testing process”

Editor's Notes

  • #11 Runs fast so that it can be integrated into the continuous integration -Fully automated no manual intervention is required. Can run unattended - The order of execution should not be assumed - If some test fails then until its fixed it should fail (no Math.random() / Datetime.Now) - The result should be either a pass or fail - Successfully failed to import the layouts
  • #12 If you are writing if then else in the test code then its better to refactor the test into separate tests - Don’t swallow unexpected exception. This mostly happens if you are writing test for the implementation and not based on the contract. - Don’t run loops inside test and cover lot of code - The production code should be free of testing related logic. (friend classes in the syngo classic in the past)