simple but usefull unit tests

17
Simple but useful Unit Testing Techniques

Upload: nir-tayeb

Post on 28-Jan-2018

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simple but usefull unit tests

Simple but useful

Unit Testing Techniques

Page 2: Simple but usefull unit tests

Who Am I?

• Nir Tayeb - 25 years old

• Senior Full stack engineer at Tapingo

since November, 2014

[email protected], [email protected]

Page 3: Simple but usefull unit tests

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

• 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

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

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

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

The main key to success in testing

is making your code more

MODULAR

Page 9: Simple but usefull unit tests

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
Page 11: Simple but usefull unit tests

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
Page 13: Simple but usefull unit tests

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
Page 15: Simple but usefull unit tests

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

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

Further readings

• Refactoring – Martin fowler