亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

directory search
歡迎 目錄 快速參考圖 基本信息 服務(wù)器要求 許可協(xié)議 變更記錄 關(guān)于CodeIgniter 安裝 下載 CodeIgniter 安裝指導(dǎo) 從老版本升級(jí) 疑難解答 介紹 開始 CodeIgniter 是什么? CodeIgniter 速記表 支持特性 應(yīng)用程序流程圖 模型-視圖-控制器 架構(gòu)目標(biāo) 教程 內(nèi)容提要 加載靜態(tài)內(nèi)容 創(chuàng)建新聞條目 讀取新聞條目 結(jié)束語 常規(guī)主題 CodeIgniter URL 控制器 保留字 視圖 模型 輔助函數(shù) 使用 CodeIgniter 類庫 創(chuàng)建你自己的類庫 使用 CodeIgniter 適配器 創(chuàng)建適配器 創(chuàng)建核心系統(tǒng)類 鉤子 - 擴(kuò)展框架的核心 自動(dòng)裝載資源 公共函數(shù) URI 路由 錯(cuò)誤處理 緩存 調(diào)試應(yīng)用程序 以CLI方式運(yùn)行 管理應(yīng)用程序 處理多環(huán)境 PHP替代語法 安全 開發(fā)規(guī)范 類庫參考 基準(zhǔn)測(cè)試類 日歷類 購物車類 配置類 Email 類 加密類 文件上傳類 表單驗(yàn)證詳解 FTP 類 圖像處理類 輸入類 Javascript 類 語言類 裝載類 遷移類 輸出類 分頁類 模板解析器類 安全類 Session 類 HTML 表格類 引用通告類 排版類 單元測(cè)試類 URI 類 User-Agent 類 表單驗(yàn)證 XML-RPC 和 XML-RPC 服務(wù)器 Zip 編碼類 緩存適配器 適配器參考 適配器 數(shù)據(jù)庫類 Active Record 類 數(shù)據(jù)庫緩存類 自定義函數(shù)調(diào)用 數(shù)據(jù)庫配置 連接你的數(shù)據(jù)庫 數(shù)據(jù)庫快速入門例子代碼 字段數(shù)據(jù) 數(shù)據(jù)庫維護(hù)類 查詢輔助函數(shù) 數(shù)據(jù)庫類 查詢 生成查詢記錄集 表數(shù)據(jù) 事務(wù) 數(shù)據(jù)庫工具類 JavaScript類 輔助函數(shù)參考 數(shù)組輔助函數(shù) CAPTCHA 輔助函數(shù) Cookie Helper 日期輔助函數(shù) 目錄輔助函數(shù) 下載輔助函數(shù) Email 輔助函數(shù) 文件輔助函數(shù) 表單輔助函數(shù) HTML輔助函數(shù) Inflector 輔助函數(shù) 語言輔助函數(shù) 數(shù)字輔助函數(shù) 路徑輔助函數(shù) 安全輔助函數(shù) 表情輔助函數(shù) 字符串輔助函數(shù) 文本輔助函數(shù) 排版輔助函數(shù) URL 輔助函數(shù) XML 輔助函數(shù)
characters

CodeIgniter 用戶指南 版本 2.1.0

編輯文檔、查看近期更改請(qǐng) 登錄 或 注冊(cè)  找回密碼
查看原文

單元測(cè)試類

單元測(cè)試是一種用于測(cè)試應(yīng)用程序中每個(gè)函數(shù)的軟件開發(fā)方法。如果你對(duì)此概念不熟悉,或許需要google一下相關(guān)概念。

CodeIgniter的單元測(cè)試類非常簡(jiǎn)單,由一個(gè)評(píng)估函數(shù)和兩個(gè)結(jié)果函數(shù)組成。它沒打算成為一個(gè)十全的測(cè)試集,只提供一個(gè)簡(jiǎn)單的能夠評(píng)測(cè)你的代碼是否可以產(chǎn)生正確的數(shù)據(jù)類型及結(jié)果的解決方案。

初始化單元類

和大多數(shù)其它的類一樣,在CodeIgniter中,單元測(cè)試類一樣要在控制器中用函數(shù)$this->load->library 來初始化:

$this->load->library('unit_test');

測(cè)試類一旦加載,單元測(cè)試對(duì)象可以這樣使用:$this->unit

運(yùn)行測(cè)試:

運(yùn)行一個(gè)測(cè)試應(yīng)提供一個(gè)測(cè)試及期望的結(jié)果給以下函數(shù):

$this->unit->run( test, expected result, 'test name', 'notes');

Where test is the result of the code you wish to test, expected result is the data type you expect, test name is an optional name you can give your test, and notes are optional notes. Example:

$test = 1 + 1;

$expected_result = 2;

$test_name = 'Adds one plus one';

$this->unit->run($test, $expected_result, $test_name);

你所給出的期望結(jié)果($expected_result)可以是字面上匹配(literal match)的也可以是數(shù)據(jù)類型上匹配的。下例為字面匹配(literal match):

$this->unit->run('Foo', 'Foo');

下例為類型匹配(data type match):

$this->unit->run('Foo', 'is_string');

注意第二個(gè)參數(shù)"is_string"的使用,它告訴函數(shù)測(cè)試你的測(cè)試用例把產(chǎn)生的將是一個(gè)字符串。下面是合法的類型列表:

  • is_object
  • is_string
  • is_bool
  • is_true
  • is_false
  • is_int
  • is_numeric
  • is_float
  • is_double
  • is_array
  • is_null

生成報(bào)告

你可以在每個(gè)測(cè)試后立刻顯示結(jié)果,也可以運(yùn)行若干測(cè)試后再產(chǎn)生報(bào)告。直接顯示一個(gè)報(bào)告,可以用echo或者返回run函數(shù)(的執(zhí)行結(jié)果):

echo $this->unit->run($test, $expected_result);

顯示所有測(cè)試的完整的報(bào)告,可以這樣:

echo $this->unit->report();

報(bào)告會(huì)以格式化了的HTML表格的形式呈現(xiàn)。如果你更喜歡看原始的數(shù)據(jù),可以用下面代碼獲取一個(gè)數(shù)組:

echo $this->unit->result();

嚴(yán)格模式

默認(rèn)情況下,單元測(cè)試類評(píng)測(cè)字面匹配(literal match)時(shí)比較寬松,考慮以下例子:

$this->unit->run(1, TRUE);

此測(cè)試評(píng)測(cè)的是一個(gè)整數(shù),但是期望結(jié)果卻是布爾值。不過,由于PHP是弱類型語言,使用正常的相等測(cè)試,以上代碼返回的將是TRUE:

if (1 == TRUE) echo 'This evaluates as true';

喜歡的話,你可以讓單元測(cè)試類在嚴(yán)格模式下執(zhí)行,這樣它會(huì)同時(shí)比較兩數(shù)據(jù)的類型及值:

if (1 === TRUE) echo 'This evaluates as FALSE';

啟用嚴(yán)格模式:

$this->unit->use_strict(TRUE);

啟用/禁用單元測(cè)試

如果你想放一些測(cè)試在代碼中,而又希望讓它只在需要的時(shí)候運(yùn)行,你可以禁用單元測(cè)試:

$this->unit->active(FALSE)

Unit Test Display

When your unit test results display, the following items show by default:

試翻譯:當(dāng)你顯示單元測(cè)試結(jié)果的時(shí)候,以下信息(原以為條目)將被默認(rèn)顯示:

  • Test Name (test_name)
  • 試翻譯:測(cè)試名稱(test_name)
  • Test Datatype (test_datatype)
  • 試翻譯:測(cè)試數(shù)據(jù)類型(test_datatype)
  • Expected Datatype (res_datatype)
  • 試翻譯:期望數(shù)據(jù)類型(res_datatype)
  • Result (result)
  • 試翻譯:結(jié)果(result)
  • File Name (file)
  • 試翻譯:文件名稱(file)
  • Line Number (line)
  • 試翻譯:行數(shù)(line)
  • Any notes you entered for the test (notes)
  • 試翻譯:手動(dòng)添加的注釋(notes)

You can customize which of these items get displayed by using $this->unit->set_items(). For example, if you only wanted the test name and the result displayed:

試翻譯:使用$this->unit->set_items() ,你可以自定義顯示信息(條目). 比如,如果你只期望顯示測(cè)試名稱和測(cè)試結(jié)果:

Customizing displayed tests

試翻譯:自定義顯示測(cè)試結(jié)果

$this->unit->set_test_items(array('test_name', 'result'));

Creating a Template

試翻譯:創(chuàng)建顯示模板

如果你希望測(cè)試結(jié)果以自定義的格式顯示,這里提供一個(gè)簡(jiǎn)單的模板示例。注意必需的偽變量(seudo-variables):

$str = '
<table border="0" cellpadding="4" cellspacing="1">
????{rows}
????????<tr>
????????<td>{item}</td>
????????<td>{result}</td>
????????</tr>
????{/rows}
</table>';

$this->unit->set_template($str);

注意: 你的模板必須在單元測(cè)試運(yùn)行之前聲明。

?

翻譯貢獻(xiàn)者: Hex, iptton, zhou78620051
最后修改: 2011-11-24 15:46:57
Previous article: Next article: