DEV Community

Mahadi Hasan
Mahadi Hasan

Posted on

Unit Testing

Unit testing still causes controversy among developers and product managers. There are both opponents and supporters of this kind of testing. In this article, I’ll highlight the main advantages of unit testing.
What are unit tests, why unit testing is essential, and how do they help developers and business owners? For the answers to these questions and more, read on.
Let’s start with the definition: Unit testing is a software testing method where “units”—the individual components of software—are tested. Developers write unit tests for their code to ensure that the code works correctly. This helps to detect and protect against bugs in the future.
Sometimes developers write unit tests first, then write the code. This approach is also known as test-driven development (TDD). In TDD, requirements are turned into specific test cases; then, the software is improved to pass the new tests. No code is added that hasn’t been proven to meet defined requirements in this approach. Unit testing is similar in that it allows developers to modify code without affecting the functionality of other units or the product as a whole.
Unit tests are usually written in the form of functions and check the value and behaviour of these functions in various scenarios. For example, imagine a function for dividing two numbers: the developer decides to follow the TDD approach, first writing a test with the input of values ‘4’ and ‘2’ (4 divided by 2) with ‘2’ expected as a result. Another example is that when the divisor is zero, we don’t expect that the function will produce a value—we hope it will generate an exception.

Top comments (0)