Archive for February 14th, 2011

Author:
Monday, February 14th, 2011
Software Testing is a process of verifying and validating whether the software developed is working as expected. It is a process of analyzing software to find the bugs.
Testing is the most important phase in software development. If you don’t test your source code then your are not verifying that your software worked as expected.
There are various types of software testing strategies such as white box testing strategy, black box testing strategy etc. In this article I am going to explain the most important white box testing strategy called “UNIT TESTING”. PHP will be the programming language used.
Unit Testing means testing of individual components and module. This is typically done by a developer because it requires knowledge of program design and code.
Few developers or software owners have misconception about unit testing that it is time consuming and unnecessary waste of time. Even I felt the same way initially, but later I figured out that it’s worth unit testing your code because it reduces your time in future, modifying and maintaining undetected code or bug. Imagine a complex project which has many modules. Now if you have unit tested all these modules then introducing a new module into the system will be easier because the integration issues can be detected immediately. But if you have not unit tested your code then it is harder to introduce a new module because it might have the impact on the other parts of the system. But this doesn’t mean that you can’t test the legacy code. I will be writing on how to handle the legacy code in my future article.
Unit testing is definitely is one of the means of making developers life easier.
There are many frameworks available for unit testing your code using PHP. Once such framework in PHP is “PHPUNIT”.
PHPUNIT is derived from the popular java unit testing framework called JUnit.
I have researched on PHPUNIT and I feel that there is not enough documentation on installing and using it. It is time consuming for a developer initially because of the lack of documentation. This is my personal experience.

Software Testing is a process of verifying and validating whether the software developed is working as expected. It is a process of analyzing software to find the bugs.

Testing is the most important phase in software development. If you don’t test your source code then your are not verifying that your software worked as expected.

There are various types of software testing strategies such as white box testing strategy, black box testing strategy etc. In this article I am going to explain the most important white box testing strategy called “UNIT TESTING”. PHP will be the programming language used.

Unit Testing means testing of individual components and module. This is typically done by a developer because it requires knowledge of program design and code.

Few developers or software owners have misconception about unit testing that it is time consuming and unnecessary waste of time. Even I felt the same way initially, but later I figured out that it’s worth unit testing your code because it reduces your time in future, modifying and maintaining undetected code or bug. Imagine a complex project which has many modules. Now if you have unit tested all these modules then introducing a new module into the system will be easier because the integration issues can be detected immediately. But if you have not unit tested your code then it is harder to introduce a new module because it might have the impact on the other parts of the system. But this doesn’t mean that you can’t test the legacy code. I will be writing on how to handle the legacy code in my future article.

Unit testing is definitely is one of the means of making developers life easier.

There are many frameworks available for unit testing your code using PHP. Once such framework in PHP is “PHPUNIT”.

PHPUNIT is derived from the popular java unit testing framework called JUnit.

I have researched on PHPUNIT and I feel that there is not enough documentation on installing and using it. It is time consuming for a developer initially because of the lack of documentation. This is my personal experience.Hopefully this article will help you get started with PHPUnit.

The best and easiest way to install PHPUNIT is using the pear installer
Steps to install PHPUnit on windows.
1 ) On ‘C’ drive create a folder named ‘php5’
2 )Then on command prompt type:
pear upgrade pear
2 ) Once PEAR  is updated, on command prompt type:
pear channel-discover components.ez.no
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com
4 )Finally type:
pear install –alldeps   pear/phpunit2
This will install phpunit with all the required dependencies.
You might face couple of issues while installing this.  In that case please write to me and I shall be more than happy to help you.
So those were the steps to install PHPUNIT on windows. I have not tried this on the linux , but I believe the command ‘s will be similar. I will publish if I happen to install PHPUNIT on Linux.
Now let me start writing few basic test cases to show how PHPUNIT works.
The following is the basic example testing the size of the array
<?php
require_once ‘PHPUnit2/Framework/TestCase.php’;
class ArrayTest extends PHPUnit2_Framework_TestCase {
public function testNewArrayIsEmpty() {
// Create the Array fixture.
$fixture = Array();
// Assert that the size of the Array fixture is 0.
$this->assertEquals(0, sizeof($fixture));
}
public function testArrayContainsAnElement() {
// Create the Array fixture.
$fixture = Array();
// Add an element to the Array fixture.
$fixture[] = ‘Element’;
// Assert that the size of the Array fixture is 1.
$this->assertEquals(1, sizeof($fixture));
}
}
?>
The test for a class named  “Xyz” go into a class “XyzTest”
“XyzTest” is inherited from the base class  PHPUnit2_Framework_TestCase.
All the tests are public methods and it expects no parameters.
It’s a good practice to give the test method name as what we are testing in that method. In the above example I am testing if the array is empty, so my method name would ideally be testNewArrayIsEmpty.
Inside the test, assertion methods such as assertEquals() assert that an actual value matches an expected value.
Now to run this test you need to first get into the folder where you save this file .
After that use the following command
C:\wamp\bin\php\php5.3.0> phpunit ArrayTest
Here ArrayTest is the name of the Php file(ArrayTest.php)
After you run this you get the following screen
PHPUnit 2.3.0 by Sebastian Bergmann.
..
Time: 0.067288
OK (2 tests)
For  each test cases when you run them ,phpunit  command line print characters to display status the status .
. This is Printed when the test succeeds.
F This is Printed when an assertion fails while running the test method.
E This is Printed when an error occurs while running the test method.
I This Printed when the test is marked as being incomplete or not yet implemented.
There is a difference between Failure and Error . Failure is basically violated assertion and error is unexpected exception of PHP error.
Now lets make this test fail . So what I will do is I will insert an element to an array and try to assert  it to 0 instead of 1.
So my test case would be
public function testArrayContainsAnElement() {
// Create the Array fixture.
$fixture = Array();
// Add an element to the Array fixture.
$fixture[] = ‘Element’;
// Assert that the size of the Array fixture is 1.
$this->assertEquals(0, sizeof($fixture));
}
C:\wamp\bin\php\php5.3.0> phpunit ArrayTest
PHPUnit 2.3.0 by Sebastian Bergmann.
.F
Time: 0.001147
There was 1 failure:
1) testArrayContainsAnElement(ArrayTest)
expected same: <0> was not: <1>
/home/siyam/ArrayTest.php:21
FAILURES!!!
Tests run: 2, Failures: 1, Errors: 0, Incomplete Tests: 0.
As you can see that my test is complete but I have the .F  symbol . That means my assertion is failed.
So that was the simple test cases using PHPUNIT . As we see that we have a array called fixture which is used in both the test methods. So we can eliminate this code duplication  in our test . PHPunit invokes a test method before it runs any test . That method is setup(). So we can use this method to create objects against which we test .Once the test method finished running irrespective of test method failing or passing PHPUnit invokes another template method called tearDown(). In this method you can clean the objects that you have used.
So using the template method we can refactor  our test cases as follows
<?php
require_once ‘PHPUnit2/Framework/TestCase.php’;
class ArrayTest extends PHPUnit2_Framework_TestCase {
protected $fixture;
protected function setUp() {
// Create the Array fixture.
$this->fixture = Array();
}
public function testNewArrayIsEmpty() {
// Assert that the size of the Array fixture is 0.
$this->assertEquals(0, sizeof($this->fixture));
}
public function testArrayContainsAnElement() {
// Add an element to the Array fixture.
$this->fixture[] = ‘Element’;
// Assert that the size of the Array fixture is 1.
$this->assertEquals(1, sizeof($this->fixture));
}
}
?>
So that’s it for this article. I hope this article at least gets you started. I will write more about unit testing in my future articles. Its wide topic and I can cover a lot.
Happy Coding 

The best and easiest way to install PHPUNIT is using the pear installer

Steps to install PHPUnit on windows.

1 ) On ‘C’ drive create a folder named ‘php5’

2 )Then on command prompt type:

pear upgrade pear

2 ) Once PEAR  is updated, on command prompt type:

pear channel-discover components.ez.no

pear channel-discover pear.phpunit.de

pear channel-discover pear.symfony-project.com

4 )Finally type:

pear install –alldeps   pear/phpunit2

This will install phpunit with all the required dependencies.

You might face couple of issues while installing this.  In that case please write to me and I shall be more than happy to help you.

So those were the steps to install PHPUNIT on windows. I have not tried this on the linux , but I believe the command ‘s will be similar. I will publish if I happen to install PHPUNIT on Linux.

Now let me start writing few basic test cases to show how PHPUNIT works.

The following is the basic example testing the size of the array

<?php

require_once ‘PHPUnit2/Framework/TestCase.php’;

class ArrayTest extends PHPUnit2_Framework_TestCase {

public function testNewArrayIsEmpty() {

// Create the Array fixture.

$fixture = Array();

// Assert that the size of the Array fixture is 0.

$this->assertEquals(0, sizeof($fixture));

}

public function testArrayContainsAnElement() {

// Create the Array fixture.

$fixture = Array();

// Add an element to the Array fixture.

$fixture[] = ‘Element’;

// Assert that the size of the Array fixture is 1.

$this->assertEquals(1, sizeof($fixture));

}

}

?>

The test for a class named  “Xyz” go into a class “XyzTest

“XyzTest” is inherited from the base class  PHPUnit2_Framework_TestCase.

All the tests are public methods and it expects no parameters.

It’s a good practice to give the test method name as what we are testing in that method. In the above example I am testing if the array is empty, so my method name would ideally be testNewArrayIsEmpty.

Inside the test, assertion methods such as assertEquals() assert that an actual value matches an expected value.

Now to run this test you need to first get into the folder where you save this file .

After that use the following command

C:\wamp\bin\php\php5.3.0> phpunit ArrayTest

Here ArrayTest is the name of the Php file(ArrayTest.php)

After you run this you get the following screen

PHPUnit 2.3.0 by Sebastian Bergmann.

..

Time: 0.067288

OK (2 tests)

For  each test cases when you run them ,phpunit  command line print characters to display status the status .

. This is Printed when the test succeeds.

F This is Printed when an assertion fails while running the test method.

E This is Printed when an error occurs while running the test method.

I This Printed when the test is marked as being incomplete or not yet implemented.

There is a difference between Failure and Error . Failure is basically violated assertion and error is unexpected exception of PHP error.

Now lets make this test fail . So what I will do is I will insert an element to an array and try to assert  it to 0 instead of 1.

So my test case would be

public function testArrayContainsAnElement() {

// Create the Array fixture.

$fixture = Array();

// Add an element to the Array fixture.

$fixture[] = ‘Element’;

// Assert that the size of the Array fixture is 1.

$this->assertEquals(0, sizeof($fixture));

}

C:\wamp\bin\php\php5.3.0> phpunit ArrayTest

PHPUnit 2.3.0 by Sebastian Bergmann.

.F

Time: 0.001147

There was 1 failure:

1) testArrayContainsAnElement(ArrayTest)

expected same: <0> was not: <1>

/home/siyam/ArrayTest.php:21

FAILURES!!!

Tests run: 2, Failures: 1, Errors: 0, Incomplete Tests: 0.
As you can see that my test is complete but I have the .F  symbol . That means my assertion is failed.

So that was the simple test cases using PHPUNIT . As we see that we have a array called fixture which is used in both the test methods. So we can eliminate this code duplication  in our test . PHPunit invokes a test method before it runs any test . That method is setup(). So we can use this method to create objects against which we test .Once the test method finished running irrespective of test method failing or passing PHPUnit invokes another template method called tearDown(). In this method you can clean the objects that you have used.

So using the template method we can refactor  our test cases as follows

<?php

require_once ‘PHPUnit2/Framework/TestCase.php’;

class ArrayTest extends PHPUnit2_Framework_TestCase {

protected $fixture;

protected function setUp() {

// Create the Array fixture.

$this->fixture = Array();

}

public function testNewArrayIsEmpty() {

// Assert that the size of the Array fixture is 0.

$this->assertEquals(0, sizeof($this->fixture));

}

public function testArrayContainsAnElement() {

// Add an element to the Array fixture.

$this->fixture[] = ‘Element’;

// Assert that the size of the Array fixture is 1.

$this->assertEquals(1, sizeof($this->fixture));

}

}

?>

So that’s it for this article. I hope this article at least gets you started. I will write more about unit testing in my future articles. Its wide topic and I can cover a lot.

Happy Coding .

Category: PHP, Testing/QA  | Leave a Comment