Install PHPUnit in Centos5.4.

Source: Internet
Author: User

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:

  1. Wgethttp: // pear.php.net/go-pear.phar
  2. /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"

  1. Pearchannel-discoverpear.phpunit.de
  2. Pearchannel-discovercomponents.ez.no
  3. Pearchannel-discoverpear.symfony-project.com
  4. Pearupgrade-all
  5. 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
  1. Classhello_testextendsPHPUnit_Framework_TestCase
  2. {
  3. Publicfunctiontest_equal ()
  4. {
  5. $ Stack = array ();
  6. $ This-> assertEquals (0,123); // judge whether 0 and 123 are equal
  7. }
  8. }
  9. ?>
<? 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:

  1. Pearupdate-channels
  2. 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:

  1. 1pearchannel-discoverpear.symfony.com
  2. 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 ......

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.