PHPUnit学习笔记

发表于 2018-03-27 08:44:19
阅读 22

1. 安装phpunit

我们今天只讨论linux版本,因为很多成熟的项目在windows下是不能完全兼容的,他依赖很多linux固有的工具

wget http://phar.phpunit.cn/phpunit.phar
php phpunit.phar --version

安装成功的情况下会显示

PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

请使用高版本的php运行phpunit.phar,低版本的php将会报错不兼容(建议使用php7或以上的版本)


2. 测试

建立一个程序文件 src/test.inc,内容如下:

class testUnit{

    public function time(){
        return time();
    }
}

建立一个测试文件 tests/test.inc,内容如下:

use PHPUnit\Framework\TestCase;

class testUnit_test extends TestCase{

    public function test_hello(){
        $my = new testUnit();
        $ret = $my->time();
        $this->assertEquals(
                0,
                $ret
            );
    }
}

启动测试脚本

php phpunit.phar --bootstrap src/test.inc tests/test.inc

成功的话会显示如下内容

PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 72 ms, Memory: 8.00MB

OK (1 test, 1 assertion)


3. 小结

phpunit的工作方式,一句话就是:通过内建的TestCase对象自带的各种判定(断言)方法对我们要测试的程序代码的返回值进行判定。


那么我们使用phpunit的好处是什么呢?就是“好多人已经在用了”。。。


其实这种玩意,哪个工程师也都会写,而且还可以结合自己的项目写得很贴近很好用。


那么为什么我们非要用人家的东西呢??

其原因就是因为人家老外写代码很标准,很规范。而且,他们有大把的时间把一个小东西写得非常细致,非常专业,非常。。。反正就是给工具化,产品化了。

算了吧,既然人家都在用,学吧~~

鬼谷子叔叔

跟福哥学编程吧~~
日志
212
浏览
1626

相关日志