<!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 1.7.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>' );</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, and <var>test name</var> is an optional name you can give your test. 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_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>Creating a Template</h2>

<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-2009 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
</div>

</body>
</html>