Blogs


Unit testing and test driven development

 February 11, 2021, 06:58 PM

 7 min read

testing
unit-test
test-driven-development

One of the most crucial aspects of developing a software, is testing. There are a variety of tests that you can perform on your application. Such tests can be as simple as just visually checking your software, or as intricate as integrated system tests. As a self-taught developer, I have always ignored tests other than manually checking an app with my eyes or just printing out some variables to see if it is as expected. Obviously, this iOne of the most crucial aspects of developing a software, is testing. There are a variety of tests that you can perform on your application. Such tests can be as simple as just visually checking your software, or as intricate as integrated system tests. As a self-taught developer, I have always ignored tests other than manually checking an app with my eyes or just printing out some variables to see if it is as expected. Obviously, this is not the best of practices. Recently, I have been working on an app with a friend and I soon realized how imperative tests are in a team environment. Sure manually printing variables does the job for me but how can I ensure that my friend - \*ehem\* colleague - also understands what is going on. Hence I set forth on a journey to learn testing in software development. As I have mentioned earlier, there are plenty of tests, but the one I want to talk about is **unit testing**.


Unit Testing
Quite simply, unit testing is a method of testing individual modules or "part" of a code, isolated from other functions. It helps to identify errors during the early phases of development. Upon changing an already implemented and tested functionality, it can help to detect and resolve bugs easily. It improves the build quality and the quality of the code written by the developers as future developers can use the unit tests as a guide to understand the code.

How to implement it exactly?
First, let us define an individual module. We will have a function add(x, y) which, as the name suggests, adds x and y and returns the sum. We can achieve this by using a single line of code in python.

assert add(4, 2) == 6, "Add function failed, should be 6"

When the above tests passes, there will no output in the terminal. However, if it fails, it will raise an AssertionError and print the text added after the comma.


Paradigm of writing code - Test Driven Development (TDD)
When developing a software, a very popular method is to write the code, build components and functionalities and then write tests for them. This approach works fine for.. probably most of the apps. But a better approach is to write the tests about the components of your application prior to actually building the component. This helps developers to stay in track by running the tests until it no longer raises any error.

Using our previous example of add(x, y), before even defining the add function, we write the test. We can then start working on the add function. Approaching development by this process helps us eliminate some of the time we use to think extensively about the code. By writing the test beforehand, we know what we want to achieve, and we get a general idea of what we have to do. Sure, in this example, it is probably not the best use case since by using this model you only save a few seconds at most, but in more complicated applications, compromised of smaller and some-what simple components, it can help a developer, or a team of developers to save hours or even days of development!s not the best of practices. Recently, I have been working on an app with a friend and I soon realized how imperative tests are in a team environment. Sure manually printing variables does the job for me but how can I ensure that my friend - ehem colleague - also understands what is going on. Hence I set forth on a journey to learn testing in software development. As I have mentioned earlier, there are plenty of tests, but the one I want to talk about is unit testing.


Unit testing
Quite simply, unit testing is a method of testing individual modules or "part" of a code, isolated from other functions. It helps to identify errors during the early phases of development. Upon changing an already implemented and tested functionality, it can help to detect and resolve bugs easily. It improves the build quality and the quality of the code written by the developers as future developers can use the unit tests as a guide to understand the code.

How to implement it exactly?
First, let us define an individual module. We will have a function add(x, y) which, as the name suggests, adds x and y and returns the sum. We can achieve this by using a single line of code in python.

assert add(4, 2) == 6, "Add function failed, should be 6"

When the above tests passes, there will no output in the terminal. However, if it fails, it will raise an AssertionError and print the text added after the comma.


Paradigm of writing code - Test Driven Development (TDD)
When developing a software, a very popular method is to write the code, build components and functionalities and then write tests for them. This approach works fine for.. probably most of the apps. But a better approach is to write the tests about the components of your application prior to actually building the component. This helps developers to stay in track by running the tests until it no longer raises any error.

Using our previous example of add(x, y), before even defining the add function, we write the test. We can then start working on the add function. Approaching development by this process helps us eliminate some of the time we use to think extensively about the code. By writing the test beforehand, we know what we want to achieve, and we get a general idea of what we have to do. Sure, in this example, it is probably not the best use case since by using this model you only save a few seconds at most, but in more complicated applications, compromised of smaller and some-what simple components, it can help a developer, or a team of developers to save hours or even days of development!


Arafat

Mohammad Arafat Zaman

"Technophile"


Go back

Mohammad Arafat Zaman © 2025


All rights reserved