Use PHPUnit to help you debug your PHP program

Source: Internet
Author: User
Tags assert php source code variable
Program Debugging program is a long process, the longer the complexity of the program, debugging is increasingly difficult. If you are debugging a PHP program, then you may want to use phpunit, it can greatly speed up your debugging speed.
What is PHPUnit
Phpunit was born out of the famous JUnit test framework written by Fred Yankowski. You can download the latest version to its website http://www.ontosys.com/phiki/phpunit. You can use PHPUnit to write a set of test packages. Make sure your program code is correct. All tests can be done automatically in one step.
If you monitor a bug, you can write a short piece of test code to find out where the error is. If you have the same bug again in the future, just run your previous test kit and you can catch it immediately. Running a test pack frequently can ensure the robustness of your program's code.
Start
Suppose we have a bank account handler. Now you need to write a test package for the account class.
The following is the account class source code:
<?php
 
Class account{
var $balance;
function account ($initialBalance =0) {
$this->balance = $initialBalance;
}
function Withdraw ($amount) {
$this->balance-= $amount;
}
Function Deposit ($amount) {
$this->balance + + $amount;
}
function GetBalance () {
return $this->balance;
}
Function Transferfrom (& $sourceAccount, $amount) {
$sourceAccount->withdraw ($amount);
$this->deposit ($amount);
}
?>
 
Create a test class
 
First, we build a test class accounttest, which is a subclass of the testcase provided by PHPUnit. There are 2 basic methods in this TestCase class: Setup and teardown. The implementation of these 2 methods is an empty procedure in the parent class and must be overloaded by ourselves. Where Setup is used for initialization of the Accounttest class. In this case, we initialize some of the accounts that were used in the test. Teardown is used for emptying the Accounttest class and is not used in this example. Therefore, it is not overloaded. The source code for this Accounttester class is as follows:
 
<?php
 
Class Accounttester extends testcase{
var $_ac1;
var $_ac2;
var $_ac3;
var $_ac4;
 
function Accounttester ($name) {
$this->testcase ($name); Call Parent Constructor
}
function SetUp () {
$this-&GT;_AC1 = new account (100); Data for Testwithdraw
$this-&GT;_AC2 = new account (20); Data for Testdeposit
$this-&GT;_AC3 = new account (30); Data for Testtransferfrom
$this-&GT;_AC4 = new account (50);
}
}
?>
 
Add a special test code
Now we can add the test code to the Accounttester class.
 
<?php
 
Make a withdrawal the units from _AC1.
_ac1 ' s initial balance is 100
 
function Testwithdraw () {
$this->_ac1->withdraw (25);
$this->assert ($this->_ac1->getbalance () = 75); 100-25 = 75
}
 
Make a deposit of units into _AC2.
_ac1 ' s initial balance is 20
 
function Testdeposit () {
$this->_ac2->deposit (10);
$this->assertequals ($this->_ac2->getbalance ()); 20 +10 = 30
}
  
Tranfers units from _AC3 to _AC4
_ac3 ' s initial balance is 30
_ac4 ' s initial balance is 50
 
function Testtransferfrom () {
$this->_ac4->transferfrom (& $this->_ac3,10);
$this->assertequals ($this->_ac3->getbalance (), "Source account balance incorrect"); 30-10 = 20
$this->assertequals ($this->_ac4->getbalance (), "Target account balance incorrect"); 50 + 10 = 60
}
 
?>
 
In this code, assert (as in C's assertion) method is a key part of the test. If the conditional expression in assert is true, then the test passes. Otherwise, an error is returned. Because assert methods are mostly used to determine whether the values of two variables are equal. Therefore, the TestClass class introduces the Assertequals method to implement this feature specifically. There are 3 parameters in the Assertequals method: expected value, test values, and message hint strings that are returned when the two values are not equal.
Run the test process
OK, now it's time to run the test program that we've prepared. We also have to set up a runtest.php test program to run all the test procedures.
 
Runtest.php source code is as follows:
 
<?php
$tSuite = new TestSuite (); Creation of the test suite object to create testing suite objects
$tSuite->addtest (New Accounttester ("Testwithdraw")); ADD inidividual Tests
$tSuite->addtest (New Accounttester ("Testdeposit")); Add a special test method.
$tSuite->addtest (New Accounttester ("Testtransferfrom"));
$res = new Texttestresult (); Creation of the result set up a test results class
$tSuite->run (& $res); Run the test runs tests
$res->report (); Print results output test results.
?>
 
Program Description:
You first create a test suite object Tsuite, and then add a specific test method, the Addtest method's parameter is the test method's again Create test Report object, and then run the test. The test found that the results of the error were captured by the TestResult class, and TestResult could customize a set of text/html error reports. You can also write the output section yourself if necessary. The test results are encapsulated in the TestResult class in order to output the test results. We have adopted another class Texttestresult class provided by PHPUnit, which can output reports in text or hypertext format. Of course we can also customize a new TestResult subclass to control more complex output formats.
 
Tips and Tricks
1. After writing a new test suite, we can first introduce a small bug to prove that the test suite works.
For example, in this example account class, we deliberately introduce a problematic function.
<?php
function Withdraw ($amount) {
$this->balance-= $Amount;
Variable name case error, the intention is to invoke the $amount parameter, the result of introducing a new variable $amount.
}
?>
OK, now let's run the test suite, and if it's not an accident, we'll soon find out where the bugs are.
2. It should be noted that not all methods require testing. You only need to test the relevant methods.
3. Writing test code before you start coding will give you a better idea of what your program needs to accomplish.
Now, by introducing the PHPUnit test suite class, you can find that the time to find bugs has been shortened and your productivity as a programmer has improved.
So, enjoy the fun of catching bugs. Good appetite.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.