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