Sunday, 23 June 2013

PHPUnit - Unit Testing in PHP



Guys, How many of you are familiar with unit testing in PHP? Well coming to PHP, every would say Yeah every one does it, through echo or die and exit; testing individual modules or functions. Many of us has perception that my code works well so there is no need to test it.

Well, if  you are inline across with same idea, it's what we are not been addicted of the advantages of unit testing frameworks. Instead of testing manually each and every case all time, why not we do it through automation in unit tests right? Further more , When you are under going for a product development, it really comes handy when you are required to know, how much resource a particular module is taking up. Will this module be influencing the other modules performance too? All these things has to be analyzed carefully for a successful product development at each at every stage.

The one which am I talking here is about PHPUnit testing framework which was initially inspired JUnit testing framework for Java. Well if you are PHP Geek, or amateur , you would not take much time to learn this framework and use it .  Here  I'm going to write basics about PHPUnit and simple example  to demonstrate  its usage. Without further ado lets dive into it.

PHPUnit should be installed using the PEAR installer. You need to bother about all those. I would suggest you to download XAMPP  with latest version with PHP version 5.4+ . Below 5.3.3 the PHPUnit does not work. 



I am assuming this article for windows users, Linux users can have slightly different commands. After Installation of XAMPP, there are slight  updates required to be done  in the pear package. 



Go  command line installation directory of PHP, Run it as Administrator.

If you have installed your XAMMP in F drive: 
do the following:
1) Type: F:  then press Enter
 you will be directed to F drive , then type 
  CD F:\xampp\php> 
Here you will run the PEAR command to for updating and installing some dependent package of PHPUnit. 



First one is installing -   PHP_CodeCoverage" package that does not come with the xampp itself. So install it type :  

pear channel-discover pear.phpunit.de pear channel-discover components.ez.no pear install phpunit/PHP_CodeCoverage 
Press the enter , it will be installed automatically. 



If you face any problem, then you need to make sure that your pear package is up to date. To update the Pear package type: 

pear update-channels 
It will update your pear package, the repeat the same above command.



You might face some problems relating to update too. Update may be failed because of caching problems. If you find like so , clear the cache of pear as 

pear clear-cache



That's it you are done with the installation, now for testing your script all you need to do is 

Type the command 
phpunit "FullAbsolutePathOfYourFile"      
Example : F:\xam\php>phpunit "F:\xam\htdocs\test\PersonTest.php"  





Here i will show you how will you write the test script for you actual class being tested. 



Before you dive into details for testing, you need to understand basic idea about it.  The basic idea about  testing in a very nutshell is very simple. Take an example; you have a certain class having function to add 2 numbers that returns sum of it. Now, we need to ensure that the sum is correct ie. 2+3 should yield 5. So the sum = 5 is the result we expect from that function. That means in  our testing class we need to write our testing which compares our expected output with the real output of the function. If the expected out put comes equal with that, our test result passes. That's it, all about the testing. This is the very generalized concept in a nutshell.  



Lets understand it by our example: We have a class person, when  he talks, we should expect the result as Wecome guys or some thing else whatever may be , but we do not expect the sound like MEW as cat right?  



Person.php

<?php
class Person{
    protected $name;



    public function getName() {

        return $this->name;
    }



    public function setName($name) {

        $this->name = $name;
    }



    public function talk() {

        return "Welcome guys !";
    }
}



?>



So we would like to write  UnitTesting Class for this function, which ensures that only man has spoken and not an animal.  To do so,  we write a class PersonTest which must extend PHPUnit_Framework_TestCase class



PersonTest.php

<?php
class PersonTest extends PHPUnit_Framework_TestCase
{

      // test the talk method
     public function testTalk() {
         // make an instance of the person
        $person= new Person();
        // use assertEquals to ensure the greeting is what you
        $expected = "Welcome guys !";
        $actual = $person->talk();
        $this->assertEquals($expected, $actual);


    }
}

?>

assertEquals is a method built in PHPUnit testing framework class which will compare if both the output are equal. There are many methods.

Now run this PersonTest.php class through command line as  



F:\xam\php>phpunit "F:\xam\htdocs\test\PersonTest.php"  



you will see the output something similar to as below. 




That's it You are done with the unit testing with PHP. Wow. !! Congrats.


Couple of things to note.

If test is successful,  you will  see the little dot there.  For every test run there will be a character indicating the result. The characters are as follows:
  • . – Printed when a test succeeds.
  • F – Printed when an assertion fails.
  • E – Printed when an error occurs while running the test.
  • S – Printed when the test has been skipped.
  • I – Printed when the test is marked as being incomplete

This is just an introduction to PHPUnit testing, you need to dive more detail on it. Please do refer an official PHPUnit testing manual here for further detail

Don't forgot to send your feedback enjoy up. !!!




                                                               




  












No comments:

Post a Comment