本文網址http://write.blog.csdn.net/postedit
使用phpunit 工具測試框架程序,最重要的是要在測試環境中模擬框架環境
比如 配置文件,加載模塊,session,cookie 等等,如果沒有特別需求,儘量用thinkphp 系統加載所有環境,
1、測試工具包
一個專門測試thinkphp 的封裝工具,在編寫測試代碼時把工具包加載到測試文件中
工具包:有兩部分組成:核心包Library,項目包:Thinkphp項目名稱。
核心包Library 主要由以下文件組成
#測試控制器Action 的基類,所有測試action 的case 都必須繼承此類
BaseActionTest.php
#測試頁面(Html)的基類,所有測試Html的case 都必須繼承此類
BaseHtmlTest.php
#測試業務(Service)的基類,所有測試Service的case 都必須繼承此類
BaseModelTest.php
# 測試控制器工具類
Controller.php
HttpWebRequest2.php
HttpWebRequest.php
IHttpWebRequest.php
#測試控制器時設置session 文件
Setsession.php
# Thinkphp 項目入口文件
TPConfig.php
項目包 主要由以下文件組成
#測試控制器文件目錄
Action
#測試頁面文件目錄
Html
#測試業務文件目錄
Service
#測試控制器文件時必須加載的初始化文件
ActionInit.php
#測試頁面文件時必須加載的初始化文件
HtmlInit.php
#測試業務文件時必須加載的初始化文件
ServiceInit.php
#測試項目的配置文件
Config.php
2、修改Thinkphp代碼
1、打開ThinkPHP/Lib/Core/App.class.php 文件,此處修改,主要是爲了測試model 類
,找到App::exec()。
修改爲:
if(!defined('APP_PHPUNIT_MOLDE') || !APP_PHPUNIT_MOLDE){
App::exec();
}
2、修改項目配置文件,以便加載測試數據庫
打開{項目名稱}/Config/config.php 文件。
文件頭部寫入:
if(defined('APP_PHPUNIT') || APP_PHPUNIT == true) {
$_systemConfig = require APP_ROOT_PATH.'testsystemconfig.php';
} else {
$_systemConfig = require APP_ROOT_PATH.'systemconfig.php';
}
3、Thinkphp 測試用例
在測試項目目錄Service目錄下創建文件UserServiceTest.php,內容如下
<?php
require_once'PHPUnit/Autoload.php';
require_once'../ServiceInit.php';
/**
* 會員業務測試類
*
*/
class UserServiceTest extends BaseModelTest
{
/**
* 測試管理員登錄
*/
public function testLogin()
{
//echo THINK_PATH;
$s_user = Ap::GetService('User');
$_rtn =$s_user->Login('admin','admin');
$this->assertEquals(true,$_rtn);
}
/**
* 驗證用戶名是否唯一檢查
*/
public function testCheck()
{
//echo THINK_PATH;
$s_user = Ap::GetService('User');
$_rtn =$s_user->Login('admin','admin');
$this->assertEquals(true,$_rtn);
}
}
?>
切換到Service目錄下,命令行輸入:phpunit UserServiceTest.php
特別說明:在正式使用時,一般都不用在代碼中直接加載
require_once'PHPUnit/Autoload.php';
require_once'../ServiceInit.php';
正式使用時,都會有工具自動加載,或phpunit.xml 已經配置好加載文件
er NewSmoo@ott-family:"Times New Roman";color:#3F5FBF'> </blacklist>
-->
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix="Service.class.php">src/Service</directory>
<!--
<file>ArrayTest.php</file>
//排除文件
<exclude>
<directorysuffix=".php">action/lib</directory>
<directorysuffix=".php">model</directory>
<file>action/lib/Loginxxx.php</file>
</exclude>
-->
</whitelist>
</filter>
<!-- 測試結果:代碼覆蓋率,測試結果
<logging>
<logtype="coverage-html" target="/tmp/report"charset="UTF-8"
highlight="false" lowUpperBound="35"highLowerBound="70"/>
<logtype="coverage-clover" target="/tmp/coverage.xml"/>
<logtype="coverage-php" target="/tmp/coverage.serialized"/>
<logtype="coverage-text" target="php://stdout"showUncoveredFiles="false"/>
<logtype="json" target="/tmp/logfile.json"/>
<logtype="tap" target="/tmp/logfile.tap"/>
<logtype="junit" target="/tmp/logfile.xml"logIncompleteSkipped="false"/>
<logtype="testdox-html" target="/tmp/testdox.html"/>
<logtype="testdox-text" target="/tmp/testdox.txt"/>
</logging>
-->
<logging>
<!-- target(report/html)生成html 文件的目錄-->
<log type="coverage-html"target="test/Log/html"charset="UTF-8"yui="true"highlight="false"lowUpperBound="35"highLowerBound="70"/>
<!-- target(report/coverage/coverage.xml) 生成xml的文件名,生成的xml 用圖標插件解析xml-->
<log type="coverage-clover"target="test/Log/coverage/coverage.xml"/>
</logging>
</phpunit>
本文網址http://write.blog.csdn.net/postedit