What is Unit Testing?
Unit Testing is a type of software testing where individual units or components of a software or program of a software are tested. The purpose is to validate that each unit of the software or program performs as expected.
Unit Testing is not a new concept. It’s been there since the early days of programming. Usually, developers and sometimes White box testers write Unit tests to improve code quality by verifying every unit of the code used to implement functional requirements.
In simple words, it means – writing a piece of code (unit test) to verify the code (unit) written for implementing requirements.
Unit Testing In SDLC
In Unit testing, developers use manual or automated tests to ensure that each unit in the software meets the customer’s requirement. This unit can be an individual function, object, method, procedure, or module in the software under test.
Writing unit tests to test the individual units makes writing comprehensive tests easier as all the units are put together. During software development, it is done as the first level of testing.
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 defects in the early stage of the software development cycle. This testing serves the same purpose.
It is an integral part of the agile software development process. When a nightly build run unit test suite should run and report should be generated. If any of the unit tests have failed then the 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.
Unit Testing Methods
It can be performed in 2 ways :
- Manual Testing
- Automated Testing
In Manual Testing, the tester manually executes test cases without using any automation tool. Here, each stage of the test is executed manually. Manual Testing is tedious especially for tests that are repetitive and requires more effort to create and execute test cases. Manual Testing does not require knowledge of any testing tool.
It is a fact that 100% of Automation is not possible and thus there will always be some level of manual testing performed.
In Automated Testing, software testing automation tools are used to automate the tests/test cases. The automation tool can record and save your test and it can be re-played as many times as needed without any further human intervention.
These tools can even enter test data into the system being tested as well as it can compare the expected results to the actual results and automatically generate the reports. However, the initial cost of setting up test automation tools is high.
Benefits Of Unit Testing
- The process becomes agile: For adding new functions or features to the existing software we need to make changes to the old code. But changing things to the already tested code can be risky as well as costly.
- Code quality improves: The quality of code is automatically improved when unit testing is done. The bugs identified during this testing are fixed before it is sent for the integration testing phase. Result in robust design and development as developers write test cases by understanding the specifications first.
- Detects bugs early: As developers run unit tests, they detect bugs early in the software development life cycle and resolves them. This includes flaws or missing parts in the specification as well as bugs in the programmer’s implementation.
- Easier changes and simplified integrations: Doing unit testing makes it easy for the developer to restructure the code, make changes, and maintain the code. It also makes testing the code after integration much easier. Fixing an issue in Unit Testing can fix many other issues occurring in later development and testing stages
- Documentation availability: Developers who are looking into the functionality at a later stage can refer to the unit testing documentation and can easily find the unit test interface and correct or work fast and easily.
- Easy debugging process: It helps in simplifying the debugging process. If the test fails at any stage the code needs to be debugged or else the process can be continued without any obstacles.
- Lower cost: When bugs are detected and resolved during unit testing, cost and development time is reduced. Without this testing, if the same bugs are detected at a later stage after the code integration, it becomes more difficult to trace and resolve, making it more costly and increasing development time.
- Code completeness can be demonstrated using unit tests: This is more useful in the 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 tests can demonstrate code completeness.
- Saves development time: Code completion may take more time but due to fewer bugs in the System and Acceptance testing, overall development time can be saved.
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 tell the characteristics of a good Unit Test. The 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?
- A Unit test should be written to verify a 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 another part of the software should not affect the Unit test if those are isolated and written for a specific unit of code.
- It should run quickly
- A 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 do not support unit testing with the 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
Misconceptions and Truths
- It takes more time to write code with Unit test cases, and we don’t have time for that – In reality, it would save your development time in the long run.
- Unit testing will find all bugs – It won’t, as the intent of the Unit test is not to find bugs but develop robust software components that 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.
How To Accept Unit Testing?
Good unit testing can be carried out in 3 basic parts.
- Write the unit test code
- Run the unit test code to check if it meets the system requirement
- Execute the software code to test for any defects and whether the code meets the system requirement.
After undertaking the above 3 steps, if the code appears to be correct then the unit test is said to be passed. And if it does not meet the system requirements, the test fails. In this case, the developer needs to recheck and correct the code.
In some instances, it is necessary to separate the code to perform this testing more accurately.
Best Practice
To create the best code during this testing, consider the below points:
- Code should be strong: There are instances where the test fails or in worst cases does not get executed at all if the code is broken.
- Understandable and reasonable: The code should be easy to understand. This makes it easy for the developer to write the code and even other developers who will work on the code subsequently will find it easy to debug.
- Should be the single case: Tests that defines multiple cases in one, are complex to work with. Thus writing a single case code is best practice, which makes the code easier to understand and debug.
- Allow automated tests: The developers should make sure that the test runs in an automated form. It should be in a continuous delivery process or integration process.
Comments
Post a Comment