PHPUnit测试框架如何在PHP中使用-创新互联
这篇文章给大家介绍PHPUnit测试框架如何在PHP中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

目录结构

源文件夹为 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 --bootstrap src/autoload.php tests/UserTest所以我们还可以在根目录写一个配置文件phpunit.xml来为项目指定bootstrap,这样就不用每次都写在命令里了。
phpunit.xml
然后
打开cmd命令 执行MoneyTest 命令如下
phpunit tests/UserTest打开cmd命令 执行tests下面所有的文件 命令如下
phpunit tests关于PHPUnit测试框架如何在PHP中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
分享标题:PHPUnit测试框架如何在PHP中使用-创新互联
URL地址:http://www.jxjierui.cn/article/pdiec.html


咨询
建站咨询
