blob: 4071ac1ed38cc2ff7240b6ff011bac901669c05a [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>
adminb0dd10f2006-08-25 17:25:49 +000011<script type="text/javascript" src="../scripts/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>
admin41a16852006-09-26 18:17:19 +000036<td><h1>Code Igniter User Guide Version 1.5.0</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
79<code>$this->load->library('unit_test');</code>
80<p>Once loaded, the Unit Test object will be available using: <dfn>$this->unit</dfn></p>
81
82
83<h2>Running Tests</h2>
84
85<p>Running a test involves supplying a test and an expected result to the following function:</p>
86
87<h2>$this->unit->run( <var>test</var>, <var>expected result</var>, '<var>test name</var>' );</h2>
88
89<p>Where <var>test</var> is the result of the code you wish to test,
90<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>
91
92<code>$test = 1 + 1;<br />
93<br />
94$expected_result = 2;<br />
95<br />
96$test_name = 'Adds one plus one';<br />
97<br />
98$this->unit->run($test, $expected_result, $test_name);</code>
99
100<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>
101
102<code>$this->unit->run('Foo', 'Foo');</code>
103
104<p>Here is an example of a data type match:</p>
105
106<code>$this->unit->run('Foo', 'is_string');</code>
107
108<p>Notice the use of "is_string" in the second parameter? This tells the function to evaluate whether your test is producing a string
109as the result. Here is a list of allowed comparison types:</p>
110
111<ul>
112<li>is_string</li>
113<li>is_bool</li>
114<li>is_true</li>
115<li>is_false</li>
116<li>is_int</li>
117<li>is_numeric</li>
118<li>is_float</li>
119<li>is_double</li>
120<li>is_array</li>
121<li>is_null</li>
122</ul>
123
124
125<h2>Generating Reports</h2>
126
127<p>You can either display results after each test, or your can run several tests and generate a report at the end.
128To show a report directly simply echo or return the <var>run</var> function:</p>
129
130<code>echo $this->unit->run($test, $expected_result);</code>
131
132<p>To run a full report of all tests, use this:</p>
133
134<code>echo $this->unit->report();</code>
135
136<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>
137
138<code>echo $this->unit->result();</code>
139
140
141<h2>Strict Mode</h2>
142
143<p>By default the unit test class evaluates literal matches loosely. Consider this example:</p>
144
145<code>$this->unit->run(1, TRUE);</code>
146
147<p>The test is evaluating an integer, but the expected result is a boolean. PHP, however, due to it's loose data-typing
148will evaluate the above code as TRUE using a normal equality test:</p>
149
150<code>if (1 == TRUE) echo 'This evaluates as true';</code>
151
152<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>
153
154<code>if (1 === TRUE) echo 'This evaluates as FALSE';</code>
155
156<p>To enable strict mode use this:</p>
157
158<code>$this->unit->strict(TRUE);</code>
159
160<h2>Enabling/Disabling Unit Testing</h2>
161
162<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
163unit testing using:</p>
164
165<code>$this->unit->active(FALSE)</code>
166
167
168
169<h2>Creating a Template</h2>
170
171<p>If you would like your test results formatted differently then the default you can set your own template. Here is an
172example of a simple template. Note the required pseudo-variables:</p>
173
174<code>
175$str = '<br />
176&lt;table border="0" cellpadding="4" cellspacing="1"><br />
177&nbsp;&nbsp;&nbsp;&nbsp;<kbd>{rows}</kbd><br />
178&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr><br />
179&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td><kbd>{item}</kbd>&lt;/td><br />
180&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td><kbd>{result}</kbd>&lt;/td><br />
181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr><br />
182&nbsp;&nbsp;&nbsp;&nbsp;<kbd>{/rows}</kbd><br />
183&lt;/table>';<br />
184<br />
185$this->unit->set_template($str);
186</code>
187
adminc47289a2006-09-17 18:09:42 +0000188<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 +0000189
190
191
192
193
194</div>
195<!-- END CONTENT -->
196
197
198<div id="footer">
199<p>
200Previous Topic:&nbsp;&nbsp;<a href="parser.html">Template Parser Class</a>
201&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
202<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
203<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
204Next Topic:&nbsp;&nbsp;<a href="validation.html">Validation Class</a>
205<p>
206<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>
207</div>
208
209</body>
210</html>