blob: 57934cba3b9afd0183fff8f7e715a9440cbbe01a [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
ftwbzhaoa5ea5062015-07-06 13:18:11 +080079- is_resource
Derek Jones8ede1a22011-10-05 13:34:52 -050080
81Generating Reports
82==================
83
84You can either display results after each test, or your can run several
85tests and generate a report at the end. To show a report directly simply
86echo or return the run function::
87
88 echo $this->unit->run($test, $expected_result);
89
90To run a full report of all tests, use this::
91
92 echo $this->unit->report();
93
94The report will be formatted in an HTML table for viewing. If you prefer
95the raw data you can retrieve an array using::
96
97 echo $this->unit->result();
98
99Strict Mode
100===========
101
102By default the unit test class evaluates literal matches loosely.
103Consider this example::
104
105 $this->unit->run(1, TRUE);
106
107The test is evaluating an integer, but the expected result is a boolean.
108PHP, however, due to it's loose data-typing will evaluate the above code
109as TRUE using a normal equality test::
110
111 if (1 == TRUE) echo 'This evaluates as true';
112
113If you prefer, you can put the unit test class in to strict mode, which
114will compare the data type as well as the value::
115
116 if (1 === TRUE) echo 'This evaluates as FALSE';
117
118To enable strict mode use this::
119
120 $this->unit->use_strict(TRUE);
121
122Enabling/Disabling Unit Testing
123===============================
124
125If you would like to leave some testing in place in your scripts, but
126not have it run unless you need it, you can disable unit testing using::
127
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200128 $this->unit->active(FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130Unit Test Display
131=================
132
133When your unit test results display, the following items show by
134default:
135
136- Test Name (test_name)
137- Test Datatype (test_datatype)
138- Expected Datatype (res_datatype)
139- Result (result)
140- File Name (file)
141- Line Number (line)
142- Any notes you entered for the test (notes)
143
144You can customize which of these items get displayed by using
Daniel Paul Searles7ffaea42012-11-05 14:54:00 -0800145$this->unit->set_test_items(). For example, if you only wanted the test name
Derek Jones8ede1a22011-10-05 13:34:52 -0500146and the result displayed:
Derek Jones31853922011-10-05 15:34:21 -0500147
Derek Jones8ede1a22011-10-05 13:34:52 -0500148Customizing displayed tests
149---------------------------
150
151::
152
Derek Jones31853922011-10-05 15:34:21 -0500153 $this->unit->set_test_items(array('test_name', 'result'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
155Creating a Template
156-------------------
157
158If you would like your test results formatted differently then the
159default you can set your own template. Here is an example of a simple
160template. Note the required pseudo-variables::
161
Derek Jones31853922011-10-05 15:34:21 -0500162 $str = '
163 <table border="0" cellpadding="4" cellspacing="1">
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200164 {rows}
165 <tr>
166 <td>{item}</td>
167 <td>{result}</td>
168 </tr>
169 {/rows}
Derek Jones31853922011-10-05 15:34:21 -0500170 </table>';
171
172 $this->unit->set_template($str);
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
174.. note:: Your template must be declared **before** running the unit
175 test process.
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200176
177***************
178Class Reference
179***************
180
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200181.. php:class:: CI_Unit_test
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200182
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200183 .. php:method:: set_test_items($items)
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200184
185 :param array $items: List of visible test items
186 :returns: void
187
188 Sets a list of items that should be visible in tests.
189 Valid options are:
190
191 - test_name
192 - test_datatype
193 - res_datatype
194 - result
195 - file
196 - line
197 - notes
198
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200199 .. php:method:: run($test[, $expected = TRUE[, $test_name = 'undefined'[, $notes = '']]])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200200
Andrey Andreev28c2c972014-02-08 04:27:48 +0200201 :param mixed $test: Test data
202 :param mixed $expected: Expected result
203 :param string $test_name: Test name
204 :param string $notes: Any notes to be attached to the test
205 :returns: Test report
206 :rtype: string
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200207
208 Runs unit tests.
209
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200210 .. php:method:: report([$result = array()])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200211
Andrey Andreev28c2c972014-02-08 04:27:48 +0200212 :param array $result: Array containing tests results
213 :returns: Test report
214 :rtype: string
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200215
216 Generates a report about already complete tests.
217
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200218 .. php:method:: use_strict([$state = TRUE])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200219
Andrey Andreev28c2c972014-02-08 04:27:48 +0200220 :param bool $state: Strict state flag
221 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200222
223 Enables/disables strict type comparison in tests.
224
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200225 .. php:method:: active([$state = TRUE])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200226
Andrey Andreev28c2c972014-02-08 04:27:48 +0200227 :param bool $state: Whether to enable testing
228 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200229
230 Enables/disables unit testing.
231
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200232 .. php:method:: result([$results = array()])
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200233
Andrey Andreev28c2c972014-02-08 04:27:48 +0200234 :param array $results: Tests results list
235 :returns: Array of raw result data
236 :rtype: array
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200237
238 Returns raw tests results data.
239
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200240 .. php:method:: set_template($template)
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200241
Andrey Andreev28c2c972014-02-08 04:27:48 +0200242 :param string $template: Test result template
243 :rtype: void
Andrey Andreevfd7d9ca2014-01-03 18:44:23 +0200244
245 Sets the template for displaying tests results.