How Important Unit Testing Is?
As we all know that, software testing is done at the end of the software development cycle and is relegated to a less creative testing department, hence making software testing a low priority in the development process. This approach results in error detection at the very end and sometimes even by the customer. Any bugs found during this testing phase must be fixed and retested before the final release; potentially causing delays and release date slips.
So, what’s the big deal?
- Finding bugs early and fixing them is always critical to good development
- When you’ve written new code:- How do you know it works?
- When you have to make a change:- Will it cause new problems or Will old bugs rise from the grave?
So, in order to resolve & tackle such kind of issues, it is required to replace our traditional approach with a new practice known as “Test-driven development (TDD)”. It will not 100% resolve such type of problems but, it will reduce the problems to minimum possible. Using the concepts of TDD, we can produce higher quality and well-tested code.
Let’s talk about Test Driven Development
Test driven development (sometimes called Test First Development) is a style of coding characterized by writing tests first, then writing just enough code to make the test pass and then repeating.
Mechanics of TDD
- Write a single test
- Compile it. It shouldn’t compile because you’ve not written the implementation code
- Implement just enough code to get the test to compile
- Run the test and see it fail
- Implement just enough code to get the test to pass
- Run the test and see it pass
- Refactor for clarity and “once and only once” - Remove duplication
- Repeat as needed to meet requirements
So what exactly Unit Tests are?
Well the tests what we have to create based on TDD concept, which verifies our production code, known as Unit tests. Following elaborates the concept more:
Unit testing must be primarily done by the programmer, and the immediate benefit is to the programmer. Each individual unit of functionality (method) is tested, in isolation where possible, to make sure that the individual building blocks of your application are solid.
One might will argue that if the developer is writing all the unit tests, it is quite possible to get the set of unit tests that are passable, because these unit tests are developed based either on the fore-knowledge of application code or the assumptions made in the application code.
So, to handle such kind of scenarios, there must exist a strict as well as more importantly a disciplined review and verification process. Either application architect or analyst/technical lead can review all the unit test cases and test the developed code against the cases and functionalities.
Why Tests? Or Need of Unit Tests?
Why exactly should you test? Well the answer is you should test to ensure that the software is behaving exactly as intended. Tests allow you to detect the defects that cause the software to behave in an unexpected fashion.
You don’t have to – unless you have to maintain code written by someone else, give code to others to maintain, want to make sure that bug you fixed doesn’t creep back in, want confidence that the new feature you added doesn’t break something else, etc, etc
- The most of the projects developed today are always under tight development schedules and usually have only its developers as the tester of their code
- By writing the unit tests, developers can have a head start towards bug-free and quality code
- Code will get tested more rigorously and therefore the product ships with fewer bugs
- When we make changes to the code, we can have a bunch of automated tests to make sure that we didn't break something, somewhere
- It makes the code better if we write the tests first, then write the code to pass the tests
- It makes testing easier. We don't have to run the application over and over simply to get to the point where a block of code is executed
- When something fails, writing a unit test for that failure guarantees that it's fixed for once and for all
- Unit testing offers the shortest feedback cycle between writing code and finding out if you wrote it incorrectly
What should be tested?
The simplistic answer is everything. However, everything does not have an equally likely chance of having a bug. So the more practical answer is “everything that could possibly break”.
Realize of course that the goal of testing is to break things, not show that they work.
Typical test cases are:
Ultimately the goal of any software product is Quality and I believe maintaining right set of Unit Tests will always helps in achieving this goal.
Thanks for reading!!! AND looking forward for your feedback & comments.
Author | Digitizing Businesses | Growing Technologist | Creating Employment
3 年Thanks Manoj Kumar for this topic. This topic may be vintage but very relevant topic. I like the fact that you specifically indicate unit testing is developer’s responsibility. With the maturity of QA discipline, there seem to be a notion that developers do not need to test their code.
Senior Team Lead | Product Platform Owner @ DSV | Microsoft Certified, .NET Technologies
7 年Good Article
Consultant at Tata Consultancy Services.
7 年Very informative, and really like the way fundamental were explained in structured way, keep posting
Azure Solution Architect | Royal Mail | TCS (London)
7 年Very well written article. Test driven development is the de facto standard these days. Not only does this reduces the overall maintenance cost of the application by reducing the regresion testing and unit testing cost but also forces the developer to write testable code. Unit test is a tool, its effectivness ia determined by how detailed test cases are. In case of tdd this will drive the quality of the code as well. Thanks Manoj for the great article.