“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.
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get(‘/’);$response->assertStatus(200);
}
}
In the above example, I have given the URL as ’/’ which is defined in the application. So the result will be:
Output:
PHPUnit 7.5.14 by Sebastian Bergmann and contributors.
1 / 1 (100%)
Time: 115 ms, Memory: 18.00 MB
OK (1 test, 1 assertion)
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.
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get(‘/notAvailable’);$response->assertStatus(200);
}
}
In the above example, I have given the URL as notAvailable which is not defined in the application. So the result will be:
PHPUnit 7.5.14 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 114 ms, Memory: 18.00 MB
There was 1 failure:
1) Tests\Feature\ExampleTest::testBasicTest
Expected status code 200 but received 404.
Failed asserting that false is true.
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
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