PHP Unit Test Overview

Source: Internet
Author: User
Tags manual new features pear php and

Do you encounter the following situations in the process of program development: When you have spent a lot of time developing an application, you think it should be done, unfortunately, when debugging, always constantly find bugs, and the most frightening is that these bugs are recurring, you may find that there are links between these bugs, But the problem is always not found.

When you encounter all of these frustrating situations, you will think that there is a better way to solve it? Of course it is! This is the use of unit tests. Unit test can not only solve the above headache problem to some extent, but also make the code easy to maintain, and can allow you to refactor the code more.

Once you've written a unit test case, when you need to modify your code, all you have to do is rerun your unit test case and see if the unit test case passes, and if it does, the proof code is OK.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

People tend to say, "Since unit testing is so good, why are so many people still reluctant to write unit tests?" There are several false interpretations on the following:

1. Consider it a waste of time to write unit tests. Although many IDE tools now have a framework for writing unit tests, developers are asked to write code for unit tests. As with many of the best practices in development, doing the right thing in the right way can save a lot of time for development. Every time you add new features, you may be able to click on manual tests by visiting your Web pages, while running the built unit test cases faster than testing them manually.

2, that since the code can run, do not need to write unit tests. But assuming there are new members in the team, if there are no good unit test cases, the new members are likely to arbitrarily code without considering the consequences. If you have well-written unit tests, you can minimize bugs by performing various tests while the program is running.

3, think that writing unit test code is boring. The programmer's instinct is to solve the problem, and many programmers think it would be tedious to write unit test code when it comes to tight coding. But you know, if you can write unit tests at a very early stage to find as many errors in the code as possible, then save time and reduce errors, why not?

Start to install PHPUnit

In this article, we will introduce the Unit Test tool PHPUnit (http://phpunit.de/) in PHP and explain how to use phpunit in practical work through practical examples. The first way to install PHPUnit is to install it through the pear under PHP:

Pear channel-discover pear.phpunit.de pear  
channel-discover components.ez.no pear  
channel-discover pear.symfony-project.com  
  Pear Install Phpunit/phpunit

If you want to install manually, refer to the PHPUnit Manual for installation (http://www.phpunit.de/manual/3.0/en/installation.html).

Writing the first unit test case

Here we start writing the first unit test case. When writing test cases, follow the following phpunit rules:

1 generally, in test cases, you can extend the Phpunit_framework_testcase class so that you can use such methods as Setup (), teardown ().

2 The name of a test case is best used in a conventional format, that is, after the test class is added "test", such as the class to be tested is remoteconnect, then the test case is named Remoteconnecttest.

3 All test methods in a test case should be named with the test+ test method name, such as Testdoeslikewaffles (), and note that the method must be declared to be of public type. Of course you can include private methods in your test cases, but they cannot be invoked by PHPUnit.

4 The test method cannot receive parameters.

Let's start with a simple example, the code is as follows:

<?php  
class Remoteconnect  
{public  
  function connecttoserver ($serverName =null)  
  {  
    if ($ Servername==null) {  
      throw new Exception ("That's not a server name!");  
    }  
    $fp = Fsockopen ($serverName,);  
    Return ($FP)? True:false;  
  }  
  Public Function Returnsampleobject ()  
  {return  
    $this  
  }  
}  
? >

The above code is actually the function of connecting to a specified server, then we can write the test code as follows:

<?php  
require_once (' remoteconnect.php ');  
Class Remoteconnecttest extends Phpunit_framework_testcase  
{public  
  function setUp () {}  
  Public function teardown () {} public  
  function Testconnectionisvalid ()  
  {  
   //test to ensure, the object from Fsockopen is valid  
   $connObj = new Remoteconnect ();  
    $serverName = ' www.google.com ';  
    $this->asserttrue ($connObj->connecttoserver ($serverName)!== false);  
 }  
? >

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.