国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

directory search
Array Array Helper Benchmarking Benchmarking Class Caching Caching Driver Calendaring Calendaring Class CAPTCHA CAPTCHA Helper Config Config Class Cookie Cookie Helper Database Connecting to your Database Custom Function Calls Database Caching Class Database Configuration Database Forge Class Database Metadata Database Quick Start: Example Code Database Reference Database Utility Class DB Driver Reference Generating Query Results Queries Query Builder Class Query Helper Methods Transactions Date Date Helper Directory Directory Helper Download Download Helper Email Email Class Email Helper Encrypt Encrypt Class Encryption Encryption Library File File Helper File Uploading File Uploading Class Form Form Helper Form Validation Form Validation FTP FTP Class Functions compatibility_functions common_functions HTML HTML Helper HTML Table HTML Table Class Image Manipulation Image Manipulation Class Inflector Inflector Helper Input Input Class Javascript Javascript Class Language Language Class Language Helper Loader Loader Class Migrations Migrations Class Number Number Helper Output Output Class Pagination Pagination Class Path Path Helper Security Security Class Security Helper Session Session Library Shopping Cart Shopping Cart Class Smiley Smiley Helper String String Helper Template Parser Template Parser Class Text Text Helper Trackback Trackback Class Typography Typography Class Typography Helper Unit Testing Unit Testing Class URI URL User Agent XML XML-RPC and XML-RPC Server Zip Encoding Zip Encoding Class XML-RPC and XML-RPC Server Classes XML Helper User Agent Class URL Helper URI Class
characters

單元測試是軟件開發(fā)的一種方法,其中為應(yīng)用程序中的每個(gè)功能編寫測試。如果你對這個(gè)概念不熟悉,你可以在這個(gè)主題上進(jìn)行一些搜索。

CodeIgniter的單元測試類非常簡單,包含一個(gè)評估函數(shù)和兩個(gè)結(jié)果函數(shù)。它不是一個(gè)完整的測試套件,而是一個(gè)簡單的機(jī)制來評估你的代碼,以確定它是否產(chǎn)生正確的數(shù)據(jù)類型和結(jié)果。

  • 使用單元測試庫

    • 定制顯示的測試

    • 創(chuàng)建一個(gè)模板

    • 初始化類

    • 運(yùn)行測試

    • 生成報(bào)告

    • 嚴(yán)格模式

    • 啟用/禁用單元測試

    • 單元測試顯示

  • 類參考

使用單元測試庫

初始化類

像CodeIgniter中的大多數(shù)其他類一樣,Unit Test類在您的控制器中使用$ this-> load-> library函數(shù)進(jìn)行初始化:

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

加載后,單元測試對象將可用 $this->unit

運(yùn)行測試

運(yùn)行測試包括以下列方式提供測試和預(yù)期結(jié)果:

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

測試是您希望測試的代碼的結(jié)果,預(yù)期的結(jié)果是您期望的數(shù)據(jù)類型,測試名稱是可以給您測試的可選名稱,注釋是可選注釋。例:

$test = 1 + 1;$expected_result = 2;$test_name = 'Adds one plus one';$this->unit->run($test, $expected_result, $test_name);

您提供的預(yù)期結(jié)果可以是文字匹配,也可以是數(shù)據(jù)類型匹配。這是一個(gè)文字的例子:

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

這是一個(gè)數(shù)據(jù)類型匹配的例子:

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

注意在第二個(gè)參數(shù)中使用“is_string”?這告訴函數(shù)來評估你的測試是否產(chǎn)生一個(gè)字符串作為結(jié)果。以下是允許的比較類型列表:

  • is_object

  • is_string

  • is_bool

  • is_true

  • is_false

  • is_int

  • is_numeric

  • is_float

  • is_double

  • is_array

  • is_null

  • is_resource

生成報(bào)告

您可以在每次測試后顯示結(jié)果,也可以運(yùn)行多個(gè)測試并在最后生成報(bào)告。要直接顯示報(bào)告,只需回顯或返回運(yùn)行功能:

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

要運(yùn)行所有測試的完整報(bào)告,請使用以下命令:

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

該報(bào)告將被格式化為HTML表格供查看。如果您更喜歡原始數(shù)據(jù),則可以使用以下方法檢索數(shù)組:

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

嚴(yán)格模式

默認(rèn)情況下,單元測試類會(huì)松散地評估文字匹配??紤]這個(gè)例子:

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

測試正在評估一個(gè)整數(shù),但預(yù)期的結(jié)果是一個(gè)布爾值。但是,由于PHP數(shù)據(jù)松散,PHP會(huì)使用普通的相等性測試將上述代碼評估為TRUE:

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

如果你愿意,你可以把單元測試類放在嚴(yán)格模式下,它將比較數(shù)據(jù)類型和值:

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

要啟用嚴(yán)格模式,請使用以

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

啟用/禁用單元測試

如果您想在腳本中保留一些測試,但是除非您需要,否則不會(huì)運(yùn)行測試,您可以使用以下命令禁用單元測試:

$this->unit->active(FALSE);

單元測試顯示

當(dāng)您的單元測試結(jié)果顯示時(shí),下列項(xiàng)目默認(rèn)顯示:

  • 測試名稱(test_name)

  • 測試數(shù)據(jù)類型(test_datatype)

  • 預(yù)期的數(shù)據(jù)類型(res_datatype)

  • 結(jié)果(結(jié)果)

  • 文件名(文件)

  • 行號(行)

  • 您為測試輸入的任何筆記(筆記)

您可以使用$ this-> unit-> set_test_items()來自定義其中哪些項(xiàng)目顯示。例如,如果您只想顯示測試名稱和結(jié)果:

定制顯示的測試

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

創(chuàng)建一個(gè)模板

如果你希望你的測試結(jié)果格式不同,那么你可以設(shè)置你自己的模板。這是一個(gè)簡單模板的例子。請注意所需的偽變量:

$str = '<table border="0" cellpadding="4" cellspacing="1">{rows}        <tr>                <td>{item}</td>                <td>{result}</td>        </tr>{/rows}</table>';$this->unit->set_template($str);

注意

運(yùn)行單元測試過程之前,必須聲明您的模板。

類參考

class CI_Unit_testset_test_items($items)

參數(shù):

$ items(array) - 可見測試項(xiàng)目列表

返回:

空虛

  • $ itemsarray) - 可見測試項(xiàng)目列表

Returns:  void
設(shè)置應(yīng)在測試中可見的項(xiàng)目列表。有效的選項(xiàng)是:
  • test_name

  • test_datatype

  • res_datatype

  • result

  • file

  • line

  • notes

run($test[, $expected = TRUE[, $test_name = 'undefined'[, $notes = '']]])

參數(shù):

$ test(mixed) - 測試數(shù)據(jù)$ expected(混合) - 預(yù)期結(jié)果$ test_name(字符串) - 測試名稱$ notes(字符串) - 任何要附加到測試的注釋

返回:

測試報(bào)告

返回類型:

  • $ test混合) - 測試數(shù)據(jù)

  • $預(yù)期混合) - 預(yù)期結(jié)果

  • $ test_namestring) - 測試名稱

  • $ notesstring) - 任何要附加到測試的注釋

Returns:  Test report
Return type:  string
Runs unit tests.

report([$result = array()])

參數(shù):

$ result(array) - 包含測試結(jié)果的數(shù)組

返回:

測試報(bào)告

返回類型:

  • $ resultarray) - 包含測試結(jié)果的數(shù)組

Returns:  Test report
Return type:  string
生成關(guān)于已完成測試的報(bào)告。

use_strict([$state = TRUE])

參數(shù):

$ state(bool) - 嚴(yán)格狀態(tài)標(biāo)志

返回類型:

空虛

  • $ statebool) - 嚴(yán)格狀態(tài)標(biāo)志

Return type:  void
Enables/disables strict type comparison in tests.

active([$state = TRUE])

參數(shù):

$ state(bool) - 是否啟用測試

返回類型:

void

  • $ statebool) - 是否啟用測試

Return type:  void
啟用/禁用單元測試。

result([$results = array()])

參數(shù):

$ results(array) - 測試結(jié)果列表

返回:

原始結(jié)果數(shù)據(jù)數(shù)組

返回類型:

排列

  • $ resultsarray) - 測試結(jié)果列表

返回:原始結(jié)果數(shù)據(jù)的數(shù)組
Return type:  array
返回原始測試結(jié)果數(shù)據(jù)。

set_template($template)

參數(shù):

$ template(string) - 測試結(jié)果模板

返回類型:

void

  • $ templatestring) - 測試結(jié)果模板

Return type:  void
設(shè)置用于顯示測試結(jié)果的模板。
Previous article: Next article: