PHP单元测试PHPUnit
2017-04-01 11:30
405 查看
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
测试
先写一个需要测试的类,该类有一个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)则表示断言处成功,即返回值与传入的参数值一致。
安装
首先下载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)则表示断言处成功,即返回值与传入的参数值一致。
相关文章推荐
- phpunit和php单元测试体验
- PHP单元测试工具PHPUnit初体验
- Windows下安装PHP单元测试环境PHPUnit图文教程
- phpstorm配置phpunit,进行php单元测试
- PHP单元测试框架——PHPUnit入门
- 在Eclipse中使用PHPUnit进行单元测试 | Using PHPUnit with eclipse
- PHP单元测试phpunit
- PHP单元测试利器 PHPUNIT深入用法(三)第1/2页
- PHP单元测试利器 PHPUNIT深入用法(二)第1/2页
- php单元测试-phpunit,以及thinkphp框架集成的单元测试下载
- PHP单元测试利器:PHPUNIT初探
- PHP 单元测试(PHPUnit)(2)
- PHP 单元测试(PHPUnit)(2)
- Windows下安装PHP单元测试环境PHPUnit图文教程
- PHP 单元测试(PHPUnit)(1)
- 【PHP】PHPUnit单元测试利器:PHP Mock的使用方法
- PHP单元测试利器:PHPUnit深入用法
- PHP 单元测试(PHPUnit)(1)
- PHP单元测试利器 PHPUNIT深入用法(二)第1/2页
- PHP 单元测试(PHPUnit)