Sunday, October 28, 2012

Test Protected and Private functions in php

Testing private and protected functions in php is normally done through testing the public function it calls. But sometimes you need to test the private or protected functions separately. How can we do this? Lets consider the following simple example.

When you try to run the test class you will get the following error. PHP Fatal error:  Call to private/protected method MyClass::myProtectedMethod() from context 'MyTestClass' After version 5.3.2 PHP's "Reflection Class" makes it possible to temporarily work around access modifiers. This is done using the setAccessible function. Then you need to change your test class as follows.
You will be able to run the test case for your private/protected function now.

Friday, June 1, 2012

Automated Testing - GSoC 2012


For Google Summer of Code (GSoC) 2012 I submitted a proposal on "AutomatedTesting" project listed by phpmyadmin (PMA). Michal Čihař, Marc Delisle, Madhura Jayaratne and Stas Zarubin who is the last year GSoC contributor helped me a lot when I improve my patches and the proposal. Michal Čihař act as my mentor for my project in this summer of code.

PMA is a free software tool written in PHP, intended to handle the administration of MySQL over the web. Automated testing is a project idea which continues from the last year GSoC session which was partially done by Stas Zarubin. Expectation of the project is automate the testing in PMA by writing uni test cases and selenium test cases using phpunit and improve the code coverage. Last year Stas Zarubin has written some test cases for the PMA libraries and classes but couldn't complete all. In this time I am improving the written test cases coverage and write new test cases for the remaining libraries and classes. 

In the next series of blog post I will discuss more about my project in this summer.

Thursday, April 5, 2012

Write Your First Unit Test Using PHPUnit

Hi All. We will discuss how to write your first unit test case using PHPUnit. If you are not configured your testing environment yet, refer my first post

Unit testing is a method of testing that verifies the individual units of source code are working properly. Normally we are considering a function (method) as our unit of source code.

Assume we are writing a function to add two numbers in class operation.

Copy the above code into a empty text file and name it as operation.php. Now we need to write a test case for function add(); Function add() accepting two parameters and returning the addition of those. So what we have to test is passing two values to the add method and check whether we are getting the expected output.

Create the operationTest.php file and copy the above code into it. The class should extend the PHPUnit_Framework_TestCase. In function testAdd(), it passes two values for the add method and getting the return value. As we passes 2 and 3 the return valued should be 5. We can verify it using assertions. We used assertEquals() to check the return value and the expected values are equals. If it equals the test will pass. 

Running the Unit Test

Place the above two file in a same folder and run the following command.
$ phpunit operationTest.php

You will get the following.

PHPUnit 3.6.10 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 3.50Mb
OK (1 test, 1 assertion)

For each test run, the PHPUnit command-line tool prints one character to indicate progress.
.         Printed when the test succeeds.
F        Printed when an assertion fails while running the test method.
E        printed when an error occurs while running the test method.
S        Printed when the test has been skipped.
I         Printed when the test is marked as being incomplete or not yet implemented.
              
Change the expected value into some other value and run the test. You will have test failure. 

PHPUnit 3.6.10 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 3.50Mb
There was 1 failure:
1) operationTest::testAdd
Failed asserting that 2 matches expected 5.
/var/www/codemix/operationTest.php:15
FAILURES!
Change the method in to some other name and run the test. You will have fetal error.
PHPUnit 3.6.10 by Sebastian Bergmann.
PHP Fatal error:  Call to undefined method operation::sum() in
/var/www/codemix/operationTest.php on line 14.   .............

We can write multiple tests for one method. Add following test method to operationTest.php to test addition of decimals values. 

Testing Negative Scenario

In good test cases there should be test cases for testing the success scenarios as well as negative scenarios. Add following test cases to operationTest.php. It checks the return value is not equal to a invalid output. 


Keep writing test cases and get them passed. Try following assertion methods. 

assertTrue($x)Fail if $x is false
assertFalse($x)Fail if $x is true
assertNull($x)Fail if $x is set
assertNotNull($x)Fail if $x not set
assertIsA($x, $t)Fail if $x is not the class or type $t
assertNotA($x, $t)Fail if $x is of the class or type $t
assertEqual($x, $y)Fail if $x == $y is false
assertNotEqual($x, $y)Fail if $x == $y is true
assertWithinMargin($x, $y, $m)Fail if abs($x - $y) < $m is false
assertOutsideMargin($x, $y, $m)Fail if abs($x - $y) < $m is true
assertIdentical($x, $y)Fail if $x == $y is false or a type mismatch
assertNotIdentical($x, $y)Fail if $x == $y is true and types match
assertReference($x, $y)Fail unless $x and $y are the same variable
assertClone($x, $y)Fail unless $x and $y are identical copies
assertPattern($p, $x)Fail unless the regex $p matches $x
assertNoPattern($p, $x)Fail if the regex $p matches $x
expectError($x)Fail if matching error does not occur
expectException($x)Fail if matching exception is not thrown
ignoreException($x)Swallows any upcoming matching exception
assert($e)Fail on failed expectation object $e

We will look in to more advanced techniques of unit testing. Write a method, write a test case for that and enjoying by get passing it. :)

Friday, March 30, 2012

Unit Testing with PHPUnit

Hi All. Here I am going to discuss how to write unit tests using PHPUnit. In this post I am listing down the necessary steps  to configure your testing environment. PHPUnit is unit testing software frame work for php programming language developed by Sebastian Bergmann. It is one of the xUnit family of framework.

Set up the environment

PHPUnit should be installed using the PEAR Installer.

Upgrading PEAR


If you have version less than 1.9 it is recommended to update the PEAR channel and upgrade the PEAR.

Installing PHP Extensions

PHPUnit depends on package pear/XML_RPC2 that requires curl.


Need to install Xdebug to run the tests and generate code coverage.

Installing PHPUnit

Installing PHPUnit with all dependencies.

Installing Selenium

Selenium RC integration for PHPUnit.

You need to download Selenium Server from the http://seleniumhq.org/download/
You can download the SeleniumIDE which is Firefox plugin that does record-and-playback of interactions with the browser.

In the next post we will discuss how to write your first test case using PHPUnit. Keep your testing environment configured. :)