Phpunit test tutorial for beginner

In this tutorial we are going to talk about unit testing of our php based applications, Few month back i had wrote about unit testing using nodejs and now i’ll tell you how to do same with php.

Suppose we are planning to create any product then we must ensure one thing in mind is testing of the product because testing makes your product more stable & bug free. So that you can write pre defined test cases and execute every time you made changes in your codes and ensure that everything is fine or not after making changes in your programming code.

So Here you will learn some basic methods of unit testing using phpunit.

PHPUnit! PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.

Configuring PHPUnit for your project. if you have linux machine then you can simply follow below commands and set phpunit globally.

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version

Or you can simply download phpunit.phar file from official website https://phpunit.de and place it in your project.
And goto your project directory and run below command to test

php phpunit.phar --version




Now create first php class, This is just a sample class you can follow same steps for your real project based class.
Create MathClass.php and placed it in classes folder.

classes/MathClass.php

<?php
class MathClass {
 
  public function __construct()
    {
 
    }
 
    public function sum($a, $b) {
    	$sum = $a + $b;
    	return $sum;
    }
 
    public function sub($a, $b) {
    	$sub = $a - $b;
    	return $sub;
    }
}
 
?>

In this class i have written two method, Now i’ll create a phpunit test class to test these methods are working fine or not.

Create MathClassTest.php under tests folder.

tests/MathClassTest.php

<?php
class MathClassTest extends PHPUnit_Framework_TestCase
{
    public function testSum()
    {
        // create class object
        $obj = new MathClass();
 
        // define inputs
        $a = 2; $b = 3;
 
        // real value
        $realValue = $obj->sum($a, $b);
 
        // set expected value
        $expectedVlaue = 5;
 
        // Assert
        $this->assertEquals($expectedVlaue, $realValue);
    }
 
    public function testSub()
    {
        // create class object
        $obj = new MathClass();
 
        // define inputs
        $a = 2; $b = 3;
 
        // real value
        $realValue = $obj->sub($a, $b);
 
        // set expected value
        $expectedVlaue = -2;
 
        // Assert
        $this->assertEquals($expectedVlaue, $realValue);
    }
 
}

In MathClassTest.php i have written two test cases for MathClass.php methods One for sum method and other for sub method.
Where $this->assertEquals(“EXPECTED_VALUE”, “REAL_VALUE”) to check test. And EXPECTED_VALUE should equal REAL_VALUE, If not then test will be fail.

Now time to run your tests.
If you have configured phpunit.phar globally in your system, You may run.

phpunit --bootstrap classes/MathClass.php tests/MathClassTest

If you haven’t configured phpunit.phar globally them please ensure that phpunit.phar file should present on your project directory. After that run below command on terminal.

php phpunit.phar --bootstrap classes/MathClass.php tests/MathClassTest

In below image you can see the test output on my terminal, Where 1st case is success and other one is fail because for passing test $expectedVlaue must equal $realValue
unitestphp

If suppose you have multiple classes to test then you need to create autoload.php file under classes folder it’ll execute all your test cases for all classes in one single command.

classes/autoload.php

<?php
function __autoload($class_name) {
    include $class_name . '.php';
}
?>

After that hit this command in your terminal.

php phpunit.phar --bootstrap classes/autoload.php tests/MathClassTest

Download sample code.

DOWNLOAD

Hope this tutorial will help you to understand phpunit test, It is just a basic tutorial for beginner you can study more advance function from official website https://phpunit.de/manual/current/en/index.html

If you like this post please don’t forget to subscribe My Public Notebook for more useful stuff.

Posted in PHP