<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 單元測試類 單元測試是一種為你的應用程序中的每個函數編寫測試的軟件開發方法。如果你還不熟悉這個概念, 你應該先去 Google 一下。 CodeIgniter 的單元測試類非常簡單,由一個測試方法和兩個顯示結果的方法組成。 它沒打算成為一個完整的測試套件,只是提供一個簡單的機制來測試你的代碼是否 生成了正確的數據類型和結果。 * [使用單元測試類庫](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id2) * [初始化類](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id3) * [運行測試](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id4) * [生成報告](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id5) * [嚴格模式](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id6) * [啟用/禁用單元測試](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id7) * [單元測試結果顯示](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id8) * [自定義顯示測試結果](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id9) * [創建模板](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id10) * [類參考](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id11) ## [使用單元測試類庫](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id12) ### [初始化類](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id13) 正如 CodeIgniter 中的其他類一樣,在你的控制器中使用?$this->load->library()?方法來初始化單元測試類: ~~~ $this->load->library('unit_test'); ~~~ 初始化之后,單元測試類的對象就可以這樣訪問: ~~~ $this->unit ~~~ ### [運行測試](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id14) 要運行一個測試用例,需要提供一個測試和一個期望結果,像下面這樣: ~~~ $this->unit->run('test', 'expected result', 'test name', 'notes'); ~~~ 其中,test 是你希望測試的代碼的結果,expected result 是期望返回的結果,test name 是可選的, 你可以為你的測試取一個名字,notes 是可選的,可以填些備注信息。例如: ~~~ $test = 1 + 1; $expected_result = 2; $test_name = 'Adds one plus one'; $this->unit->run($test, $expected_result, $test_name); ~~~ 期望的結果可以是字面量匹配(a literal match),也可以是數據類型匹配(a data type match)。 下面是字面量匹配的例子: ~~~ $this->unit->run('Foo', 'Foo'); ~~~ 下面是數據類型匹配的例子: ~~~ $this->unit->run('Foo', 'is_string'); ~~~ 注意第二個參數 "is_string" ,這讓方法測試返回的結果是否是字符串類型。以下是可用的數據類型的列表: * is_object * is_string * is_bool * is_true * is_false * is_int * is_numeric * is_float * is_double * is_array * is_null * is_resource ### [生成報告](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id15) 你可以在每個測試之后顯示出測試的結果,也可以先運行幾個測試,然后在最后生成一份測試結果的報告。 要簡單的顯示出測試結果,可以直接在 run 方法的前面使用 echo: ~~~ echo $this->unit->run($test, $expected_result); ~~~ 要顯示一份所有測試的完整報告,使用如下代碼: ~~~ echo $this->unit->report(); ~~~ 這份報告會以 HTML 的表格形式顯示出來,如果你喜歡獲取原始的數據,可以通過下面的代碼得到一個數組: ~~~ echo $this->unit->result(); ~~~ ### [嚴格模式](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id16) 默認情況下,單元測試類在字面量匹配時是松散的類型匹配。看下面這個例子: ~~~ $this->unit->run(1, TRUE); ~~~ 正在測試的結果是一個數字,期望的結果是一個布爾型。但是,由于 PHP 的松散數據類型, 如果使用常規的比較操作符的話,上面的測試結果將會是 TRUE 。 > if (1 == TRUE) echo 'This evaluates as true'; 如果愿意的話,你可以將單元測試設置為嚴格模式,它不僅會比較兩個數據的值, 而且還會比較兩個數據的數據類型: ~~~ if (1 === TRUE) echo 'This evaluates as FALSE'; ~~~ 使用如下代碼啟用嚴格模式: ~~~ $this->unit->use_strict(TRUE); ~~~ ### [啟用/禁用單元測試](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id17) 如果你希望在你的代碼中保留一些測試,只在需要的時候才被執行,可以使用下面的代碼禁用單元測試: ~~~ $this->unit->active(FALSE); ~~~ ### [單元測試結果顯示](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id18) 單元測試的結果默認顯示如下幾項: * Test Name (test_name) * Test Datatype (test_datatype) * Expected Datatype (res_datatype) * Result (result) * File Name (file) * Line Number (line) * Any notes you entered for the test (notes) 你可以使用 $this->unit->set_test_items() 方法自定義要顯示哪些結果,例如, 你只想顯示出測試名和測試的結果: #### [自定義顯示測試結果](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id19) ~~~ $this->unit->set_test_items(array('test_name', 'result')); ~~~ #### [創建模板](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id20) 如果你想讓你的測試結果以不同于默認的格式顯示出來,你可以設置你自己的模板, 這里是一個簡單的模板例子,注意那些必須的偽變量: ~~~ $str = ' <table border="0" cellpadding="4" cellspacing="1"> {rows} <tr> <td>{item}</td> <td>{result}</td> </tr> {/rows} </table>'; $this->unit->set_template($str); ~~~ 注解 你的模板必須在運行測試?**之前**?被定義。 ## [類參考](http://codeigniter.org.cn/user_guide/libraries/unit_testing.html#id21) classCI_Unit_test set_test_items($items) 參數: * **$items**?(array) -- List of visible test items 返回: void 設置要在測試的結果中顯示哪些項,有效的選項有: > > > * test_name > * test_datatype > * res_datatype > * result > * file > * line > * notes > > run($test[,?$expected = TRUE[,?$test_name = 'undefined'[,?$notes = '']]]) 參數: * **$test**?(mixed) -- Test data * **$expected**?(mixed) -- Expected result * **$test_name**?(string) -- Test name * **$notes**?(string) -- Any notes to be attached to the test 返回: Test report 返回類型: string 運行單元測試。 report([$result = array()]) 參數: * **$result**?(array) -- Array containing tests results 返回: Test report 返回類型: string 根據已運行的測試生成一份測試結果的報告。 use_strict([$state = TRUE]) 參數: * **$state**?(bool) -- Strict state flag 返回類型: void 在測試中啟用或禁用嚴格比較模式。 active([$state = TRUE]) 參數: * **$state**?(bool) -- Whether to enable testing 返回類型: void 啟用或禁用單元測試。 result([$results = array()]) 參數: * **$results**?(array) -- Tests results list 返回: Array of raw result data 返回類型: array 返回原始的測試結果數據。 set_template($template) 參數: * **$template**?(string) -- Test result template 返回類型: void 設置顯示測試結果數據的模板。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看