1. bdean
  2. CodeIgniter Reactor


CodeIgniter Reactor / user_guide / libraries / unit_testing.html

<!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">

<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' />


<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%">
<td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>

<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<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 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>

<br clear="all" />

<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.

<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>

<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>', '<var>notes</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,
<var>test name</var> is an optional name you can give your test, and <var>notes</var> are optional notes. 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>


<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>


<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>


<h2>Unit Test Display</h2>

<p>When your unit test results display, the following items show by default:</p>

	<li>Test Name (test_name)</li>
	<li>Test Datatype (test_datatype)</li>
	<li>Expected Datatype (res_datatype)</li>
	<li>Result (result)</li>
	<li>File Name (file)</li>
	<li>Line Number (line)</li>
	<li>Any notes you entered for the test (notes)</li>

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>

<h3>Customizing displayed tests</h3>

	$this->unit->set_test_items(array('test_name', 'result'));

<h3>Creating a Template</h3>

<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>

$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 />

<p class="important"><strong>Note:</strong> Your template must be declared <strong>before</strong> running the unit test process.</p>

<!-- END CONTENT -->

<div id="footer">
Previous Topic:&nbsp;&nbsp;<a href="typography.html">Typography Class</a>
<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><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>