“The principal objective of unit testing is to give confidence in the code.”

What is Unit Testing?

As per the definition, Unit Testing is nothing but the testing of individual hardware or software components. In simple words, Unit Testing is the concept of executing the small code section from a complete application.

Why Developers Should Do Unit Testing?

One of the main reasons for developers not doing the unit test is to save the developing time. Developers are thinking that unit test makes the development time to increase but the real fact is that by writing unit test debugging can be done easily and confidence on the result will be more after a feature has been released through the unit testing.

How To Do Unit Testing?

The following example explains how to run a sample unit test in PHP.
Note: I have done this example in the Laravel framework and Ubuntu system.

First, need to install the PHPUnit in your local system.

Then write a sample test and run the execution command for PHPUnit.
Since it’s in Laravel framework I have run the execution command as

Let’s check the below example of loading the index URL and its output.

In the above example, I have given the URL as ’/’ which is defined in the application. So the result will be:

Output:

Next, I am going to make some mistakes in the test. For example, I will be giving the test to load a URL that is not defined in the application.

In the above example, I have given the URL as notAvailable which is not defined in the application. So the result will be:

Advantages of Unit Testing

Better Code Architecture

While writing the unit test, the standard of the code architecture gets improved by thinking of how this code gets used in the system.

Increase in Confidence

After unit testing, basic bugs can be resolved at the initial stage itself which gives more confidence on our application and there’s no need for larger maintenance period if a basic bug has been found in the production environment.

Code Refactor

The major problem faced by the developers is to change the code of a feature which is working fine. So adding some code for that feature may break the existing flow. This will not happen if we have written the unit test, thereby giving confidence.

 

Disadvantages of Unit Testing

  • The major disadvantage of unit testing is error capturing. Unit testing will not capture all errors.
  • Since unit testing is done for a small section of code so there will an absence of a full application testing.

Why No Unit Testing In Reality?

While developing an application, most of them will avoid unit testing. This is maybe due to the following reasons:

The Myth of Difficulties for Writing Unit Test

Most developers think that writing unit tests is very difficult. But these days most of the frameworks have some default unit testing modules which will be helpful for this problem.

No Time Spent on Unit Testing

The second reason will be the allocation of time for unit testing. However, unit testing should be done so that it will be a best practice in software development.

 

 

References

https://glossary.istqb.org/
https://en.wikipedia.org/wiki/Unit_testing
https://laravel.com/docs/5.5/testing




Ready to Write an Interesting blog in QA Touch?


If you have an interesting blog post you would want to share with the Testing Community, please forward it to info@qatouch.com along with your picture and a short bio. Our editorial team will review and publish it.

Subscribe to our blog

Ahilan SM

Ahilan SM

Ahilan, Product Associate at DCKAP is our enthusiasm quotient. Being a passionate LARAVEL developer, he handles the complete spectrum of qatouch usability and support.He rejoices listening music, you can be found him watching movies at theatres in Chennai during the weekends.

Leave a Reply

avatar