admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
Derek Allard | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Unit Testing Class</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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.jpg" width="153" height="44" 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 Allard | 067f2cc | 2008-02-04 22:12:03 +0000 | [diff] [blame^] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.1</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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">
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Unit Testing Class
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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.
|
| 62 | If you are not familiar with the concept you might do a little googling on the subject.</p>
|
| 63 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 64 | <p>CodeIgniter's Unit Test class is quite simple, consisting of an evaluation function and two result functions.
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 65 | It's not intended to be a full-blown test suite but rather a simple mechanism to evaluate your code
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 66 | to determine if it is producing the correct data type and result.
|
| 67 | </p>
|
| 68 |
|
| 69 |
|
| 70 | <h2>Initializing the Class</h2>
|
| 71 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 73 |
|
admin | 8d3f977 | 2006-10-09 21:39:48 +0000 | [diff] [blame] | 74 | <code>$this->load->library('unit_test');</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 84 | <p>Where <var>test</var> is the result of the code you wish to test,
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 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
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 104 | as 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 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 122 | <p>You can either display results after each test, or your can run several tests and generate a report at the end.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 123 | To 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
|
| 143 | will 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 |
|
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 153 | <code>$this->unit->use_strict(TRUE);</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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
|
| 158 | unit 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
|
| 167 | example of a simple template. Note the required pseudo-variables:</p>
|
| 168 |
|
| 169 | <code>
|
| 170 | $str = '<br />
|
| 171 | <table border="0" cellpadding="4" cellspacing="1"><br />
|
| 172 | <kbd>{rows}</kbd><br />
|
| 173 | <tr><br />
|
| 174 | <td><kbd>{item}</kbd></td><br />
|
| 175 | <td><kbd>{result}</kbd></td><br />
|
| 176 | </tr><br />
|
| 177 | <kbd>{/rows}</kbd><br />
|
| 178 | </table>';<br />
|
| 179 | <br />
|
| 180 | $this->unit->set_template($str);
|
| 181 | </code>
|
| 182 |
|
admin | c47289a | 2006-09-17 18:09:42 +0000 | [diff] [blame] | 183 | <p class="important"><strong>Note:</strong> Your template must be declared <strong>before</strong> running the unit test process.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 | </div>
|
| 190 | <!-- END CONTENT -->
|
| 191 |
|
| 192 |
|
| 193 | <div id="footer">
|
| 194 | <p>
|
| 195 | Previous Topic: <a href="parser.html">Template Parser Class</a>
|
| 196 | ·
|
| 197 | <a href="#top">Top of Page</a> ·
|
| 198 | <a href="../index.html">User Guide Home</a> ·
|
Derek Allard | 9da4dbc | 2007-04-03 11:39:35 +0000 | [diff] [blame] | 199 | Next Topic: <a href="uri.html">URI Class</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 200 | </p>
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 201 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 202 | </div>
|
| 203 |
|
| 204 | </body>
|
| 205 | </html> |