blob: 0bc860f617006811f84b2d031ff8c8d90b9eea06 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################
2Unit Testing Class
3##################
4
5Unit testing is an approach to software development in which tests are
6written for each function in your application. If you are not familiar
7with the concept you might do a little googling on the subject.
8
9CodeIgniter's Unit Test class is quite simple, consisting of an
10evaluation function and two result functions. It's not intended to be a
11full-blown test suite but rather a simple mechanism to evaluate your
12code to determine if it is producing the correct data type and result.
13
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +020014.. contents::
15 :local:
16
17.. raw:: html
18
19 <div class="custom-index container"></div>
20
Derek Jones8ede1a22011-10-05 13:34:52 -050021Initializing the Class
22======================
23
24Like most other classes in CodeIgniter, the Unit Test class is
25initialized in your controller using the $this->load->library function::
26
27 $this->load->library('unit_test');
28
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +020029Once loaded, the Unit Test object will be available using ``$this->unit``
Derek Jones8ede1a22011-10-05 13:34:52 -050030
31Running Tests
32=============
33
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +020034Running a test involves supplying a test and an expected result in the
35following way:
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +020037 $this->unit->run('test', 'expected result', 'test name', 'notes');
Derek Jones8ede1a22011-10-05 13:34:52 -050038
39Where test is the result of the code you wish to test, expected result
40is the data type you expect, test name is an optional name you can give
41your test, and notes are optional notes. Example::
42
Derek Jones31853922011-10-05 15:34:21 -050043 $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 Jones8ede1a22011-10-05 13:34:52 -050050
51The expected result you supply can either be a literal match, or a data
52type match. Here's an example of a literal::
53
54 $this->unit->run('Foo', 'Foo');
55
56Here is an example of a data type match::
57
58 $this->unit->run('Foo', 'is_string');
59
60Notice the use of "is_string" in the second parameter? This tells the
61function to evaluate whether your test is producing a string as the
62result. 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
76Generating Reports
77==================
78
79You can either display results after each test, or your can run several
80tests and generate a report at the end. To show a report directly simply
81echo or return the run function::
82
83 echo $this->unit->run($test, $expected_result);
84
85To run a full report of all tests, use this::
86
87 echo $this->unit->report();
88
89The report will be formatted in an HTML table for viewing. If you prefer
90the raw data you can retrieve an array using::
91
92 echo $this->unit->result();
93
94Strict Mode
95===========
96
97By default the unit test class evaluates literal matches loosely.
98Consider this example::
99
100 $this->unit->run(1, TRUE);
101
102The test is evaluating an integer, but the expected result is a boolean.
103PHP, however, due to it's loose data-typing will evaluate the above code
104as TRUE using a normal equality test::
105
106 if (1 == TRUE) echo 'This evaluates as true';
107
108If you prefer, you can put the unit test class in to strict mode, which
109will compare the data type as well as the value::
110
111 if (1 === TRUE) echo 'This evaluates as FALSE';
112
113To enable strict mode use this::
114
115 $this->unit->use_strict(TRUE);
116
117Enabling/Disabling Unit Testing
118===============================
119
120If you would like to leave some testing in place in your scripts, but
121not have it run unless you need it, you can disable unit testing using::
122
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200123 $this->unit->active(FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
125Unit Test Display
126=================
127
128When your unit test results display, the following items show by
129default:
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
139You can customize which of these items get displayed by using
Daniel Paul Searles7ffaea42012-11-05 14:54:00 -0800140$this->unit->set_test_items(). For example, if you only wanted the test name
Derek Jones8ede1a22011-10-05 13:34:52 -0500141and the result displayed:
Derek Jones31853922011-10-05 15:34:21 -0500142
Derek Jones8ede1a22011-10-05 13:34:52 -0500143Customizing displayed tests
144---------------------------
145
146::
147
Derek Jones31853922011-10-05 15:34:21 -0500148 $this->unit->set_test_items(array('test_name', 'result'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500149
150Creating a Template
151-------------------
152
153If you would like your test results formatted differently then the
154default you can set your own template. Here is an example of a simple
155template. Note the required pseudo-variables::
156
Derek Jones31853922011-10-05 15:34:21 -0500157 $str = '
158 <table border="0" cellpadding="4" cellspacing="1">
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200159 {rows}
160 <tr>
161 <td>{item}</td>
162 <td>{result}</td>
163 </tr>
164 {/rows}
Derek Jones31853922011-10-05 15:34:21 -0500165 </table>';
166
167 $this->unit->set_template($str);
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
169.. note:: Your template must be declared **before** running the unit
170 test process.
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200171
172***************
173Class 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 Andreev28c2c972014-02-08 04:27:48 +0200196 :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 Andreevfd7d9ca2014-01-03 18:44:23 +0200202
203 Runs unit tests.
204
205 .. method:: report([$result = array()])
206
Andrey Andreev28c2c972014-02-08 04:27:48 +0200207 :param array $result: Array containing tests results
208 :returns: Test report
209 :rtype: string
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200210
211 Generates a report about already complete tests.
212
213 .. method:: use_strict([$state = TRUE])
214
Andrey Andreev28c2c972014-02-08 04:27:48 +0200215 :param bool $state: Strict state flag
216 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200217
218 Enables/disables strict type comparison in tests.
219
220 .. method:: active([$state = TRUE])
221
Andrey Andreev28c2c972014-02-08 04:27:48 +0200222 :param bool $state: Whether to enable testing
223 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200224
225 Enables/disables unit testing.
226
227 .. method:: result([$results = array()])
228
Andrey Andreev28c2c972014-02-08 04:27:48 +0200229 :param array $results: Tests results list
230 :returns: Array of raw result data
231 :rtype: array
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200232
233 Returns raw tests results data.
234
235 .. method:: set_template($template)
236
Andrey Andreev28c2c972014-02-08 04:27:48 +0200237 :param string $template: Test result template
238 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200239
240 Sets the template for displaying tests results.