PHPUnit installation and use example, phpunit installation example

Source: Internet
Author: User

PHPUnit installation and use example, phpunit installation example

PHPUnit is a testing framework officially supported by zend. It ensures the foundation of project quality in high-quality unit tests, effectively reduces bugs and improves programs.

Install PHPUnit:

Under the php Directory:

Copy codeThe Code is as follows:
Pear channel-discover pear;
Pear install phpunit/PHPUnit

Add the php environment variable to the PATH environment variable in windows.
Easy to use:

Copy codeThe Code is as follows:
<? Php
Class StackTest extends PHPUnit_Framework_TestCase
{
 
Public function testArray ()
{
$ Stack = array ();
$ This-> assertEquals (0, count ($ stack ));
 
Array_push ($ stack, 'foo ');
$ This-> assertEquals ('foo', $ stack [count ($ stack)-1]);
$ This-> assertEquals (1, count ($ stack ));
 
$ This-> assertEquals ('foo', array_pop ($ stack ));
$ This-> assertEquals (0, count ($ stack ));
}

/**
* @ Test
*/
Public function Stringlen ()
{
$ Str = 'abc ';
$ This-> assertEquals (3, strlen ($ str ));
}
}

From the above, we can see the basic rules for compiling PHPUnit:
(1) Test the Class in ClassTest
(2) ClassTest inherits from PHPUnit_Framework_TestCase
(3) The test method is in the test * format. You can use @ test to mark it as a test method.
(4) assertEquals can be used to assert the actual and expected values.


How to install PHPUnit

PHPUnit can be obtained through PHP extensions and Libraries (PEAE. PEAR is a framework and Distribution System for reusable PHP components. To install PHPUnit, run the PEAR installer command:

Pear install PHPUnit2

According to the PEAR version Naming Standard, the PHPUnit package for PHP5 is called PHPUnit2. PHPUnit is a package for PHP4, which will be mentioned later in the chapter "using PHPUnit in PHP4.

Only install PHPUnit. The source code of PHPUnit can be found in the local PEAR directory. The path is usually/usr/lib/php/PHPUnit2.

Although using the PEAR installer is the only installation method supported by PHPUnit, you can still install it manually. To install it manually, follow these steps:

1. Slave.

2. Prepare the phpunit script

A. Rename the pear-phpunit script to phpunit.

B. Rename all @ php_bin @ in the script to the directory where the PHP Command Line interpreter is located (usually/usr/bin/php ).

C. Copy the script to the directory contained in a PATH environment variable and change the file attribute to executable (chmod + x phpunit ).

3. replace all @ package_version @ strings in the PHPUnit2/Runner/Version. php script with your installed PHPUnit Version (for example, 2.3.0 ).

Phpunit Installation Error

Solution
Based on the above error information, we can see the pear db class installation error: Because the installation directory environment variable PHP_PEAR_INSTALL_DIR of PEAR is not changed during re-installation of PEAR, the PEAR command cannot be used normally.
The prompt tells us that we can solve this problem by modifying the value of the pear installation environment variable PHP_PEAR_INSTALL_DIR in PEAR. bat.
Right-click pear. bat and select Edit. You can open this batch of files in notepad. Find
REM Check PEAR global ENV, set them if they do not exist
Modify the following
IF "% PHP_PEAR_INSTALL_DIR %" = "" SET "PHP_PEAR_INSTALL_DIR = E: \ phpos \ deampz \ WebRoot \ Default \ pear"
Is
IF "% PHP_PEAR_INSTALL_DIR %" = "E: \ phpos \ DedeAMPZ \ Program \ PHP5 \ pear" SET "PHP_PEAR_INSTALL_DIR = E: \ phpos \ deampz \ WebRoot \ Default \ pear"
You can overwrite the value of the PEAR installation environment variable PHP_PEAR_INSTALL_DIR.
At this point, the php pear cannot be used because of the incorrect PHP_PEAR_INSTALL_DIR (installation directory) value during installation.
 

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.