PHP Unit Test & Test-Driven Development
Test-Driven Development! Sounds fancy! It’s not new but you still hear about it every now and then. Do you know what it is? Do you know why they talk about it? If you don’t know, this might get interesting for you. The idea behind test-driven development is plain and easy: Before we write any code we start with defining and writing test cases for our upcoming function/class or what ever we are going to write. Simple? Nooooo.
The best thing about test-driven development is that errors are found quickly and in the beginning of the project and it’s easy to know when a function or method are doing to many different things at once. Errors and bugs are expensive in the long run. To debug hundreds of lines of code is time consuming and can be avoided by using the test-driven development concept.
So, what so hard about it? The hard thing is to write good and useful test cases. There is no point writing test cases and creating unit tests that doesn’t really test the function/method or class you are working on. And figuring out and writing those straight on test cases can be tricky. Plus, the creation of test cases is rather time consuming and in the beginning it’s hard to see the point of writing them. You should remember that tested code is always better then non-tested code, no exceptions. In the end you feel more comfortable with your code since you know it’s working and is correct, your partners will be happy since the code they are receiving works and finally your clients will be happy because bugs in the end of a project is rare.
I am just in my early stage working with the test-driven development concept. I have been working in teams containing small groups at my university where each group are solving a specific task and end the end we put it all together. If we just focus on the test-driven development concept and not all the communication, planning and other concepts I have found that test-driven development is great! With some modifications…
Writing test cases from scratch is very time consuming. When you are on a tightly schedule it’s hard to estimate the time to write these tests. Also, the very first time you seriously going to write these tests it’s, once again, hard to know what to test, what’s important and whats not. Since the whole point of writing test cases is to be able to run them as many times as you want and make sure every time you update something your test cases still go through. My suggestion here is to start with small functions and start writing up some unit test for them. A second point my team and our teachers talked about was that writing test before wrting anything else doesn’t work. You might have an excellent idea on how to solve a task and you can’t start because you need to write test cases first, this is not idea! You try to write as many test cases as you can before you start coding but you continue updating and adding those test cases when write your code. The important thing is that in the very end you have a test case for your function.
Unit Testing isn’t something special or unique for PHP, it is found in almost all programming and scripting languages in some way. Now when we have looked upon the test-driven development concept we are about to see how we can implement a test-driven environment in PHP. Many things in life can be hard, this isn’t one of those things. We are going to look at the very basic of Unit Testing and we are going to use SimpleTest, a free PHP unit test and web test framework.
- Download the SimpleTest framwork
- Unzip the tar.gz file into your WAMP www folder
The installation is complete! You don’t need to do anything except unzipping that tarball into your WAMP www folder. Continuing, there are a few required lines of code we must follow to be able to use the SimpleTest framework. First we need to include a file named autorun.php. This is found in simpletest/autorun.php and depending on your current location you might have to add ../ or other folder names to get to it. We create a new PHP file and we give it a name, unittest.php. The first to lines looks like this:
<?php
require_once('../simpletest/autorun.php');
We force the script to make sure that the autorun.php file exists by using the require_once method. In my case I’m currently working in a folder deeper in the main www folder and therefore I use ../ to get my hands on the autorun.php file. Next we will declare and implement a class. You can choose whatever name on the class you want but in our example it needs to be extended by UnitTestCase. (If we want to use TestSuits we extend our class with TestSuits, these are used when we want to group many UnitTestCase classes together).
class Tests extends UnitTestCase
{
I named the class Tests and it extends the UnitTestCase. Well inside the class we can define functions and methods. There are two important functions you might want to use when the test starts and when the test ends, there are called setUp() and tearDown(). These methods are activated on startup and in the end of the test and here you can place things like instantiating and removing objects and more.
function test()
{
Here we have created a new function called test(). At the moment it’s empty but we will fill it soon. These method will be automatically activated when we run our script. Inside we do a very simple test:
$this->assertEqual('test', 'test');
The assertEqual class method looks at the first parameter and see if it’s the same, equal, to the second one. If it is the test is OK and completed, otherwise it will raise an error. Except for the assertEqual you can also test assertTrue and assertFalse, just to name a few. In our example above we will get 0 failures and 1 completed. To run our test file, we need to make sure that the WAMP software is up and running and in our browser navigate to your newly created test page using localhost in the beginning. In our case will would navigate to the unittest.php file. On entering that page the setUp() method will be called and our tests will be tested, in the end the tearDown() method will popup and remove everything we have done, if that’s what we want.
Unit testing is a great way of knowing if your code is doing what it should. It’s a great feeling knowing that all code I have written today have been tested and is working as it should. You will be able to sleep at night not worring about mysterious errors popping up when the product or project is released. Much more information about SimpleTest can be found at their website. I strongly recommend you to continue reading there. Enjoy!
About this entry
You’re currently reading “PHP Unit Test & Test-Driven Development,” an entry on Is the coffee still warm?
- Published:
- July 3, 2008 / 7:35 am
- Category:
- July
- Tags:
- extends, php, setUp(), simpleTest, tearDown(), test case, test-driven, TestSuit, unit test, UnitTestCase, web development
5 Comments
Jump to comment form | comments rss [?] | trackback uri [?]