simple but usefull unit tests

Post on 28-Jan-2018

241 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Simple but useful

Unit Testing Techniques

Who Am I?

• Nir Tayeb - 25 years old

• Senior Full stack engineer at Tapingo

since November, 2014

• nir@tapingo.com, nir.tayeb@gmail.com

The situation

The company new senior developer (A.K.A

me) got a task to solve a big operational

issue in the core system

• 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

No unit-testings are available

That’s means I cannot make sure I don’t f*ck the whole ordering system

So for every change I made in the flow, I’ve created a test to make sure it get the right results

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.

The main key to success in testing

is making your code more

MODULAR

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”

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

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

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!

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

Further readings

• Refactoring – Martin fowler

top related