blob: 026781cb72ec1bc200a81da5155bed2b676d8b54 [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
James L Parry08191be2014-12-20 02:37:13 -080021******************************
22Using the Unit Testing Library
23******************************
24
Derek Jones8ede1a22011-10-05 13:34:52 -050025Initializing the Class
26======================
27
28Like most other classes in CodeIgniter, the Unit Test class is
29initialized in your controller using the $this->load->library function::
30
31 $this->load->library('unit_test');
32
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +020033Once loaded, the Unit Test object will be available using ``$this->unit``
Derek Jones8ede1a22011-10-05 13:34:52 -050034
35Running Tests
36=============
37
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +020038Running a test involves supplying a test and an expected result in the
39following way:
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +020041 $this->unit->run('test', 'expected result', 'test name', 'notes');
Derek Jones8ede1a22011-10-05 13:34:52 -050042
43Where test is the result of the code you wish to test, expected result
44is the data type you expect, test name is an optional name you can give
45your test, and notes are optional notes. Example::
46
Derek Jones31853922011-10-05 15:34:21 -050047 $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 Jones8ede1a22011-10-05 13:34:52 -050054
55The expected result you supply can either be a literal match, or a data
56type match. Here's an example of a literal::
57
58 $this->unit->run('Foo', 'Foo');
59
60Here is an example of a data type match::
61
62 $this->unit->run('Foo', 'is_string');
63
64Notice the use of "is_string" in the second parameter? This tells the
65function to evaluate whether your test is producing a string as the
66result. 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
80Generating Reports
81==================
82
83You can either display results after each test, or your can run several
84tests and generate a report at the end. To show a report directly simply
85echo or return the run function::
86
87 echo $this->unit->run($test, $expected_result);
88
89To run a full report of all tests, use this::
90
91 echo $this->unit->report();
92
93The report will be formatted in an HTML table for viewing. If you prefer
94the raw data you can retrieve an array using::
95
96 echo $this->unit->result();
97
98Strict Mode
99===========
100
101By default the unit test class evaluates literal matches loosely.
102Consider this example::
103
104 $this->unit->run(1, TRUE);
105
106The test is evaluating an integer, but the expected result is a boolean.
107PHP, however, due to it's loose data-typing will evaluate the above code
108as TRUE using a normal equality test::
109
110 if (1 == TRUE) echo 'This evaluates as true';
111
112If you prefer, you can put the unit test class in to strict mode, which
113will compare the data type as well as the value::
114
115 if (1 === TRUE) echo 'This evaluates as FALSE';
116
117To enable strict mode use this::
118
119 $this->unit->use_strict(TRUE);
120
121Enabling/Disabling Unit Testing
122===============================
123
124If you would like to leave some testing in place in your scripts, but
125not have it run unless you need it, you can disable unit testing using::
126
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200127 $this->unit->active(FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
129Unit Test Display
130=================
131
132When your unit test results display, the following items show by
133default:
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
143You can customize which of these items get displayed by using
Daniel Paul Searles7ffaea42012-11-05 14:54:00 -0800144$this->unit->set_test_items(). For example, if you only wanted the test name
Derek Jones8ede1a22011-10-05 13:34:52 -0500145and the result displayed:
Derek Jones31853922011-10-05 15:34:21 -0500146
Derek Jones8ede1a22011-10-05 13:34:52 -0500147Customizing displayed tests
148---------------------------
149
150::
151
Derek Jones31853922011-10-05 15:34:21 -0500152 $this->unit->set_test_items(array('test_name', 'result'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
154Creating a Template
155-------------------
156
157If you would like your test results formatted differently then the
158default you can set your own template. Here is an example of a simple
159template. Note the required pseudo-variables::
160
Derek Jones31853922011-10-05 15:34:21 -0500161 $str = '
162 <table border="0" cellpadding="4" cellspacing="1">
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200163 {rows}
164 <tr>
165 <td>{item}</td>
166 <td>{result}</td>
167 </tr>
168 {/rows}
Derek Jones31853922011-10-05 15:34:21 -0500169 </table>';
170
171 $this->unit->set_template($str);
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
173.. note:: Your template must be declared **before** running the unit
174 test process.
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200175
176***************
177Class Reference
178***************
179
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200180.. php:class:: CI_Unit_test
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200181
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200182 .. php:method:: set_test_items($items)
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200183
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 Andreevcd3d9db2015-02-02 13:41:01 +0200198 .. php:method:: run($test[, $expected = TRUE[, $test_name = 'undefined'[, $notes = '']]])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200199
Andrey Andreev28c2c972014-02-08 04:27:48 +0200200 :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 Andreevfd7d9ca2014-01-03 18:44:23 +0200206
207 Runs unit tests.
208
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200209 .. php:method:: report([$result = array()])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200210
Andrey Andreev28c2c972014-02-08 04:27:48 +0200211 :param array $result: Array containing tests results
212 :returns: Test report
213 :rtype: string
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200214
215 Generates a report about already complete tests.
216
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200217 .. php:method:: use_strict([$state = TRUE])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200218
Andrey Andreev28c2c972014-02-08 04:27:48 +0200219 :param bool $state: Strict state flag
220 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200221
222 Enables/disables strict type comparison in tests.
223
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200224 .. php:method:: active([$state = TRUE])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200225
Andrey Andreev28c2c972014-02-08 04:27:48 +0200226 :param bool $state: Whether to enable testing
227 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200228
229 Enables/disables unit testing.
230
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200231 .. php:method:: result([$results = array()])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200232
Andrey Andreev28c2c972014-02-08 04:27:48 +0200233 :param array $results: Tests results list
234 :returns: Array of raw result data
235 :rtype: array
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200236
237 Returns raw tests results data.
238
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200239 .. php:method:: set_template($template)
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200240
Andrey Andreev28c2c972014-02-08 04:27:48 +0200241 :param string $template: Test result template
242 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200243
244 Sets the template for displaying tests results.