PHPUnit installation and usage examples, phpunit installation examples_PHP tutorial

WBOY
Release: 2016-07-13 10:15:43
Original
955 people have browsed it

PHPUnit installation and usage examples, phpunit installation examples

PHPUnit is a testing framework officially supported by zend. High-quality unit testing is the basis for ensuring project quality and can effectively reduce BUGs and improve programs.

Install PHPUnit:

In the php directory:

Copy code The code is as follows:

pear channel-discover pear;
pear install phpunit/PHPUnit

Add the php environment variable to the PATH environment variable under Windows.
Easy to use:

Copy code The code is as follows:

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 you can see the basic rules for writing PHPUnit:
(1) The test of class Class is written in ClassTest
(2) ClassTest inherits PHPUnit_Framework_TestCase
(3) Test methods are all in test* format, and can also be marked as test methods through @test.
(4) Assert the actual value and expected value through the assertion method assertEquals.

How to install PHPUnit

PHPUnit is available through the PHP Extensions and Libraries (PEAE). PEAR is a framework and distribution system for reusable PHP components. Installing PHPUnit can be obtained through the PEAR installer command:

pear install PHPUnit2

According to PEAR's version naming standard, the PHPUnit package for PHP5 is called PHPUnit2. PHPUnit is a package suitable for PHP4, which will be mentioned in the chapter "Using PHPUnit in PHP4" later in this book.

Only for installation, 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 manually, follow the steps below:

1. Download the PHPUnit release package from pear.php.net/package/PHPUnit2/download, then unzip it and make sure the directory is in the include_path defined in php.ini.

2. Prepare the phpunit script

a. Rename the pear-phpunit script to phpunit

b. Rename all @php_bin@ in the script to PHP command line interpreter The directory where it is located (usually /usr/bin/php).

c. Copy this script to a directory included in the 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 the PHPUnit version you installed (such as 2.3.0).

phpunit installation problem reporting error

Solution
Based on the above error message, we can know the problem with the PEAR DB class installation error: it is because the PEAR installation directory environment variable PHP_PEAR_INSTALL_DIR was not changed when reinstalling PEAR, causing the PEAR command to not work properly.
The prompt tells us that this problem can be solved by modifying the value of the PEAR installation environment variable PHP_PEAR_INSTALL_DIR in pear.bat.
Right-click pear.bat and select Edit to open this batch file in Notepad mode. 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\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”
You can overwrite the value of the PEAR installation environment variable PHP_PEAR_INSTALL_DIR.
At this point, the problem that PHP PEAR cannot be used due to the incorrect PHP_PEAR_INSTALL_DIR (installation directory) value during installation has been solved.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/902776.htmlTechArticlePHPUnit installation and usage examples, phpunit installation examples PHPUnit is a test framework strongly supported by zend, high-quality unit testing It is the basis for ensuring project quality and can effectively reduce B...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template