Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################## |
| 2 | Unit Testing Class |
| 3 | ################## |
| 4 | |
| 5 | Unit testing is an approach to software development in which tests are |
| 6 | written for each function in your application. If you are not familiar |
| 7 | with the concept you might do a little googling on the subject. |
| 8 | |
| 9 | CodeIgniter's Unit Test class is quite simple, consisting of an |
| 10 | evaluation function and two result functions. It's not intended to be a |
| 11 | full-blown test suite but rather a simple mechanism to evaluate your |
| 12 | code to determine if it is producing the correct data type and result. |
| 13 | |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 14 | .. contents:: |
| 15 | :local: |
| 16 | |
| 17 | .. raw:: html |
| 18 | |
| 19 | <div class="custom-index container"></div> |
| 20 | |
James L Parry | 08191be | 2014-12-20 02:37:13 -0800 | [diff] [blame] | 21 | ****************************** |
| 22 | Using the Unit Testing Library |
| 23 | ****************************** |
| 24 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 25 | Initializing the Class |
| 26 | ====================== |
| 27 | |
| 28 | Like most other classes in CodeIgniter, the Unit Test class is |
| 29 | initialized in your controller using the $this->load->library function:: |
| 30 | |
| 31 | $this->load->library('unit_test'); |
| 32 | |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 33 | Once loaded, the Unit Test object will be available using ``$this->unit`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | |
| 35 | Running Tests |
| 36 | ============= |
| 37 | |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 38 | Running a test involves supplying a test and an expected result in the |
| 39 | following way: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 41 | $this->unit->run('test', 'expected result', 'test name', 'notes'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
| 43 | Where test is the result of the code you wish to test, expected result |
| 44 | is the data type you expect, test name is an optional name you can give |
| 45 | your test, and notes are optional notes. Example:: |
| 46 | |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 47 | $test = 1 + 1; |
| 48 | |
| 49 | $expected_result = 2; |
| 50 | |
| 51 | $test_name = 'Adds one plus one'; |
| 52 | |
| 53 | $this->unit->run($test, $expected_result, $test_name); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
| 55 | The expected result you supply can either be a literal match, or a data |
| 56 | type match. Here's an example of a literal:: |
| 57 | |
| 58 | $this->unit->run('Foo', 'Foo'); |
| 59 | |
| 60 | Here is an example of a data type match:: |
| 61 | |
| 62 | $this->unit->run('Foo', 'is_string'); |
| 63 | |
| 64 | Notice the use of "is_string" in the second parameter? This tells the |
| 65 | function to evaluate whether your test is producing a string as the |
| 66 | result. Here is a list of allowed comparison types: |
| 67 | |
| 68 | - is_object |
| 69 | - is_string |
| 70 | - is_bool |
| 71 | - is_true |
| 72 | - is_false |
| 73 | - is_int |
| 74 | - is_numeric |
| 75 | - is_float |
| 76 | - is_double |
| 77 | - is_array |
| 78 | - is_null |
ftwbzhao | a5ea506 | 2015-07-06 13:18:11 +0800 | [diff] [blame] | 79 | - is_resource |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
| 81 | Generating Reports |
| 82 | ================== |
| 83 | |
| 84 | You can either display results after each test, or your can run several |
| 85 | tests and generate a report at the end. To show a report directly simply |
| 86 | echo or return the run function:: |
| 87 | |
| 88 | echo $this->unit->run($test, $expected_result); |
| 89 | |
| 90 | To run a full report of all tests, use this:: |
| 91 | |
| 92 | echo $this->unit->report(); |
| 93 | |
| 94 | The report will be formatted in an HTML table for viewing. If you prefer |
| 95 | the raw data you can retrieve an array using:: |
| 96 | |
| 97 | echo $this->unit->result(); |
| 98 | |
| 99 | Strict Mode |
| 100 | =========== |
| 101 | |
| 102 | By default the unit test class evaluates literal matches loosely. |
| 103 | Consider this example:: |
| 104 | |
| 105 | $this->unit->run(1, TRUE); |
| 106 | |
| 107 | The test is evaluating an integer, but the expected result is a boolean. |
| 108 | PHP, however, due to it's loose data-typing will evaluate the above code |
| 109 | as TRUE using a normal equality test:: |
| 110 | |
| 111 | if (1 == TRUE) echo 'This evaluates as true'; |
| 112 | |
| 113 | If you prefer, you can put the unit test class in to strict mode, which |
| 114 | will compare the data type as well as the value:: |
| 115 | |
| 116 | if (1 === TRUE) echo 'This evaluates as FALSE'; |
| 117 | |
| 118 | To enable strict mode use this:: |
| 119 | |
| 120 | $this->unit->use_strict(TRUE); |
| 121 | |
| 122 | Enabling/Disabling Unit Testing |
| 123 | =============================== |
| 124 | |
| 125 | If you would like to leave some testing in place in your scripts, but |
| 126 | not have it run unless you need it, you can disable unit testing using:: |
| 127 | |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 128 | $this->unit->active(FALSE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
| 130 | Unit Test Display |
| 131 | ================= |
| 132 | |
| 133 | When your unit test results display, the following items show by |
| 134 | default: |
| 135 | |
| 136 | - Test Name (test_name) |
| 137 | - Test Datatype (test_datatype) |
| 138 | - Expected Datatype (res_datatype) |
| 139 | - Result (result) |
| 140 | - File Name (file) |
| 141 | - Line Number (line) |
| 142 | - Any notes you entered for the test (notes) |
| 143 | |
| 144 | You can customize which of these items get displayed by using |
Daniel Paul Searles | 7ffaea4 | 2012-11-05 14:54:00 -0800 | [diff] [blame] | 145 | $this->unit->set_test_items(). For example, if you only wanted the test name |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 146 | and the result displayed: |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 147 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | Customizing displayed tests |
| 149 | --------------------------- |
| 150 | |
| 151 | :: |
| 152 | |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 153 | $this->unit->set_test_items(array('test_name', 'result')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
| 155 | Creating a Template |
| 156 | ------------------- |
| 157 | |
| 158 | If you would like your test results formatted differently then the |
| 159 | default you can set your own template. Here is an example of a simple |
| 160 | template. Note the required pseudo-variables:: |
| 161 | |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 162 | $str = ' |
| 163 | <table border="0" cellpadding="4" cellspacing="1"> |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 164 | {rows} |
| 165 | <tr> |
| 166 | <td>{item}</td> |
| 167 | <td>{result}</td> |
| 168 | </tr> |
| 169 | {/rows} |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 170 | </table>'; |
| 171 | |
| 172 | $this->unit->set_template($str); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
| 174 | .. note:: Your template must be declared **before** running the unit |
| 175 | test process. |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 176 | |
| 177 | *************** |
| 178 | Class Reference |
| 179 | *************** |
| 180 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 181 | .. php:class:: CI_Unit_test |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 182 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 183 | .. php:method:: set_test_items($items) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 184 | |
| 185 | :param array $items: List of visible test items |
| 186 | :returns: void |
| 187 | |
| 188 | Sets a list of items that should be visible in tests. |
| 189 | Valid options are: |
| 190 | |
| 191 | - test_name |
| 192 | - test_datatype |
| 193 | - res_datatype |
| 194 | - result |
| 195 | - file |
| 196 | - line |
| 197 | - notes |
| 198 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 199 | .. php:method:: run($test[, $expected = TRUE[, $test_name = 'undefined'[, $notes = '']]]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 200 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 201 | :param mixed $test: Test data |
| 202 | :param mixed $expected: Expected result |
| 203 | :param string $test_name: Test name |
| 204 | :param string $notes: Any notes to be attached to the test |
| 205 | :returns: Test report |
| 206 | :rtype: string |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 207 | |
| 208 | Runs unit tests. |
| 209 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 210 | .. php:method:: report([$result = array()]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 211 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 212 | :param array $result: Array containing tests results |
| 213 | :returns: Test report |
| 214 | :rtype: string |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 215 | |
| 216 | Generates a report about already complete tests. |
| 217 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 218 | .. php:method:: use_strict([$state = TRUE]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 219 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 220 | :param bool $state: Strict state flag |
| 221 | :rtype: void |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 222 | |
| 223 | Enables/disables strict type comparison in tests. |
| 224 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 225 | .. php:method:: active([$state = TRUE]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 226 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 227 | :param bool $state: Whether to enable testing |
| 228 | :rtype: void |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 229 | |
| 230 | Enables/disables unit testing. |
| 231 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 232 | .. php:method:: result([$results = array()]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 233 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 234 | :param array $results: Tests results list |
| 235 | :returns: Array of raw result data |
| 236 | :rtype: array |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 237 | |
| 238 | Returns raw tests results data. |
| 239 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 240 | .. php:method:: set_template($template) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 241 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 242 | :param string $template: Test result template |
| 243 | :rtype: void |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 244 | |
| 245 | Sets the template for displaying tests results. |