PHPunit安装和使用

以下在Ubuntu12.04中完成:

sudo pear channel-discover pear.phpunit.de sudo pear channel-discover pear.symfony-project.com sudo pear channel-discover components.ez.no sudo pear update-channels sudo pear upgrade-all sudo pear install –alldeps phpunit/PHPUnit #报错就运行下面那句 sudo pear install –force –alldeps phpunit/PHPUnit

例子:对checkemail函数进行单元测试:

require('PHPUnit/Framework/TestCase.php');
require('libs/user.class.php');

/*
   user.class.php里User类的单元测试
*/
class UserTest extends PHPUnit_Framework_TestCase
{
   public function test_check_email()
   {
      $this->assertTrue(User::check_email('lx@shellcodes.org'),true);
      $this->assertFalse(User::check_email('lx@<script>.org'),false);
      $this->assertTrue(User::check_email('123@123.org'),true);
$this->assertFalse(User::check_email('<script>@123.org'),false);
   }
}

最后用phpunit执行这个文件即可。