Why to Unit Test your Software
Senior Software Engineer - 22 February 2017 - 12min
Senior Software Engineer - 22 February 2017 - 12min
Today I will discuss about the topic many developers don’t want to talk about and think like “Why do I have to do it if we have a dedicated team for that? We should focus on some other thing rather than wasting time on it”. If you guessed “Testing”! Then you are right.
We will be discussing about “Testing” more specifically “Unit testing”.
I don’t think I need to discuss about the importance of Testing but Unit testing. We all agree that development is an important part of software life cycle but it’s not the only important part, Testing is equally important a part of successful software life cycle.
According to a research, around 50% of cost and time of software life cycle is spent on finding and fixing bug.
According to Cambridge University research in 2012, the global cost of debugging the software has risen to $312 billion annually.
What is Unit Testing?
Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation.
We will see how “Unit testing” will help us in building awesome software.
I will try to clear a few of the misconceptions about “Unit testing”
1. Why to “Unit Test”.
Unit test is our 1st line of defense against Bugs/Defects. Unit test is the fastest way to test your program logic as it took lesser time to execute unit test in comparison to some manual tests or integrated test executed by your QA team.
I think Unit tests are even more important than static analysis because it tests your program’s actual logic which has higher probability of presence of defects/bugs and their occurrence than the one found by static analysis (most IDE while programming warned us about potential error by doing static analysis). It doesn’t mean we shouldn’t do static analysis, it has its own purpose.
2. How unit testing is different from what your QA team will do.
We do Unit test to prevent defect/bug from occurring. It is better to prevent rather than finding and then fixing.Unit test helps in finding defect at earlier stage which have very little impact to fix and easy to fix which will be cost/time effective to be overall software lifecycle.
3. Unit Testing Is About Finding Bugs.
No Unit test is not only about finding bugs. Because of this misconception many of developer think they will find lots and lots of bugs but when they don’t they give up on Unit test. Unit test is all about writing bug free code so that we can implement new feature on existing one with confidence and better understanding of underlying code.
4. Write unit test without changing source code.
Our way of implementing and thinking about unit test is totally wrong. We first program our code then write test to check expected output for specific input and the we try to change our test in such a way that it fits some way somehow and increase our test coverage. But it’s totally wrong, its fine to change source code if that make our product more architectural better not change source code just for the sake of unit test.
5. Unit test is for utility methods only.
Many people think Unit test is only for utility methods, like addition method which take two input and return output as addition of that two numbers.Unit test is more than that we can use unit test for different scenarios like validating the correct sequence of methods get called or no of time particular method called etc.
6. How can I write Good Unit Test?
There is no one way to write good unit test. You heard it correct.
There is no way to learn the magic of writing good unit test.What we can learn is how to write good testable code, that’s it. If you learnt how to write a good testable code then any unit test you write will be good unit test.
7. Unit test is very time consuming, Client won’t approve it.
I don’t think any client doesn’t want a product which is bug free, have easy maintenance, robust for future enhancement and great user experience. Yes it’s right you will achieve these entire things if you do unit test from starting of your product development, it will bring down the total product life cycle time by reducing the time spent on manual testing and fixing those bug.
This “Testing Pyramid” better illustrates the importance of Unit test in testing life cycle.
We can see how time and cost effective is Unit testing.
Let’s see few advantages of Unit testing
o Deeper and better understanding of requirement
* Without understanding requirement one cannot write unit test for it
o Find problems early
* As we write Unit test parallelly to our code we are able to find potential defect in early stage of development itself
o Simplifies integration
* As each unit tested separately we are in better possible to do integration with confidence
o Can be used as Documentation
* Proper written unit test work as documentation of feature for developer, one can understand what we want to achieve by seeing unit test of that feature
o Help to come up with better Design
* If some unit/feature is not testable it means there is some flaw in our design which should better be corrected to make testable in turn more robust and reliable product
o Increase reliability and robustness
* As we have tested each unit independently in isolation we have better understanding and confidence of its behavior under different situation hence we are in better position to develop enhancement on existing product without any issue.
o Customer satisfaction
* As unit test prevent defects/bugs which improve end customer user experience
o Reduce maintenance cost
* Unit test will help to find the cause of any bug easily hence can be resolved in less time and cost.
We will see how to do Unit testing, what is Test doubles, how to use Test doubles in unit test, what are different framework to help us in unit test, how to use them etc. in my upcoming blogs.