1 <!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">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>Unit Testing Class : CodeIgniter User Guide</title>
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
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>
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' />
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>
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
36 <!-- END NAVIGATION -->
39 <!-- START BREADCRUMB -->
40 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
43 <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
44 <a href="../index.html">User Guide Home</a> ›
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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
50 <!-- END BREADCRUMB -->
55 <!-- START CONTENT -->
59 <h1>Unit Testing Class</h1>
61 <p>Unit testing is an approach to software development in which tests are written for each function in your application.
62 If you are not familiar with the concept you might do a little googling on the subject.</p>
64 <p>CodeIgniter's Unit Test class is quite simple, consisting of an evaluation function and two result functions.
65 It's not intended to be a full-blown test suite but rather a simple mechanism to evaluate your code
66 to determine if it is producing the correct data type and result.
70 <h2>Initializing the Class</h2>
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>
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>
78 <h2>Running Tests</h2>
80 <p>Running a test involves supplying a test and an expected result to the following function:</p>
82 <h2>$this->unit->run( <var>test</var>, <var>expected result</var>, '<var>test name</var>', '<var>notes</var>');</h2>
84 <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,
85 <var>test name</var> is an optional name you can give your test, and <var>notes</var> are optional notes. Example:</p>
87 <code>$test = 1 + 1;<br />
89 $expected_result = 2;<br />
91 $test_name = 'Adds one plus one';<br />
93 $this->unit->run($test, $expected_result, $test_name);</code>
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>
97 <code>$this->unit->run('Foo', 'Foo');</code>
99 <p>Here is an example of a data type match:</p>
101 <code>$this->unit->run('Foo', 'is_string');</code>
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
104 as the result. Here is a list of allowed comparison types:</p>
121 <h2>Generating Reports</h2>
123 <p>You can either display results after each test, or your can run several tests and generate a report at the end.
124 To show a report directly simply echo or return the <var>run</var> function:</p>
126 <code>echo $this->unit->run($test, $expected_result);</code>
128 <p>To run a full report of all tests, use this:</p>
130 <code>echo $this->unit->report();</code>
132 <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>
134 <code>echo $this->unit->result();</code>
139 <p>By default the unit test class evaluates literal matches loosely. Consider this example:</p>
141 <code>$this->unit->run(1, TRUE);</code>
143 <p>The test is evaluating an integer, but the expected result is a boolean. PHP, however, due to it's loose data-typing
144 will evaluate the above code as TRUE using a normal equality test:</p>
146 <code>if (1 == TRUE) echo 'This evaluates as true';</code>
148 <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>
150 <code>if (1 === TRUE) echo 'This evaluates as FALSE';</code>
152 <p>To enable strict mode use this:</p>
154 <code>$this->unit->use_strict(TRUE);</code>
156 <h2>Enabling/Disabling Unit Testing</h2>
158 <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
159 unit testing using:</p>
161 <code>$this->unit->active(FALSE)</code>
163 <h2>Unit Test Display</h2>
165 <p>When your unit test results display, the following items show by default:</p>
168 <li>Test Name (test_name)</li>
169 <li>Test Datatype (test_datatype)</li>
170 <li>Expected Datatype (res_datatype)</li>
171 <li>Result (result)</li>
172 <li>File Name (file)</li>
173 <li>Line Number (line)</li>
174 <li>Any notes you entered for the test (notes)</li>
177 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>
179 <h3>Customizing displayed tests</h3>
182 $this->unit->set_test_items(array('test_name', 'result'));
185 <h3>Creating a Template</h3>
187 <p>If you would like your test results formatted differently then the default you can set your own template. Here is an
188 example of a simple template. Note the required pseudo-variables:</p>
192 <table border="0" cellpadding="4" cellspacing="1"><br />
193 <kbd>{rows}</kbd><br />
194 <tr><br />
195 <td><kbd>{item}</kbd></td><br />
196 <td><kbd>{result}</kbd></td><br />
197 </tr><br />
198 <kbd>{/rows}</kbd><br />
201 $this->unit->set_template($str);
204 <p class="important"><strong>Note:</strong> Your template must be declared <strong>before</strong> running the unit test process.</p>
216 Previous Topic: <a href="typography.html">Typography Class</a>
217 ·
218 <a href="#top">Top of Page</a> ·
219 <a href="../index.html">User Guide Home</a> ·
220 Next Topic: <a href="uri.html">URI Class</a>
222 <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p>