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