Home > php教程 > php手册 > body text

PHPUnit安装及使用示例

WBOY
Release: 2016-06-06 20:18:40
Original
1359 people have browsed it

PHPUnit是一个用PHP编程语言开发的开源软件,是一个单元测试框架。PHPUnit由Sebastian Bergmann创建,源于Kent Beck的SUnit,是xUnit家族的框架之一。本文将介绍

PHPUnit是zend官方大力支持的测试框架,高质量的单元测试时保证项目质量的基础,,能够有效的减少BUG,改善程序。

安装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来对实际值和预期值进行断言。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!