phpunit的安装与使用

phpunit是php的单元测试框架,安装起来十分方便。
一 安装phpunit
下载phpunit然后移动到bin目录下即可
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
之后就可以通过phpunit执行测试脚本了。
二 使用phpunit
一个简单的例子,比较结果是否一致

<?php
 
class ArrayTest extends PHPUnit_Framework_TestCase
{
    public function testNewArrayIsEmpty()
    {
        // 创建数组fixture。
        $fixture = array();
 
        // 断言数组fixture的尺寸是0。
        $this->assertEquals(0, sizeof($fixture));
    }
}
?>

同时可以用dataProvider 提供参数,其中provider函数必须是静态工有函数

class FeedTest extends UnitTestBase {

    /**
     *@dataProvider provider
     */
    public function testArray($a, $b) {

        $this->assertEquals($a, $b);


    }
    
    public static function provider() {
    
        return array(
                array(0, 0),
                array(1, 1),
                array(1, 0),
                array(1, 1),
                array(2, 2),
    
        );
    }   
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注


*