summaryrefslogtreecommitdiffstats
path: root/user_guide/libraries/unit_testing.html
blob: 871007a5760c6c74f8fd489e699879ffbcaf44e0 (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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unit Testing Class : CodeIgniter User Guide</title>

<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />

<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='ExpressionEngine Dev Team' />
<meta name='description' content='CodeIgniter User Guide' />

</head>
<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>CodeIgniter User Guide Version 2.1.1</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->


<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Unit Testing Class
</td>
<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
</tr>
</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Unit Testing Class</h1>

<p>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.</p>

<p>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.
</p>


<h2>Initializing the Class</h2>

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

<code>$this->load->library('unit_test');</code>
<p>Once loaded, the Unit Test object will be available using: <dfn>$this->unit</dfn></p>


<h2>Running Tests</h2>

<p>Running a test involves supplying a test and an expected result to the following function:</p>

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

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

<code>$test = 1 + 1;<br />
<br />
$expected_result = 2;<br />
<br />
$test_name = 'Adds one plus one';<br />
<br />
$this->unit->run($test, $expected_result, $test_name);</code>

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

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

<p>Here is an example of a data type match:</p>

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

<p>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:</p>

<ul>
<li>is_object</li>
<li>is_string</li>
<li>is_bool</li>
<li>is_true</li>
<li>is_false</li>
<li>is_int</li>
<li>is_numeric</li>
<li>is_float</li>
<li>is_double</li>
<li>is_array</li>
<li>is_null</li>
</ul>


<h2>Generating Reports</h2>

<p>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 <var>run</var> function:</p>

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

<p>To run a full report of all tests, use this:</p>

<code>echo $this->unit->report();</code>

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

<code>echo $this->unit->result();</code>


<h2>Strict Mode</h2>

<p>By default the unit test class evaluates literal matches loosely.  Consider this example:</p>

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

<p>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:</p>

<code>if (1 == TRUE) echo 'This evaluates as true';</code>

<p>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:</p>

<code>if (1 === TRUE) echo 'This evaluates as FALSE';</code>

<p>To enable strict mode use this:</p>

<code>$this->unit->use_strict(TRUE);</code>

<h2>Enabling/Disabling Unit Testing</h2>

<p>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:</p>

<code>$this->unit->active(FALSE)</code>

<h2>Unit Test Display</h2>

<p>When your unit test results display, the following items show by default:</p>

<ul>
	<li>Test Name (test_name)</li>
	<li>Test Datatype (test_datatype)</li>
	<li>Expected Datatype (res_datatype)</li>
	<li>Result (result)</li>
	<li>File Name (file)</li>
	<li>Line Number (line)</li>
	<li>Any notes you entered for the test (notes)</li>
</ul>

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

<h3>Customizing displayed tests</h3>

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

<h3>Creating a Template</h3>

<p>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:</p>

<code>
$str = '<br />
&lt;table border="0" cellpadding="4" cellspacing="1"><br />
&nbsp;&nbsp;&nbsp;&nbsp;<kbd>{rows}</kbd><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td><kbd>{item}</kbd>&lt;/td><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td><kbd>{result}</kbd>&lt;/td><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr><br />
&nbsp;&nbsp;&nbsp;&nbsp;<kbd>{/rows}</kbd><br />
&lt;/table>';<br />
<br />
$this->unit->set_template($str);
</code>

<p class="important"><strong>Note:</strong> Your template must be declared <strong>before</strong> running the unit test process.</p>





</div>
<!-- END CONTENT -->


<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="typography.html">Typography Class</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="uri.html">URI Class</a>
</p>
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
</div>

</body>
</html>