本文實(shí)例講述了thinkPHP框架單元測(cè)試庫(kù)tpunit用法。分享給大家供大家參考,具體如下:
thinkphp本身并沒(méi)有提供相應(yīng)的單元測(cè)試支持,所以這里介紹一個(gè)可以對(duì)tp進(jìn)行單元測(cè)試的庫(kù)tpunit。
Tpunit這個(gè)庫(kù)是依賴于phpunit的,其特點(diǎn)是易用、方便和非入侵式。
使用TPUNIT很簡(jiǎn)單,只需要定義一個(gè)路徑常量和導(dǎo)入要被測(cè)試的文件即可。
Home模塊下有個(gè)控制器如下:
namespace Home/Controller;use Think/Controller;class IndexController extends Controller { function test(){ echo 123; }}使用tpunit的話,測(cè)試類:
class IndexTest extends PHPUnit_Framework_TestCase{ //構(gòu)造函數(shù) function __construct(){ //定義TP的版本 define('TPUNIT_VERSION','3.2.3'); //定義目錄路徑,最好為絕對(duì)路徑 define('TP_BASEPATH', 'E:/www/novel/'); //導(dǎo)入base庫(kù) include_once'E:/www/novel/Application/test/base.php'; //導(dǎo)入要測(cè)試的控制器 include_once'E:/www/novel/Application/Home/Controller/IndexController.php'; } //測(cè)試index動(dòng)作 public function testIndex(){ //新建控制器 $index=new /Home/Controller/IndexController(); //調(diào)用控制器的方法 $index->test(); //斷言 $this->expectOutputString('123'); }}然后使用:
phpunit E://www//novel//Application//test//IndexTest.php
即可自動(dòng)進(jìn)行測(cè)試了。
使用上幾乎跟使用phpunit的方法是一模一樣的了。使用后可以對(duì)模型、控制器等等進(jìn)行單元測(cè)試。
TPUNIT庫(kù)github下載地址(詳細(xì)使用說(shuō)明):
https://github.com/CODE7070/TPUNIT
或者點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選