PHPUnit Testing Framework

December 18, 2013
admin

Developers are often seen to use some testing methods on their software development process. Software testing helps them to test the quality of the product or service. It’s very important for minimizing the risk of the broken functionalities on software enhancement or modification. Software engineers generally use white-box testing at the unit level.

There are several testing levels. Unit testing is one of them. It is also known as component testing. It is used to verify the functionality of a specific section of code, generally at the function level.

To ensure the specific function is working as expected is the main purpose of using this test. Only unit testing cannot check the functionality of a piece of software. There might be multiple tests for catching corner cases or other branches in the code of single function. Rather it is used to assure that the building blocks that are used in software work independently of each other.

Which Programming Language supports Unit Testing?

There are a number of programming languages that support unit testing directly. Such as-

  • C#

  • Java

  • Ruby

  • Python

  • Corba

  • ABAP

  • D

  • Obix

PHPUnit

PHPUnit is a unit test framework package for PHP. If we want to write unit test in php we have to integrate PHPUnit testing framework into php because there is no built in testing framework in PHP language.

Behaviour of PHPUnit framework

A framework such as PHPUnit has to resolve a set of constraints, some of which seem always to conflict with each other. Tests have some advantages. Such as:

1.     It’s easy to learn to write.

2.     Tests are easy to write. So developers do not ignore to write them.

3.     Tests code is easy to read.

4.     It’s easy to execute. The tests should run at the touch of a button and present their results in a clear and unambiguous format.

5.     It run fast. So developers can easily run the test hundreds or thousands of times a day.

6.     The tests do not affect each other. If the order in which the tests are run changes, the results of the tests do not change.

How to install PHPUnit?

First open a terminal and update pear (this is the facility that we’ll use to install PHPUnit):

sudo pear config-set auto_discover 1

sudo pear install pear.phpunit.de/PHPUnit

sudo pear install phpunit/DbUnit

How to Use?

From the following table you can see how to write tests using PHPunit that exercise PHP’s array operations. This example introduces the basic conventions and steps for writing tests with PHPUnit:

  1. The tests for a class Class go into a class ClassTest.

  2. Most of the time ClassTest inherits from PHPUnit_Framework_TestCase.

  3. The tests are public methods that are named test*. Otherwise, you can use the @test annotation in a method’s docblock for marking it as a test method.

  4. Inside the test methods, assertion methods such as assertEquals() (see the section called “Assertions”) are used to assert that an actual value matches an expected value.

Our main php coding file Calculator.php:

<?php
class Calculator()
{
public function add($number1, $number2)
{
if(is_numeric($number1) && is_int($number2))
{
$result = $number1 + $number2;
return $result;
}
else
return false;
}
}
?>

This is our simple Calculator class with single function add that will return the summation of two number provided by user. Now can a write test code for this class it will be better if we write our test code into a separate directory for example myapp/unit/testcodefile.php. I have created a unit test file called TestCalculator.php.

<?php
require_once ‘path/to/file/Calculator.php’
class TestCalculator extends PHPUnit_Framework_TestCase
{
public $objCalculator;
public function setUp()
{
$this->objCalculator =  new Calculator();
}
public function testAddFunctionWithValidData()
{
$knownResult = 7;
$expectedResult = $this->objCalculator->add(5, 2);
$this->assertEquals($knownResult, $expectedResult);
}
public function testAddFunctionWithInValidData()
{
$expectedResult = $this->objCalculator->add(‘a’, 2);
$this->assertFalse($expectedResult)
}
}
?>

There are many assertion in php unit some of are:

  • assertCount(0, array(‘foo’))

  • AssertEmpty(array())

  • assertFalse(TRUE)

  • AssertGreaterThanOrEqual(2, 1)

  • assertNull(‘foo’)

  • assertEquals(‘bar’, ‘baz’)

  • AssertSame(‘2204’, 2204)

For running unit test go to terminal then go to your unit test directory myapp/unit/ and then type unittest yourfile.php. Here are the output of our unit test result

PHPUnit 3.7.0 by Sebastian Bergmann.
..
Time: 0 seconds, Memory: 3.50Mb

Tests: 2, Assertions: 2, Failures: 0.

If there are something wrong into your add function then test will be failed and displayed error message for each assertion.

So, this is the way you can use PHPUnit testing for PHP programming language.

Contributor: Mehreen Chowdhury, Marketing Executive, Nascenia

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.