安装phpunit:
在php的目录下:
复制代码 代码如下:
pear channel-discover pear;
pear install phpunit/phpunit
windows下将php的环境变量加入到path环境变量中。
简单使用:
复制代码 代码如下:
class stacktest extends phpunit_framework_testcase
{
public function testarray()
{
$stack = array();
$this->assertequals(0, count($stack));
array_push($stack, 'foo');
$this->assertequals('foo', $stack[count($stack)-1]);
$this->assertequals(1, count($stack));
$this->assertequals('foo', array_pop($stack));
$this->assertequals(0, count($stack));
}
/**
* @test
*/
public function stringlen()
{
$str = 'abc';
$this->assertequals(3, strlen($str));
}
}
从上可以看到编写phpunit的基本规律:
(1)类class的测试写在classtest中
(2)classtest继承phpunit_framework_testcase
(3)测试方法都是test*格式,也可以通过@test将其标注为测试方法。
(4)通过断言方法assertequals来对实际值和预期值进行断言。