Developers and testers play key roles in the creation of software applications in the Software Development Life Cycle (SDLC). In the early days of software development, developers were only responsible for building code, and testers were only responsible for testing the quality of the software application after development. As a result, issues did not arise until the final stages of the SDLC during functional/end-to-end testing. These issues could be further compounded by the potential complexity of the issue, and the project could be delayed and/or go over budget. The introduction of unit testinghelped identify issues in earlier stages, during development instead of at the end, and afforded developers time to address and resolve issues.
Now, unit test cases are a standard part of the modern developers’ deliverables. Processes such as Test-Driven Development (TDD) evolved so developers can write tests even before writing the application code. However, this added responsibility is not embraced by all developers and builds continue to fail because developers either do not do unit testing or do not meet the unit test coverage percentage. Many developers cite meeting project deadlines as the reason for omitting unit testing without realizing how projects could suffer in the long run if unit tests are not implemented. Developers should have a unit test for every line of code that gets introduced. This can only happen if developers understand the benefits and importance of unit testing and are encouraged to adopt it as part of development.
Figure 1 - Some perceptions developers may have about unit testing
As development occurs in the early stages of SDLC, unit testing is key to ensure quality code by checking the code for failures early and often. This aligns well with the “Fail Fast!” concept that supports a speed-to-market strategy necessary in today’s competitive landscape. Developers who incorporate this practice as part of their role would contribute to the accelerated development of software applications.
Here are four important benefits to consider to encourage developers to incorporate unit testing into their build processes.
1. Unit Testing Explores Expected and Unexpected Scenarios While Ensuring the Quality and Stability of the Codebase
Developers perform system testing called development testing. With this manual validation approach, developers might end-up validating existing functionality that they were already aware of; however, they would likely miss issues that arise from scaling or issues that occur in corner or edge cases. For example, if a developer makes changes to an existing API, they would test the changes by validating API through Postman. This might be simple for a small change; however, as the application size grows, the number of developers contributing to the code base increases, and their validation efforts will also increase significantly.
Also, the code may look simple when reviewed in isolation because a unit testing only performs a specific task and prohibits all external effects, such as dependencies between units, calls to other functions, etc. However, as the code evolves, it can grow more complex. This might cause a delay in the releases as it does not account for unexpected outcomes.
Let’s assume developers write unit tests for every code change they make in the application. With this approach, they have unit tests in place that act as guardrails that can validate all the existing functionality. Creating unit tests also provides stability to the codebase as it scales. Thus, having unit tests as a part of the code will make code development better and faster.
2. Unit Testing Is More Efficient and Effective Than Manual Validations
Unit testing may seem more time consuming than manual validation, but imagine a scenario where a developer requires a day for code changes and one additional day to write unit tests for respective code changes. One day’s effort for unit testing vs. 3-4 hours for manual validation might appear like a no brainer; however, consider if, over a while, another developer needs to modify the same code to accommodate new business requirements. For these new requirements, developers would likely make changes to code and push it to production. It may end up breaking the existing functionality because there are no existing unit tests to warn the new developer leading to wasted time and resources and increased effort overall.
In reality, multiple teams work on the same codebase, and inevitably, changes will be required. It is critical to have proper unit tests in place to ensure code validity and to save on overall efforts by providing a guide to the code.
3. Unit Testing Helps Avoid Invalid Tests
As processes evolve, code coverage is checked as part of the continuous integration pipeline to provide immediate feedback to developers in cases when coverage goes below a certain percentage. The purpose of this validation is to ensure quality and that expectations are met when multiple teams/squads commit code.
However, some developers treat this as a deliverable because it is enforced in the build pipeline. It is recommended that the developers' culture should treat unit testing as part of the development, and they should write tests for every line of code that they introduce. Otherwise, they end-up having invalid tests, for instance, boilerplate code such as getters/setters to fulfill the unit test coverage percent.
4. Unit Testing Has Been Proven to Provide More Value Than Other Tests
Quality control plays a critical role in speed-to-market development. As part of this, many different types of automation tests, such as service tests, UI tests, and integration tests, were introduced in addition to unit testing. However, it has been proven that unit testing provides more value with less cost.
Figure 2 - This image was adapted from Martin Fowler’s article on the test pyramid
Developers Must Include Unit Testing in Their Build Culture
Most working developers have been trained to include unit testing as a part of the technical stack driving speed-to-market strategies. Unit testing is a simple concept in terms of the technical stack; however, in my experience, the adoption rate of unit testing has been low and requires a cultural mind-shift to be truly adopted as a part of the developers' practice. Oftentimes, it is perceived as outside of their scope, and many times, developers tend to overlook the importance of building and maintaining quality code due to immediate pressures to deliver projects on time and within budget. There has to be a cultural shift in the way developers build and test to allow them to build quality code with speed, which then allows them to scale without issues, and achieve long-term business goals to help their companies stay ahead of the competition.
Nisum has expertise in implementing development practices, such as TDD, CI/CD, and microservice architecture. Nisum has helped enterprise clients with monolithic applications migrate to a microservice architecture by enabling unit testing that helped them drive speed-to-market strategies. Unit testing with TDD involves both a technology and a process mindset, and Nisum has demonstrated proven-results in implementing testing for over a decade. Nisum has a strong track record of helping our clients achieve their business goals by using the latest application development techniques and the newest best practices. To learn more about our services, contact us.