blob: aa747587055b1faf1bba504e4db5586ad4c63d6b [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000013<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
admin10c3f412006-10-08 07:21:12 +000036<td><h1>Code Igniter User Guide Version 1.5.0b1</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Unit Testing Class
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64<h1>Unit Testing Class</h1>
65
66<p>Unit testing is an approach to software development in which tests are written for each function in your application.
67If you are not familiar with the concept you might do a little googling on the subject.</p>
68
69<p>Code Igniter's Unit Test class is quite simple, consisting of an evaluation function and two result functions.
70It's not intended to be a full-blown test suite but rather a simple mechanism to evaluate your code
71to determine if it is producing the correct data type and result.
72</p>
73
74
75<h2>Initializing the Class</h2>
76
77<p>Like most other classes in Code Igniter, the Unit Test class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
78
admin24dd7e72006-10-01 19:02:29 +000079<code>$this->load->library('unit');</code>
adminb0dd10f2006-08-25 17:25:49 +000080<p>Once loaded, the Unit Test object will be available using: <dfn>$this->unit</dfn></p>
adminb1fddc02006-10-09 21:29:07 +000081<p>You can also set your own class variable name. Please see the <a href="loader.html">Loader Class</a> for more info.</p>
adminb0dd10f2006-08-25 17:25:49 +000082
83
84<h2>Running Tests</h2>
85
86<p>Running a test involves supplying a test and an expected result to the following function:</p>
87
88<h2>$this->unit->run( <var>test</var>, <var>expected result</var>, '<var>test name</var>' );</h2>
89
90<p>Where <var>test</var> is the result of the code you wish to test,
91<var>expected result</var> is the data type you expect, and <var>test name</var> is an optional name you can give your test. Example:</p>
92
93<code>$test = 1 + 1;<br />
94<br />
95$expected_result = 2;<br />
96<br />
97$test_name = 'Adds one plus one';<br />
98<br />
99$this->unit->run($test, $expected_result, $test_name);</code>
100
101<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>
102
103<code>$this->unit->run('Foo', 'Foo');</code>
104
105<p>Here is an example of a data type match:</p>
106
107<code>$this->unit->run('Foo', 'is_string');</code>
108
109<p>Notice the use of "is_string" in the second parameter? This tells the function to evaluate whether your test is producing a string
110as the result. Here is a list of allowed comparison types:</p>
111
112<ul>
113<li>is_string</li>
114<li>is_bool</li>
115<li>is_true</li>
116<li>is_false</li>
117<li>is_int</li>
118<li>is_numeric</li>
119<li>is_float</li>
120<li>is_double</li>
121<li>is_array</li>
122<li>is_null</li>
123</ul>
124
125
126<h2>Generating Reports</h2>
127
128<p>You can either display results after each test, or your can run several tests and generate a report at the end.
129To show a report directly simply echo or return the <var>run</var> function:</p>
130
131<code>echo $this->unit->run($test, $expected_result);</code>
132
133<p>To run a full report of all tests, use this:</p>
134
135<code>echo $this->unit->report();</code>
136
137<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>
138
139<code>echo $this->unit->result();</code>
140
141
142<h2>Strict Mode</h2>
143
144<p>By default the unit test class evaluates literal matches loosely. Consider this example:</p>
145
146<code>$this->unit->run(1, TRUE);</code>
147
148<p>The test is evaluating an integer, but the expected result is a boolean. PHP, however, due to it's loose data-typing
149will evaluate the above code as TRUE using a normal equality test:</p>
150
151<code>if (1 == TRUE) echo 'This evaluates as true';</code>
152
153<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>
154
155<code>if (1 === TRUE) echo 'This evaluates as FALSE';</code>
156
157<p>To enable strict mode use this:</p>
158
159<code>$this->unit->strict(TRUE);</code>
160
161<h2>Enabling/Disabling Unit Testing</h2>
162
163<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
164unit testing using:</p>
165
166<code>$this->unit->active(FALSE)</code>
167
168
169
170<h2>Creating a Template</h2>
171
172<p>If you would like your test results formatted differently then the default you can set your own template. Here is an
173example of a simple template. Note the required pseudo-variables:</p>
174
175<code>
176$str = '<br />
177&lt;table border="0" cellpadding="4" cellspacing="1"><br />
178&nbsp;&nbsp;&nbsp;&nbsp;<kbd>{rows}</kbd><br />
179&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr><br />
180&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td><kbd>{item}</kbd>&lt;/td><br />
181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td><kbd>{result}</kbd>&lt;/td><br />
182&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr><br />
183&nbsp;&nbsp;&nbsp;&nbsp;<kbd>{/rows}</kbd><br />
184&lt;/table>';<br />
185<br />
186$this->unit->set_template($str);
187</code>
188
adminc47289a2006-09-17 18:09:42 +0000189<p class="important"><strong>Note:</strong> Your template must be declared <strong>before</strong> running the unit test process.</p>
adminb0dd10f2006-08-25 17:25:49 +0000190
191
192
193
194
195</div>
196<!-- END CONTENT -->
197
198
199<div id="footer">
200<p>
201Previous Topic:&nbsp;&nbsp;<a href="parser.html">Template Parser Class</a>
202&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
203<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
204<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
205Next Topic:&nbsp;&nbsp;<a href="validation.html">Validation Class</a>
206<p>
207<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
208</div>
209
210</body>
211</html>