summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/libraries/unit_testing.rst
blob: 026781cb72ec1bc200a81da5155bed2b676d8b54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
##################
Unit Testing Class
##################

Unit testing is an approach to software development in which tests are
written for each function in your application. If you are not familiar
with the concept you might do a little googling on the subject.

CodeIgniter's Unit Test class is quite simple, consisting of an
evaluation function and two result functions. It's not intended to be a
full-blown test suite but rather a simple mechanism to evaluate your
code to determine if it is producing the correct data type and result.

.. contents::
  :local:

.. raw:: html

  <div class="custom-index container"></div>

******************************
Using the Unit Testing Library
******************************

Initializing the Class
======================

Like most other classes in CodeIgniter, the Unit Test class is
initialized in your controller using the $this->load->library function::

	$this->load->library('unit_test');

Once loaded, the Unit Test object will be available using ``$this->unit``

Running Tests
=============

Running a test involves supplying a test and an expected result in the
following way:

	$this->unit->run('test', 'expected result', 'test name', 'notes');

Where test is the result of the code you wish to test, expected result
is the data type you expect, test name is an optional name you can give
your test, and notes are optional notes. Example::

	$test = 1 + 1;

	$expected_result = 2;

	$test_name = 'Adds one plus one';

	$this->unit->run($test, $expected_result, $test_name);

The expected result you supply can either be a literal match, or a data
type match. Here's an example of a literal::

	$this->unit->run('Foo', 'Foo');

Here is an example of a data type match::

	$this->unit->run('Foo', 'is_string');

Notice the use of "is_string" in the second parameter? This tells the
function to evaluate whether your test is producing a string as the
result. Here is a list of allowed comparison types:

-  is_object
-  is_string
-  is_bool
-  is_true
-  is_false
-  is_int
-  is_numeric
-  is_float
-  is_double
-  is_array
-  is_null

Generating Reports
==================

You can either display results after each test, or your can run several
tests and generate a report at the end. To show a report directly simply
echo or return the run function::

	echo $this->unit->run($test, $expected_result);

To run a full report of all tests, use this::

	echo $this->unit->report();

The report will be formatted in an HTML table for viewing. If you prefer
the raw data you can retrieve an array using::

	echo $this->unit->result();

Strict Mode
===========

By default the unit test class evaluates literal matches loosely.
Consider this example::

	$this->unit->run(1, TRUE);

The test is evaluating an integer, but the expected result is a boolean.
PHP, however, due to it's loose data-typing will evaluate the above code
as TRUE using a normal equality test::

	if (1 == TRUE) echo 'This evaluates as true';

If you prefer, you can put the unit test class in to strict mode, which
will compare the data type as well as the value::

	if (1 === TRUE) echo 'This evaluates as FALSE';

To enable strict mode use this::

	$this->unit->use_strict(TRUE);

Enabling/Disabling Unit Testing
===============================

If you would like to leave some testing in place in your scripts, but
not have it run unless you need it, you can disable unit testing using::

	$this->unit->active(FALSE);

Unit Test Display
=================

When your unit test results display, the following items show by
default:

-  Test Name (test_name)
-  Test Datatype (test_datatype)
-  Expected Datatype (res_datatype)
-  Result (result)
-  File Name (file)
-  Line Number (line)
-  Any notes you entered for the test (notes)

You can customize which of these items get displayed by using
$this->unit->set_test_items(). For example, if you only wanted the test name
and the result displayed:

Customizing displayed tests
---------------------------

::

	$this->unit->set_test_items(array('test_name', 'result'));

Creating a Template
-------------------

If you would like your test results formatted differently then the
default you can set your own template. Here is an example of a simple
template. Note the required pseudo-variables::

	$str = '
	<table border="0" cellpadding="4" cellspacing="1">
	{rows}
		<tr>
			<td>{item}</td>
			<td>{result}</td>
		</tr>
	{/rows}
	</table>';

	$this->unit->set_template($str);

.. note:: Your template must be declared **before** running the unit
	test process.

***************
Class Reference
***************

.. php:class:: CI_Unit_test

	.. php:method:: set_test_items($items)

		:param array $items: List of visible test items
		:returns: void

		Sets a list of items that should be visible in tests.
		Valid options are:

		  - test_name
		  - test_datatype
		  - res_datatype
		  - result
		  - file
		  - line
		  - notes

	.. php:method:: run($test[, $expected = TRUE[, $test_name = 'undefined'[, $notes = '']]])

		:param	mixed	$test: Test data
		:param	mixed	$expected: Expected result
		:param	string	$test_name: Test name
		:param	string	$notes: Any notes to be attached to the test
		:returns:	Test report
		:rtype:	string

		Runs unit tests.

	.. php:method:: report([$result = array()])

		:param	array	$result: Array containing tests results
		:returns:	Test report
		:rtype:	string

		Generates a report about already complete tests.

	.. php:method:: use_strict([$state = TRUE])

		:param	bool	$state: Strict state flag
		:rtype:	void

		Enables/disables strict type comparison in tests.

	.. php:method:: active([$state = TRUE])

		:param	bool	$state: Whether to enable testing
		:rtype:	void

		Enables/disables unit testing.

	.. php:method:: result([$results = array()])

		:param	array	$results: Tests results list
		:returns:	Array of raw result data
		:rtype:	array

		Returns raw tests results data.

	.. php:method:: set_template($template)

		:param	string	$template: Test result template
		:rtype:	void

		Sets the template for displaying tests results.