simple but usefull unit tests
TRANSCRIPT
![Page 1: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/1.jpg)
Simple but useful
Unit Testing Techniques
![Page 2: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/2.jpg)
Who Am I?
• Nir Tayeb - 25 years old
• Senior Full stack engineer at Tapingo
since November, 2014
![Page 3: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/3.jpg)
The situation
The company new senior developer (A.K.A
me) got a task to solve a big operational
issue in the core system
![Page 4: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/4.jpg)
• The issue happened in one of our most valuable venues
• The issue happened on specifics conditions
• Failing in fixing it right may ruin the whole ordering system and affect all the rest of the venues
• The last senior developer who tried to solve it failed in Production
![Page 5: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/5.jpg)
No unit-testings are available
That’s means I cannot make sure I don’t f*ck the whole ordering system
![Page 6: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/6.jpg)
So for every change I made in the flow, I’ve created a test to make sure it get the right results
![Page 7: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/7.jpg)
Divide and Conquer
• The system is big and complex
• The function[s] I’ve wanted to tests are depend on many external which are present in cache/RDBMS/Mongo
• The function[s] I’ve wanted to tests are called from different processes on different servers.
![Page 8: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/8.jpg)
The main key to success in testing
is making your code more
MODULAR
![Page 9: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/9.jpg)
Divide and Conquer
• Make the code more modular:
– Create function for bringing data
– Create function for verifying conditions
• In the test – patch those functions to get
specific “events”
![Page 10: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/10.jpg)
![Page 11: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/11.jpg)
Time Travel
• Datetime cannot be patched
• Instead of using datetime.now/utcnowdirect – make a function get_now
• In the test – patch this function return value to the specific time you want
![Page 12: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/12.jpg)
![Page 13: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/13.jpg)
Testing in messaging
environment
• Separate the processing code to separate
function from the function which get the
message
• In the test – call to each processing
function one after one
![Page 14: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/14.jpg)
![Page 15: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/15.jpg)
Every time a bug is encountered by the
QA (good) or in Production (bad)
make a test which check the bug – so it
won’t happen never again!
![Page 16: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/16.jpg)
Results
• In 2 weeks we solved the issue and it got to production successfully
• About 99% of the code is already tested (in the area I worked on)
• The code could easily refactored
![Page 17: Simple but usefull unit tests](https://reader031.vdocuments.mx/reader031/viewer/2022022415/5a6d79677f8b9ab8418b6f67/html5/thumbnails/17.jpg)
Further readings
• Refactoring – Martin fowler