PHPUnit installation and use example, phpunit installation example _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHPUnit installation and use example, phpunit installation example. PHPUnit installation and use example. phpunit installation example PHPUnit is a test framework officially supported by zend. it guarantees the quality of the project during high-quality unit tests, it can effectively reduce the installation and use cases of B PHPUnit, and phpunit Installation examples.

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:

The 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:

The 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.
 

PHPUnit is a testing framework officially supported by zend. it ensures the foundation of project quality during high-quality unit tests and effectively reduces the number of items...

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.