Purpose of the Article
Black box testers don’t care about unit testing. Their main goal is to validate the application against the requirements without going into the implementation details. But as a curiosity or out of the box thinking, have you ever wondered how developers test their own code? What method they use to test before releasing code for testing? How dev-testing is important in agile process? The answer to all this is unit testing. I want to educate you on the importance of unit testing so that development and testing teams can work more collaboratively to design, test and release a excellent application.
Who knows in the future some of you may even switch to white box testing and use these code validation and improvement techniques!
What is Unit Testing?
Unit testing is not a new concept. It’s been there since the early days of the programming. Usually developers and sometime white box testers write unit tests to improve code quality by verifying each and every unit of the code used to implement functional requirements (aka test driven development TDD or test-first development).
Most of us might know the classic definition of unit testing –
“Unit testing is the method of verifying smallest piece of testable code against its purpose.” If the purpose or requirement failed then unit test has failed.
In simple words, unit testing means – writing a piece of code (unit test) to verify the code (unit) written for implementing requirements.
Unit testing in SDLC:
Importance of Writing Unit Tests
Unit testing is used to design robust software components that help maintain code and eliminate the issues in code units. We all know the importance of finding and fixing the defects in early stage of software development cycle. Unit testing serves the same purpose. Unit testing is the integral part of the agile software development process. When nightly build run unit test suite should run and report should be generated. If any of the unit test has failed then QA team should not accept that build for verification. If we set this as a standard process, many defects would be caught in the early development cycle, saving much testing time.
I know many developers hate to write unit tests. They either ignore or write bad unit test cases due to tight scheduled or lack of seriousness (yea they write empty unit tests, so 100% of them pass successfully ;-)). It’s important to write good unit tests or don’t write them at all. It’s even more important to provide sufficient time and supportive environment for real benefits.
- Testing can be done in the early phases of the software development life cycle when other modules may not be available for integration
- Fixing an issue in unit testing can fix many other issues occurring in later development and testing stages
- Cost of fixing a defect found in unit testing is very less than the one found in the system or acceptance testing
- Code coverage can be measured
- Fever bugs in the system and acceptance testing
- Code completeness can be demonstrated using unit tests. This is more useful in agile process. Testers don’t get the functional builds to test until integration is completed. Code completion cannot be justified by showing that you have written and checked in the code. But running unit test can demonstrate code completeness.
- Expect robust design and development as developers write test cases by understanding the specifications first.
- Easily identify who broke the build
- Saves development time: Code completion may take more time but due to decreased defect count overall development time can be saved.
Unit Testing Cycle:
[Image credit Typemock.com]
What Makes a Good Unit Test?
Well, I’m not the right person to tell what makes a good unit test, but based on my observations on various projects I can definitely tell the characteristics of a good unit test. Bad unit test does not add value to the project. Instead, project cost increases significantly writing and managing bad unit tests.
How to write good unit tests?
- Unit test should be written to verify single unit of code and not the integration.
- Small and isolated unit tests with clear naming would make it very easy to write and maintain.
- Changing other part of the software should not affect on unit test, if those are isolated and written for a specific unit of code.
- It should run quickly
- Unit test should be reusable
Unit Testing Frameworks
Unit testing frameworks are mostly used to help write unit tests quickly and easily. Most of the programming languages does not support unit testing with inbuilt compiler. Third party open source and commercial tools can be used to make unit testing even more fun.
List of popular unit testing tools for different programming languages:
- Java framework – JUnit
- PHP framework – PHPUnit
- C++ frameworks – UnitTest++ and Google C++
- .NET framework – NUnit
- Python framework – py.test
Unit Test Example
Video tutorial explaining how to write unit test using PHPUnit testing framework:
Misconceptions and Truths
- It takes more time to write code with unit test cases, and we don’t have time for that – In reality, unit testing would save your development time in the long run.
- Unit testing will find all bugs – It won’t, as intent of unit test is not to find bugs but develop robust software components which will have fewer defects in later stages of SDLC.
- 100% code coverage means 100% test coverage – This does not guarantee that code is error-free.
While unit testing offers many advantages, there are also limitations involved with using it. Rigorous discipline and consistency is required throughout the software development process to overcome limitations and get the intended benefits.
Your Comments are Most Welcomed!
As a black box tester what are your observations about unit testing on your team? Does anyone have better idea for successful unit testing?