Art AboutFAQWrite for usContact InfoRSS Feed
Search A/R/T:

Introduction to PHPUnit

by Sebastian Bergmann — Page 1 of 3
Click to rate:    
261 votes / avg. rating 11.72%

In the last decade, PHP has developed from a niche language for adding dynamic functionality to small websites to a powerful tool making strong inroads into large-scale, business-critical Web systems. Financial institutions such as banks and insurance companies use PHP, for instance, to develop and maintain solutions for Basel II Credit Rating. Critical business logic like this needs to work correctly. But how do you ensure that it does? You test it, of course.


 

To make code testing viable, good tool support is needed. This is where PHPUnit comes into play. It provides both a framework that makes the writing of tests easy as well as the functionality to easily run the tests and analyse their results.

Writing Tests with PHPUnit

Testing with PHPUnit is not a totally different activity from what you should already be doing. It is just a different way of doing it. The difference is between testing, that is, checking that your program behaves as expected, and running a suite of tests, runnable code-fragments that automatically test the correctness of parts (units) of the software. These runnable code-fragments are called unit tests.

The goal of using automated tests is to make fewer mistakes. While your code will still not be perfect, even with excellent tests, you will likely see a dramatic reduction in defects once you start automating tests. Automated tests give you justified confidence in your code. You can use this confidence to take more daring leaps in design (Refactoring), get along with your teammates better (Cross-Team Tests), improve relations with your customers, and go home every night with proof that the system is better now than it was this morning because of your efforts.

  1. <?php
  2.     class MyClass
  3.     {
  4.         public function doSomething($a, $b)
  5.         {
  6.             return $a + $b;
  7.         }
  8.     }
  9.     ?>
Let us write a test for the doSomething() method in the class above:

  1. <?php
  2. require_once 'PHPUnit/Framework/TestCase.php';
  3. require_once 'MyClass.php';
  4.  
  5. class MyClassTest extends PHPUnit_Framework_TestCase
  6. {
  7.     public function testDoSomething()
  8.     {
  9.         $o = new MyClass;
  10.         $this->assertEquals(2, $o->doSomething(1, 1));
  11.     }
  12. }
  13. ?>
The test case above shows the basic steps for writing tests with PHPUnit:
  • 1. The tests for a class MyClass go into a class MyClassTest.
  • 2. MyClassTest inherits (most of the time) from PHPUnit_Framework_TestCase.
  • 3. The tests are public methods that expect no parameters and are named test*.
  • 4. Inside the test methods assertion methods like assertEquals() are used to assert that an expected value (first argument) to an actual value (second parameter).
There are many more assertion methods that you can use in your tests, see here for a complete list.

 


 Tags:  , , , , , , , , , ,
 Add tag(s) (comma separated):
[Tags beta] — [Add New][Help]  

Tags Help

Tags are keywords associated with a web page that help classifying information. You can find a good explanation here.

To add one or more tags to this page, simply enter them below (separate them with a comma) and hit enter or click on the "Go" button.

New Comment
This form allows you to type in a new comment. Keep in mind the following:
  • The system accepts input in plain text format. Newlines will be converted to the HTML equivalent, and the system will try to catch most URLs and make them clickable.
  • Your e-mail address will never be displayed. We will use it only to notify you when new comments are posted to this page.
  • As a rule, we do not delete comments unless they are offensive, racist, spam or otherwise inappropriate.
  • Bold fields are required
Your Name:
Your e-mail:
Type this number:
Subject:
Comment:
Comments   New Comment
No comments yet. Why not write up one of your own?
Index