A gentle introduction to unit test and testing
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
PHP: Tips of the Day
Single Line Comments
The single line comment begins with "//" or "#". When encountered, all text to the right will be ignored by the PHP interpreter.
// This is a comment # This is also a comment echo "Hello World!"; // This is also a comment, beginning where we see "//"
Multi Line Comments
The multi-line comment can be used to comment out large blocks of code. It begins with /* and ends with */.
/* This is a multi-line comment. It spans multiple lines. This is still part of the comment. */
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook