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.4PHP version: php5.3. 1. install Pear
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
PHPVersion: Php5.3.
1. install Pear
First, install the latest pear. The installation command is as follows:
- Wget http://pear.php.net/go-pear.phar
- /Usr/local/php5.3/bin/php go-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/-name php to find the location of the php executable file;
2. try to add sudo to the command or use su root 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/-name pear to locate the pear location.
2. enter this statement export PATH = "$ PATH:/usr/local/php5.3/bin/pear"
- Pear channel-discover pear.phpunit.de
- Pear channel-discover components. ez. no
- Pear channel-discover pear.symfony-project.com
- Pear upgrade-all
- Pear install phpunit/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, yum install php-dom-y.
Congratulations! If everything goes well, you have successfully installed PHPUnit.
3. use PHPUnit
Example: hello. php
Class hello_test extends PHPUnit_Framework_TestCase
{
Public function test_equal ()
{
$ Stack = array ();
$ This-> assertEquals (0,123); // judge whether 0 and 123 are equal
}
}
?>
AssertEquals (0,123); // judge whether 0 and 123 are equal}?>
Phpunit hello. php: If OK is displayed, it indicates 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:
- Pear update-channels
- Pear upgrade-all
pear update-channels pear upgrade-all
After installation:
Pear install-alldeps phpunit/PHPUnit
pear install –alldeps phpunit/PHPUnit
An error is returned:
Unknownremote channel: pear.symfony.com
Phpunit/PHPUnit requires package "channel: // pear.symfony.com/Yaml" (version> = 2.1.0)
Google to find a useful solution:
- 1 pear channel-discover pear.symfony.com
- 2 pear install pear.symfony.com/Yaml
1 pear channel-discover pear.symfony.com2 pear install pear.symfony.com/Yaml
Then execute again
Pear install-alldeps phpunit/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 assertEmpty assertTrue ......