Unit test: phpunit demonstration

Unit test is to make sure a code snippet does what it suppose to do. To do so, a test program is needed to run the code snippet that needs to be tested. In a test driven development, a test program is written before any actual coding. Once the test program is written, the result of the test program will fail if there is no code to test. A developer will then start wrting the minimum code to pass all the failures caught from the test program until there are no more failures. The test program will be run every time a developer adds new code. The benefits of doing unit test are finding bugs early, investing more time early but improving development speed later on, improving code maintainability in the future, etc.

Here is a demonstration of unit testing with phpunit.

Installing phpunit from command line

wget https://phar.phpunit.de/phpunit.phar #download phpunit.phar 
chmod +x phpunit.phar #make phpunit.phar executable
sudo mv phpunit.phar /usr/local/bin/phpunit #move phpunit.phar to a location in your environment path
phpunit --version #check phpunit version

A test program to unit test a calculator program, CalculatorTest.php

<?php
include 'Calculator.php';
class CalculatorTest extends PHPUnit_Framework_TestCase
{
    //The name for the test function has to start with test
    public function testAddition()
    {
        $cal = new Calculator();
        $this->assertEquals( 6, $cal->add(2,4), "2+4 should be 6" );
        // more assrtEquals tests...
    }

    public function testSubstraction()
    {
        $cal = new Calculator();
        $this->assertEquals( 2, $cal->substract(6,4), "6-4 should be 2" );
        // more assrtEquals tests...
    }

    public function testMultiplication()
    {
        $cal = new Calculator();
        $this->assertEquals( 24, $cal->multiply(6,4), "6*4 should be 24" );
        // more assrtEquals tests...
    }

    public function testDivision()
    {
        $cal = new Calculator();
        $this->assertEquals( 3, $cal->divide(6,2), "6-3 should be 3" );
        // more assrtEquals tests...
    }
}

To run the test program above, simply run it with phpunit command.

phpunit CalculatorTest.php 

The calculator class itself, Calculator.php

<?php
class Calculator
{
	public function add($a=0, $b=0)
	{
		return $a + $b;
	}

	public function substract($a=0,$b=0)
	{
		return $a-$b;
	}

	public function multiply($a=1,$b=1)
	{
		return $a*$b;
	}

	public function divide($a=1,$b=1)
	{
		//return $a/$b;
	}
}

A sample of success output of the test program.

PHPUnit 4.2.2 by Sebastian Bergmann.

....

Time: 5 ms, Memory: 4.50Mb

OK (4 tests, 4 assertions)

A sample of one failure output out of 4 tests from the test program.

PHPUnit 4.2.2 by Sebastian Bergmann.

...F

Time: 7 ms, Memory: 4.75Mb

There was 1 failure:

1) CalculatorTest::testDivision
6-3 should be 3
Failed asserting that null matches expected 3.

/Users/pye/dev/test/unittest/CalculatorTest.php:30

FAILURES!
Tests: 4, Assertions: 4, Failures: 1.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search