The importance of unit testing

Ahmad Abuwardeh
Edraak Engineering
Published in
4 min readMar 11, 2021
Photo by David Travis on Unsplash

It is hard to overestimate the importance of software quality. A single error can have a huge negative impact on your project. If bugs are not addressed in a timely, efficient manner, they may turn into an endless cycle of fixes or even hurt your company’s reputation.

There are numerous testing techniques to choose from, but today we want to tell you about a basic yet very effective one called unit testing. Let’s have a look at the role of unit testing in software testing and how it makes sure your product works properly.

What is unit testing?

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 make sure that the code works correctly, and performs as intended. This helps to detect and protect against bugs in the future.

The basic feature of unit testing is isolation — a unit test only performs a specific function and excludes all external influences, such as dependencies between units, calls to other functions, etc.

unit testing is not something you will see in an end product — it’s a type of testing performed during the coding stage by(developer or tester).

Code without tests is broken as designed. — Jacob Kaplan-Moss

Why unit testing?

In software development, testing is paramount. So why should I do it, you ask?

  • Tests have a short feedback loop, enabling you and your team to learn faster and adjust
  • Less time is spent debugging, allowing you to spend more time writing code
  • Fixing an issue in unit testing can fix many other issues occurring in later development and testing stages
  • Tests act as documentation for your code!
  • They improve code quality while reducing bugs
  • After refactoring the code, your tests will tell you whether the change has broken previously working code
  • Writing unit tests help developers to understand the code better. It can often be a good way for new developers on the project to get to know your code if it was written by someone else

“One of the biggest benefits of unit testing is that it also serves as credible proof of the quality of the technology your product is built using.”.

How to do unit testing

I’ll go over the structure of a typical unit test, which is usually represented by the Arrange-Act-Assert pattern (AAA), it is a great way to structure test cases. It prescribes an order of operations:

  1. Arrange inputs and targets. Arrange steps should set up the test case. Does the test require any objects or special settings? Does it need to prep a database? Does it need to log into a web app? Handle all of these operations at the start of the test.
  2. Act on the target behavior. Act steps should cover the main thing to be tested. This could be calling a function or method, calling a REST API, or interacting with a web page. Keep actions focused on the target behavior.
  3. Assert expected outcomes. Act steps should elicit some sort of response. Assert steps verify the goodness or badness of that response. Sometimes, assertions are as simple as checking numeric or string values. Other times, they may require checking multiple facets of a system. Assertions will ultimately determine if the test passes or fails.

Here’s a basic unit test for Python’s absolute value function:

def test_abs_for_a_negative_number():   # Arrange
negative = -5
# Act
answer = abs(negative)
# Assert
assert answer == 5

This test may seem trivial, but we can use it to illustrate our pattern. I like to write comments denoting each phase of the test case as well.

  • The Arrange step creates a variable named “negative” for testing.
  • The Act step calls the “abs” function using the “negative” variable and stores the returned value in a variable named “answer.”
  • The Assert step verifies that “answer” is a positive value.

Conclusion

The importance of unit testing should never be underestimated. It is a powerful tool that brings a lot of benefits to software development. With unit testing, you have a chance to catch and fix a lot of bugs early in the development process, thus significantly improving the quality of your software solution. Unit tests have proven themselves to be a great method within the quality assurance that help you both rid your code of flaws and save money doing it

--

--