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.

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 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.


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.


class MathClassTest extends PHPUnit_Framework_TestCase
    public function testSum()
        $obj = new MathClass();
        $a = 2; $b = 3;
        $realValue = $obj->sum($a, $b);
        $expectedVlaue = 5;
        $this->assertEquals($expectedVlaue, $realValue);
    public function testSub()
        $obj = new MathClass();
        $a = 2; $b = 3;
        $realValue = $obj->sub($a, $b);
        $expectedVlaue = -2;
        $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

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.


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.


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

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

Posted in PHP