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 |
| 79 | |
| 80 | Generating Reports |
| 81 | ================== |
| 82 | |
| 83 | You can either display results after each test, or your can run several |
| 84 | tests and generate a report at the end. To show a report directly simply |
| 85 | echo or return the run function:: |
| 86 | |
| 87 | echo $this->unit->run($test, $expected_result); |
| 88 | |
| 89 | To run a full report of all tests, use this:: |
| 90 | |
| 91 | echo $this->unit->report(); |
| 92 | |
| 93 | The report will be formatted in an HTML table for viewing. If you prefer |
| 94 | the raw data you can retrieve an array using:: |
| 95 | |
| 96 | echo $this->unit->result(); |
| 97 | |
| 98 | Strict Mode |
| 99 | =========== |
| 100 | |
| 101 | By default the unit test class evaluates literal matches loosely. |
| 102 | Consider this example:: |
| 103 | |
| 104 | $this->unit->run(1, TRUE); |
| 105 | |
| 106 | The test is evaluating an integer, but the expected result is a boolean. |
| 107 | PHP, however, due to it's loose data-typing will evaluate the above code |
| 108 | as TRUE using a normal equality test:: |
| 109 | |
| 110 | if (1 == TRUE) echo 'This evaluates as true'; |
| 111 | |
| 112 | If you prefer, you can put the unit test class in to strict mode, which |
| 113 | will compare the data type as well as the value:: |
| 114 | |
| 115 | if (1 === TRUE) echo 'This evaluates as FALSE'; |
| 116 | |
| 117 | To enable strict mode use this:: |
| 118 | |
| 119 | $this->unit->use_strict(TRUE); |
| 120 | |
| 121 | Enabling/Disabling Unit Testing |
| 122 | =============================== |
| 123 | |
| 124 | If you would like to leave some testing in place in your scripts, but |
| 125 | not have it run unless you need it, you can disable unit testing using:: |
| 126 | |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 127 | $this->unit->active(FALSE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
| 129 | Unit Test Display |
| 130 | ================= |
| 131 | |
| 132 | When your unit test results display, the following items show by |
| 133 | default: |
| 134 | |
| 135 | - Test Name (test_name) |
| 136 | - Test Datatype (test_datatype) |
| 137 | - Expected Datatype (res_datatype) |
| 138 | - Result (result) |
| 139 | - File Name (file) |
| 140 | - Line Number (line) |
| 141 | - Any notes you entered for the test (notes) |
| 142 | |
| 143 | You can customize which of these items get displayed by using |
Daniel Paul Searles | 7ffaea4 | 2012-11-05 14:54:00 -0800 | [diff] [blame] | 144 | $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] | 145 | and the result displayed: |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 146 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | Customizing displayed tests |
| 148 | --------------------------- |
| 149 | |
| 150 | :: |
| 151 | |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 152 | $this->unit->set_test_items(array('test_name', 'result')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 153 | |
| 154 | Creating a Template |
| 155 | ------------------- |
| 156 | |
| 157 | If you would like your test results formatted differently then the |
| 158 | default you can set your own template. Here is an example of a simple |
| 159 | template. Note the required pseudo-variables:: |
| 160 | |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 161 | $str = ' |
| 162 | <table border="0" cellpadding="4" cellspacing="1"> |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 163 | {rows} |
| 164 | <tr> |
| 165 | <td>{item}</td> |
| 166 | <td>{result}</td> |
| 167 | </tr> |
| 168 | {/rows} |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 169 | </table>'; |
| 170 | |
| 171 | $this->unit->set_template($str); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
| 173 | .. note:: Your template must be declared **before** running the unit |
| 174 | test process. |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 175 | |
| 176 | *************** |
| 177 | Class Reference |
| 178 | *************** |
| 179 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 180 | .. php:class:: CI_Unit_test |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 181 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 182 | .. php:method:: set_test_items($items) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 183 | |
| 184 | :param array $items: List of visible test items |
| 185 | :returns: void |
| 186 | |
| 187 | Sets a list of items that should be visible in tests. |
| 188 | Valid options are: |
| 189 | |
| 190 | - test_name |
| 191 | - test_datatype |
| 192 | - res_datatype |
| 193 | - result |
| 194 | - file |
| 195 | - line |
| 196 | - notes |
| 197 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 198 | .. php:method:: run($test[, $expected = TRUE[, $test_name = 'undefined'[, $notes = '']]]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 199 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 200 | :param mixed $test: Test data |
| 201 | :param mixed $expected: Expected result |
| 202 | :param string $test_name: Test name |
| 203 | :param string $notes: Any notes to be attached to the test |
| 204 | :returns: Test report |
| 205 | :rtype: string |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 206 | |
| 207 | Runs unit tests. |
| 208 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 209 | .. php:method:: report([$result = array()]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 210 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 211 | :param array $result: Array containing tests results |
| 212 | :returns: Test report |
| 213 | :rtype: string |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 214 | |
| 215 | Generates a report about already complete tests. |
| 216 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 217 | .. php:method:: use_strict([$state = TRUE]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 218 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 219 | :param bool $state: Strict state flag |
| 220 | :rtype: void |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 221 | |
| 222 | Enables/disables strict type comparison in tests. |
| 223 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 224 | .. php:method:: active([$state = TRUE]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 225 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 226 | :param bool $state: Whether to enable testing |
| 227 | :rtype: void |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 228 | |
| 229 | Enables/disables unit testing. |
| 230 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 231 | .. php:method:: result([$results = array()]) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 232 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 233 | :param array $results: Tests results list |
| 234 | :returns: Array of raw result data |
| 235 | :rtype: array |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 236 | |
| 237 | Returns raw tests results data. |
| 238 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 239 | .. php:method:: set_template($template) |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 240 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 241 | :param string $template: Test result template |
| 242 | :rtype: void |
Andrey Andreev | fd7d9ca | 2014-01-03 18:44:23 +0200 | [diff] [blame] | 243 | |
| 244 | Sets the template for displaying tests results. |