加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

PHP测试框架PHPUnit组织测试操作示例

发布时间:2021-05-26 07:31:17 所属栏目:编程 来源:网络整理
导读:本篇章节讲授PHP测试框架PHPUnit组织测试操纵。分享给各人供各人参考,详细如下: 起首是目次布局 源文件夹为 src/测试文件夹为 tests/ User.php name=$name; } public function Isempty() { try{ if(empty($this->name)) { throw new Exception('its null

本篇章节讲授PHP测试框架PHPUnit组织测试操纵。分享给各人供各人参考,详细如下:

起首是目次布局

PHP测试框架PHPUnit组织测试操纵示例

源文件夹为 src/ 测试文件夹为 tests/

User.php

name=$name; } public function Isempty() { try{ if(empty($this->name)) { throw new Exception('its null',Errorcode::NAME_IS_NULL); } }catch(Exception $e){ return $e->getMessage(); } return 'welcome '.$this->name; } }

对应的单位测试文件 UserTest.php

user = new User(''); } public function testIsempty() { $this->user->name='mark'; $result =$this->user->Isempty(); $this->assertEquals('welcome mark',$result); $this->user->name=''; $results =$this->user->Isempty(); $this->assertEquals('its null',$results); } }

第二个单位测试代码由于要引入 要测试的类 这里可以用 自动载入 停止文件多的话 太多include

以是在src/ 文件夹里写

autoload.php

当必要User类时,就去include User.php。写完__autoload()函数之后要用spl_autoload_register()注册上。

固然可以自动载入,可是要执行的呼吁变得更长了。

打开cmd呼吁如下

以是我们还可以在根目次写一个设置文件phpunit.xml来为项目指定bootstrap,这样就不消每次都写在呼吁里了。

phpunit.xml

然后

打开cmd呼吁 执行MoneyTest 呼吁如下

打开cmd呼吁 执行tests下面全部的文件 呼吁如下

更多关于PHP相干内容感乐趣的读者可查察本站专题:《》、《》、《》、《》、《》、《》、《》及《》

但愿本文所述对各人PHP措施计划有所辅佐。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读