What Is The Unit Test?

Berkin Ergürbüz
5 min readMar 4, 2023

--

Hi everyone, we wrote a small article about our experience in Unit Testing with my teammate Fatmagul Polat. Have a nice read :)

Unit testing is a method that verifies the behavior of a software by running it in isolation from all dependencies of its smallest unit. Unit tests allow us to verify the behavior of the code we write by writing code, and because it is the smallest testable part of the software, unit tests are developed by developers.

What is TDD?

Test driven Development is a practice that we have heard often in recent years and is beginning to spread. What he basically says is that his test must be written before writing the application code.

Why do we develop unit test and benefits?

There are many benefits to being able to write unit tests, we will try to talk about them.

Developing with the TDD approach requires a thorough review of the requirements or design before any functional code is written. We start by writing unit tests for development. This process will lead us to think more intensively about the features we will develop and allow us to focus only on the requirements.Starting to code directly can make it a little more difficult for us to focus on the design and flow of the code while developing. This process will help us make fewer mistakes and write simpler and cleaner code, focusing only on requirements.

A benefit of writing unit tests is that you can recognize the errors that may occur in the development stage and have the opportunity to correct them. The more time it takes between the occurrence of an error and its detection, the more cost it will cost to resolve the error. Unit Test will help us to prevent this cost increase.

Our software is constantly evolving and changing, so we will need to make changes to the methods we have written. If improvements or changes are causing an unexpected error, this error will be captured by tests we have written for this code before, and we can recognize this and take action in the development phase. When we add a new feature to a class whose tests are written, we usually focus on testing the added feature. Even if this new feature is working properly, it may cause this class to fail to perform a feature it can currently perform. We will not always be able to test pre-determined cashews. In such cases, having pre-written tests of existing features protects against unwanted or overlooked errors.

Every added feature causes our existing unit tests to fail, and if we need to rearrange these unit tests, it actually goes against the letter “O” of SOLID principles, i.e. our method is not open to change.

Writing a unit test encourages you to reduce the complexity of the code and write better quality code. It will be very difficult to write the test of methods that are complex and contain a lot of logic.

It will allow us to write our code into smaller methods to write testable code, with only one task in each method. Over time, these approaches will create a culture of software development for us.

When testing our codes, we need to be completely isolated from external dependencies, because what we are interested in is the code in the unit we want to test. We are not interested in the behavior of external dependencies. The need to test independently and isolated from the pieces of code on which the units we write are dependent allows us to abstract these units and design code into reusable small pieces. Examples of external dependencies are methods by which operations are called to the database, outsourcing endpoints, etc.

By writing testable code, we actually begin to apply more of the SOLID principles.

Well-written unit tests contain good examples of what outputs will be taken when the function or component we are examining is given which inputs. It allows us to see how the code being tested works as a happy path, with examples of what the end scenarios are. We can easily understand how our methods will react by examining unit tests. These tests may be document-specific for the project. By examining the tests, it can be significantly helpful in understanding how the methods work.

How to develop a Unit Test?

The steps in the TDD approach are as follows;

1.Developing unit test.

2. The test fails.

3. The test is coded to be successful.

4. Test will be successful.

5. The code is refactored.

Before starting these steps, it is necessary to make a declaration of the classes and methods whose test we will write. First, we should write the test of the methods and then see that this test failed, encode the relevant method and continue the process after the test is successful.

After completing these processes for our existing methods, we run all our tests and expect our test methods to work successfully. The improvement we make in order for one test method to be successful may cause other tests to fail. In this case, we will refactor the code and complete the process after the tests are successful again.

Apart from the TDD approach, we start writing unit test methods in the traditional software development process.

To sum up;

At the end of the day, the purpose of unit testing should not be just to say that we have unit tests in our project. Writing unit testing methods that really serve our purpose will bring us many things. This way, we will have a better quality code development culture by trying to write testable code. It should help us recognize errors at the earliest stage by anticipating how an improvement in our code affects other behaviors and where we might get errors.

In our next article, we will try to explain how we develop unit test methods and explain the related processes by sampling them. See you soon

Our email addresses for any feedbacks and suggestions:

Berkin — berkinergurbuz@gmail.com
Fatmagül — polat.ftmgl@gmail.com

Link to the Turkish article :

https://medium.com/@fgulpolat/unit-test-nedir-1518dbfd84ca

--

--