PHPUnit installation and use example, PHPUnit installation Example _php Tutorial

Source: Internet
Author: User

PHPUnit installation and use example, PHPUnit installation Example


PHPUnit is the Zend officially supported test framework, high-quality unit testing to ensure the quality of the Project Foundation, can effectively reduce the bug, improve the program.

Install PHPUnit:

In the Directory of PHP:

Copy the Code code as follows:
Pear Channel-discover pear;
Pear Install Phpunit/phpunit

Under Windows, add the environment variables of PHP to the PATH environment variable.
Simple to use:

Copy the Code code 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));
}
}

The basic rules of writing phpunit can be seen from the above:
(1) class test written in Classtest
(2) Classtest inheritance Phpunit_framework_testcase
(3) The test method is test* format, or it can be labeled as a test method by @test.
(4) Assert the actual value and the expected value by asserting the method Assertequals.


How to install PHPUnit

PHPUnit can be obtained through PHP extensions and libraries (Peae). Pear is the framework and distribution system of reusable PHP components. Installation PHPUnit can be obtained through 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 is mentioned in the chapter "Using PHPUnit in PHP4" later in this book.

Install only, can find the source code of PHPUnit in the local pear directory, the path is usually/usr/lib/php/phpunit2.

Although using the Pear installer is the only supported installation method for PHPUnit, you can still install it manually. To install manually, refer to the following practices:

1. Download the PHPUnit release package from Pear.php.net/package/phpunit2/download and unzip to make sure the directory is in php.ini defined include_path.

2. Prepare the PHPUnit script

A. Renaming the Pear-phpunit script to PHPUnit

B. Rename all @php_bin@ in the script to the directory where the PHP command line interpreter resides (typically/usr/bin/php).

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

3. Replace all @package_version@ strings in the phpunit2/runner/version.php script with the PHPUnit version (such as 2.3.0) that you installed.

PHPUnit Installation Problem Error

workaround
Based on the above error message, we can know the problem with the PEAR DB Class installation error: Because the PEAR installation directory environment variable PEAR was not changed when the Php_pear_install_dir was reinstalled, Causes the Pear command to not work properly. The
prompt tells us that you can resolve this issue by modifying the value of the PEAR installation environment variable Php_pear_install_dir in Pear.bat.
Right-click Pear.bat, select Edit, and you can open this batch file in Notepad. Locate the
REM Check PEAR global ENV, set them if they do not exist
Modify the following
if "%php_pear_install_dir%" = = "Set" Php_pe Ar_install_dir=e:\phpos\dedeampz\webroot\default\pear "
is
IF"%php_pear_install_dir% "= =" E:\phpos\DedeAMPZ\ Program\php5\pear "SET" Php_pear_install_dir=e:\phpos\dedeampz\webroot\default\pear "
to install environment variables for pear Php_pear_ The value of the Install_dir is overwritten.
At this point, PHP PEAR at the time of installation due to Php_pear_install_dir (installation directory) value is not correct, resulting in the inability to use the PEAR problem is resolved.
 

http://www.bkjia.com/PHPjc/902776.html www.bkjia.com true http://www.bkjia.com/PHPjc/902776.html techarticle PHPUnit Installation and use example, PHPUnit installation Example PHPUnit is the Zend of the official support of the test framework, high-quality unit testing to ensure that the basis of project quality, can effectively reduce the B ...

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