blob: 182e29e31108c564b968ba0b620ac5d1c72a1076 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Code Igniter 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="../scripts/nav.js"></script>
<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
<script type="text/javascript" src="../scripts/moo.fx.js"></script>
<script type="text/javascript">
window.onload = function() {
myHeight = new fx.Height('nav', {duration: 400});
myHeight.hide();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='Rick Ellis' />
<meta name='description' content='Code Igniter 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.jpg" width="153" height="44" 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>Code Igniter User Guide Version 1.4.0</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</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://www.codeigniter.com/">Code Igniter 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="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>
</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>Code Igniter'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 Code Igniter, 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->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>
</div>
<!-- END CONTENT -->
<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="parser.html">Template Parser 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="validation.html">Validation Class</a>
<p>
<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>
</div>
</body>
</html>