這篇文章給大家介紹PHPUnit測(cè)試框架如何在PHP中使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

目錄結(jié)構(gòu)

源文件夾為 src/
測(cè)試文件夾為 tests/
User.php
<?php
class Errorcode
{
const NAME_IS_NULL = 0;
}
class User
{
public $name;
public function __construct($name)
{
$this->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;
}
}對(duì)應(yīng)的單元測(cè)試文件 UserTest.php
<?php
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
protected $user;
public function setUp()
{
$this->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);
}
}第二個(gè)單元測(cè)試代碼因?yàn)橐?要測(cè)試的類 這里可以用 自動(dòng)載入 避免文件多的話 太多include
所以在src/ 文件夾里寫autoload.php
<?php
function __autoload($class){
include $class.'.php';
}
spl_autoload_register('__autoload');當(dāng)需要User類時(shí),就去include User.php。寫完__autoload()函數(shù)之后要用spl_autoload_register()注冊(cè)上。
雖然可以自動(dòng)載入,但是要執(zhí)行的命令變得更長(zhǎng)了。
打開cmd命令如下
phpunit --bootstrap src/autoload.php tests/UserTest
所以我們還可以在根目錄寫一個(gè)配置文件phpunit.xml來為項(xiàng)目指定bootstrap,這樣就不用每次都寫在命令里了。
phpunit.xml
<phpunit bootstrap="src/autoload.php"> </phpunit>
然后
打開cmd命令 執(zhí)行MoneyTest 命令如下
phpunit tests/UserTest
打開cmd命令 執(zhí)行tests下面所有的文件 命令如下
phpunit tests
關(guān)于PHPUnit測(cè)試框架如何在PHP中使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁(yè)題目:PHPUnit測(cè)試框架如何在PHP中使用-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://www.chinadenli.net/article42/pdiec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、虛擬主機(jī)、品牌網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容