Saturday, May 31, 2014

Unit Testing and why should I bother to understand what it is about?

When I started my software engineering course my lovely lecturer kept on mentioning “Unit testing” several times.. she gave us the definition, examples and why it is important to use unit testing in the software development process. What I did at that stage is memorizing everything she said and my journey with unit testing stopped !!. I didn't actually know why should I apply unit testing to my code.
Isn't it enough to have a running code without  bugs (No red lines, no exceptions… everything is working.. hooray).
After two years I started my masters in software engineering and of course again I came across the term unit testing.
My reaction was !! again !! what is wrong with these people ….. I am happy with want I know.. can’t you understand that my code is working fine !!
One day later the lecturer decided to divide us into teams of six students and we were asked to produce a software product in four weeks time. We divided the tasks between the team members and each two of us worked on a specific task. Me and my friend finished coding and our program was doing everything we asked for… so we went for lunch and life was amazing.
Next day the lecturer came to check our progress.. we were sure that we rock !!
The code is working.. no bugs … no exception. Insert your input and the output will be exactly what you asked for.
He looked at us and said what happens if I …… ??
He kept asking these type of questions….. I thought he wanted  to drive us crazy ??
What if ??
How could your program deal with this case??
And my reaction was.. wait a minute ?? My program met all the requirements you asked for ….. why should I care about these cases !!!
My program is designed to work with humans !! how could it suddenly start dealing with cases coming from aliens.
At that stage I guess I understood that my code should be strong enough to face life circumstances. It should not die in the first battle !!
But how could I make my program strong ??
Simple answer is …. feed it with all expected test cases. Make it strong enough to face life !!
Testing testing..
I have to think about testing my program against bla bla bla ….
I have to think about testing my program against my cat who could simply decide to use my program. …. Cats also expect to see some output…
But.. wait a minute .. this is testing .. what is Unit testing ???
Imagine that your goal is to build a skyscraper from LEGO pieces. The skyscraper is your software product and LEGO pieces are your code.
You finished building your skyscraper and It  looks amazing.
                                     
Suddenly you discovered a crack in one of your LEGO pieces !!! You will panic because its not an easy task to replace it !! Now your skyscraper will not be strong enough to stand and It may fall at any moment !!
How could I have overcome this ?? What went wrong ??
The answer is : If you simply tested each piece of LEGO before using it .. you would have noticed the crack and fixed it…
Testing testing testing !!
This is exactly the same in coding.. You test each single piece of code .. and your whole program will be strong enough to face life difficulties.

The End !!!

No comments:

Post a Comment