PHPUnit is used by PHP programmers to write unit test code, just as JUnit is used in Java. As the superiors asked employees to write unit test code when writing code, I collected relevant information online and sorted out a set of PHPUnit installation methods that suit the virtual machine environment.
Requirements:
System: centos5.4
PHP version: php5.3.
1. Install Pear
First, install the latest pear. The installation command is as follows:
- Wgethttp: // pear.php.net/go-pear.phar
- /Usr/local/php5.3/bin/phpgo-pear.phar
wget http://pear.php.net/go-pear.phar /usr/local/php5.3/bin/php go-pear.phar
Pear is successfully installed! If the preceding command fails to be executed:
1. Use find/-namephp to find the location of the php executable file;
2. Try to add sudo to the command or use suroot to switch to the root user.
Ii. Install phpunit
The following uses pear to install phpunit. The following installation process may prompt that the pear is not found because the pear is not added to the environment variable. My solution is:
1. Use find/-namepear to locate the pear location
2. Enter this statement exportPATH = "$ PATH:/usr/local/php5.3/bin/pear"
- Pearchannel-discoverpear.phpunit.de
- Pearchannel-discovercomponents.ez.no
- Pearchannel-discoverpear.symfony-project.com
- Pearupgrade-all
- Pearinstallphpunit/PHPUnit
pear channel-discover pear.phpunit.depear channel-discover components.ez.nopear channel-discover pear.symfony-project.compear upgrade-allpear install phpunit/PHPUnit
The dependency package may appear during the installation process. Follow the prompts to install the package.
For example, yuminstallphp-dom-y.
Congratulations! If everything goes well, you have successfully installed PHPUnit.
3. Use PHPUnit
Example: hello. php
<? Php
- Classhello_testextendsPHPUnit_Framework_TestCase
- {
- Publicfunctiontest_equal ()
- {
- $ Stack = array ();
- $ This-> assertEquals (0,123); // judge whether 0 and 123 are equal
- }
- }
- ?>
<? Phpclass hello_test extends PHPUnit_Framework_TestCase {public function test_equal () {$ stack = array (); $ this-> assertEquals (0,123); // judge whether 0 and 123 are equal}?>
Phpunithello. php, if OK is displayed, it indicates that the operation is successful. Otherwise ......
However, phpunit is not found in the error message. If it is not a path problem, find the following solution:
- Pearupdate-channels
- Pearupgrade-all
pear update-channels pear upgrade-all
After installation:
Pearinstall-alldepsphpunit/PHPUnit
pear install –alldeps phpunit/PHPUnit
An error is returned:
Unknownremotechannel: pear.symfony.com
Phpunit/PHPUnitrequirespackage "channel: // pear.symfony.com/Yaml" (version> = 2.1.0)
Google to find a useful solution:
- 1pearchannel-discoverpear.symfony.com
- 2pearinstallpear.symfony.com/Yaml
1 pear channel-discover pear.symfony.com2 pear install pear.symfony.com/Yaml
Then execute again
Pearinstall-alldepsphpunit/PHPUnit
pear install –alldeps phpunit/PHPUnit
This is successful.
Iv. Summary
Here I will share with you the process from failure to success when I install PHPUnit on my own virtual machine. Failure is mainly because the executable file cannot be found or some dependent libraries are missing. Failure may also result in high or low compatibility between the tool and the system version.
In the above example, assertEquals with equal asserted values are used. Of course, PHPUnit also comes with many other asserted methods, such as assertEmptyassertTrue ......