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 | |
| 14 | Initializing the Class |
| 15 | ====================== |
| 16 | |
| 17 | Like most other classes in CodeIgniter, the Unit Test class is |
| 18 | initialized in your controller using the $this->load->library function:: |
| 19 | |
| 20 | $this->load->library('unit_test'); |
| 21 | |
| 22 | Once loaded, the Unit Test object will be available using: $this->unit |
| 23 | |
| 24 | Running Tests |
| 25 | ============= |
| 26 | |
| 27 | Running a test involves supplying a test and an expected result to the |
| 28 | following function: |
| 29 | |
| 30 | $this->unit->run( test, expected result, 'test name', 'notes'); |
| 31 | =============================================================== |
| 32 | |
| 33 | Where test is the result of the code you wish to test, expected result |
| 34 | is the data type you expect, test name is an optional name you can give |
| 35 | your test, and notes are optional notes. Example:: |
| 36 | |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 37 | $test = 1 + 1; |
| 38 | |
| 39 | $expected_result = 2; |
| 40 | |
| 41 | $test_name = 'Adds one plus one'; |
| 42 | |
| 43 | $this->unit->run($test, $expected_result, $test_name); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
| 45 | The expected result you supply can either be a literal match, or a data |
| 46 | type match. Here's an example of a literal:: |
| 47 | |
| 48 | $this->unit->run('Foo', 'Foo'); |
| 49 | |
| 50 | Here is an example of a data type match:: |
| 51 | |
| 52 | $this->unit->run('Foo', 'is_string'); |
| 53 | |
| 54 | Notice the use of "is_string" in the second parameter? This tells the |
| 55 | function to evaluate whether your test is producing a string as the |
| 56 | result. Here is a list of allowed comparison types: |
| 57 | |
| 58 | - is_object |
| 59 | - is_string |
| 60 | - is_bool |
| 61 | - is_true |
| 62 | - is_false |
| 63 | - is_int |
| 64 | - is_numeric |
| 65 | - is_float |
| 66 | - is_double |
| 67 | - is_array |
| 68 | - is_null |
| 69 | |
| 70 | Generating Reports |
| 71 | ================== |
| 72 | |
| 73 | You can either display results after each test, or your can run several |
| 74 | tests and generate a report at the end. To show a report directly simply |
| 75 | echo or return the run function:: |
| 76 | |
| 77 | echo $this->unit->run($test, $expected_result); |
| 78 | |
| 79 | To run a full report of all tests, use this:: |
| 80 | |
| 81 | echo $this->unit->report(); |
| 82 | |
| 83 | The report will be formatted in an HTML table for viewing. If you prefer |
| 84 | the raw data you can retrieve an array using:: |
| 85 | |
| 86 | echo $this->unit->result(); |
| 87 | |
| 88 | Strict Mode |
| 89 | =========== |
| 90 | |
| 91 | By default the unit test class evaluates literal matches loosely. |
| 92 | Consider this example:: |
| 93 | |
| 94 | $this->unit->run(1, TRUE); |
| 95 | |
| 96 | The test is evaluating an integer, but the expected result is a boolean. |
| 97 | PHP, however, due to it's loose data-typing will evaluate the above code |
| 98 | as TRUE using a normal equality test:: |
| 99 | |
| 100 | if (1 == TRUE) echo 'This evaluates as true'; |
| 101 | |
| 102 | If you prefer, you can put the unit test class in to strict mode, which |
| 103 | will compare the data type as well as the value:: |
| 104 | |
| 105 | if (1 === TRUE) echo 'This evaluates as FALSE'; |
| 106 | |
| 107 | To enable strict mode use this:: |
| 108 | |
| 109 | $this->unit->use_strict(TRUE); |
| 110 | |
| 111 | Enabling/Disabling Unit Testing |
| 112 | =============================== |
| 113 | |
| 114 | If you would like to leave some testing in place in your scripts, but |
| 115 | not have it run unless you need it, you can disable unit testing using:: |
| 116 | |
| 117 | $this->unit->active(FALSE) |
| 118 | |
| 119 | Unit Test Display |
| 120 | ================= |
| 121 | |
| 122 | When your unit test results display, the following items show by |
| 123 | default: |
| 124 | |
| 125 | - Test Name (test_name) |
| 126 | - Test Datatype (test_datatype) |
| 127 | - Expected Datatype (res_datatype) |
| 128 | - Result (result) |
| 129 | - File Name (file) |
| 130 | - Line Number (line) |
| 131 | - Any notes you entered for the test (notes) |
| 132 | |
| 133 | You can customize which of these items get displayed by using |
| 134 | $this->unit->set_items(). For example, if you only wanted the test name |
| 135 | and the result displayed: |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 136 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 137 | Customizing displayed tests |
| 138 | --------------------------- |
| 139 | |
| 140 | :: |
| 141 | |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 142 | $this->unit->set_test_items(array('test_name', 'result')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
| 144 | Creating a Template |
| 145 | ------------------- |
| 146 | |
| 147 | If you would like your test results formatted differently then the |
| 148 | default you can set your own template. Here is an example of a simple |
| 149 | template. Note the required pseudo-variables:: |
| 150 | |
Derek Jones | 3185392 | 2011-10-05 15:34:21 -0500 | [diff] [blame] | 151 | $str = ' |
| 152 | <table border="0" cellpadding="4" cellspacing="1"> |
| 153 | {rows} |
| 154 | <tr> |
| 155 | <td>{item}</td> |
| 156 | <td>{result}</td> |
| 157 | </tr> |
| 158 | {/rows} |
| 159 | </table>'; |
| 160 | |
| 161 | $this->unit->set_template($str); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
| 163 | .. note:: Your template must be declared **before** running the unit |
| 164 | test process. |