w3resource

A gentle introduction to unit test and testing

Introduction

In our everyday life, we in one way or the other find ways to check or test out things to ensure their functionalities suits our requirements. We test gadgets before paying for them, we even test food when we are in the kitchen, testing is very essential in life.

In programming testing  is an essential part of every programming language. If we don’t test our codes, how do we verify that it works as expected? Manual testing though can be performed irregularly, and this is usually very limited. Thus, the solution to proper software testing is writing automated tests which allows our application to be extensively tested just by executing certain written codes. In PHP such tests are written usually with a unit testing framework.

Over the years, unit testing has gained popularity and it now tends to be a standard practice in PHP with many popular frameworks such as Laravel, Code Igniter and Phalcon all requiring unit test coverage of their source code.

Though unit testing is often seen as tedious and time consuming, which could be true most of the time, but spending time writing tests improves the overall quality in our code, making our program bug free, and ensuring maximum performance. Unit testing in general gives us the confidence that our programs can be depended on.

Myths about Unit Testing

Unit testing and virtually all other forms of testing, have some developer myths which often hinders their adoption by developers.

  • Unit testing is time consuming.
  • Unit testing is too hard.
  • Complex codes cannot be tested.
  • If my code is working, why write a test.
  • Writing unit test is boring.

These are testing fallacies, excuses which appear quite reasonable but are misinformed in subtle ways. So, let’s set things right!

Is testing time consuming? This question should rather be rephrased, does testing worth the time it take? Testing should be considered an integral part of software development as it eliminates or reduces the future time that should be spent on modifying, refactoring and fixing unexpected bugs in our code. Testing may not completely eliminate bugs, but just imagine the bugs without testing.

Complex codes cannot be tested: Complex codes is made up of smaller practical parts and testing these smaller parts will sum to testing the whole complex code. If our codes can’t easily be tested, then we have failed in writing practical and well-structured flexible codes which can be easily decoupled. It is always advised to write tests alongside with our codes to prevent us having to wait for so long to start testing ‘complex’ codes.

Does the code work? Yes, our codes may be working properly, but a working code is completely different from a working tested code. Tests alleviates us from the pain encountered when making changes, refactoring and adding new features since most integration issues have already been detected during the test.

Finally, is testing really and always boring? A lot of developers will say that testing is boring, but this is not true. They may find it boring because they will always wait till the end of the development phase to start writing tests. If you constantly switch from test to code, and forth you will actually get more accomplished with less fatigue. So, to prevent the assumed boredom of testing always switch tasks and don’t  fall into the trap of having to wait till the end of the application to start writing tests.

Unit Testing in PHP

Like we mentioned earlier, unit testing exists in almost if not all the programming languages, but for the purpose of this tutorial, we will briefly introduce unit testing in PHP programming language.

There are many ways to perform unit testing in PHP, the three most common methods are Marcus Baker’s SimpleTest and Sebastian Bergmann’s PHPUnit and the old but reliable phpt. All three allow us to write unit test codes in php and they offer a vast lot of functionalities.

In this tutorial, we talked about Testing and Unit test, and we tried to make us see the reasons why we must write test for our codes. In the next tutorial, we will look at the things required for us to write our first PHP test.

Don’t forget to Like, Share and Comment on this tutorial. Follow us on social media for more updates, bye for now.

Next: Installation of php unit



Follow us on Facebook and Twitter for latest update.