Home > Article > Backend Development > PHPUnit installation and usage examples, phpunit installation examples_PHP tutorial
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:
Add the php environment variable to the PATH environment variable under Windows.
Easy to use:
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.
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).
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.