PHP单元测试PHPUnit简单用法示例
本文实例讲述了PHP单元测试PHPUnit简单用法。分享给大家供大家参考,具体如下:
windows开发环境下,PHP使用单元测试可以使用PHPUnit。
安装
首先下载PHPUnit,官网:https://phpunit.de/ 根据自己的PHP版本下载对应的PHPUnit版本,我本地是PHP5.5,所以这里我下载PHPUnit4.8。下载完成得到phpunit-4.8.35.phar文件,放到任意目录,这边我放到D:\phpunit下,并把文件名改为:phpunit.phar 。配置环境变量:右击我的电脑-》属性-》高级系统设置-》环境变量-》编辑path在最后添加phpunit.phar的路径,这里我是D:\phpunit,所以在最后添加D:\phpunit 。
打开命令行win+R输入cmd,进入到D:\phpunit
cd /d D:\phpunit
安装phpunit
echo @php "%~dp0phpunit.phar" %* > phpunit.cmd
查看是否安装成功
phpunit --version
如果显示phpunit的版本信息,说明安装成功了,这边我显示:PHPUnit 4.8.35 by Sebastian Bergmann and contributors.
测试
先写一个需要测试的类,该类有一个eat方法,方法返回字符串:eating,文件名为Human.php
<?php class Human { public function eat() { return 'eating'; } }
再写一个phpunit的测试类,测试Human类的eat方法,必须引入Human.php文件、phpunit,文件名为test1.php
<?php include 'Human.php'; use PHPUnit\Framework\TestCase; class TestHuman extends TestCase { public function testEat() { $human = new Human; $this->assertEquals('eating', $human->eat()); } } ?>
其中assertEquals方法为断言,判断eat方法返回是否等于'eating',如果返回一直则成功否则返回错误,运行测试:打开命令行,进入test1.php的路径,然后运行测试:
phpunit test1.php
返回信息:
PHPUnit 4.8.35 by Sebastian Bergmann and contributors.
.
Time: 202 ms, Memory: 14.75MB
OK (1 test, 1 assertion)
则表示断言处成功,即返回值与传入的参数值一致。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家PHP程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程