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">
|
| 2 | <html>
|
| 3 | <head>
|
| 4 |
|
| 5 | <title>Code Igniter User Guide</title>
|
| 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 | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../scripts/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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
| 15 | myHeight = new fx.Height('nav', {duration: 400});
|
| 16 | myHeight.hide();
|
| 17 | }
|
| 18 | </script>
|
| 19 |
|
| 20 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 21 | <meta http-equiv='expires' content='-1' />
|
| 22 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 23 | <meta name='robots' content='all' />
|
| 24 | <meta name='author' content='Rick Ellis' />
|
| 25 | <meta name='description' content='Code Igniter User Guide' />
|
| 26 |
|
| 27 | </head>
|
| 28 | <body>
|
| 29 |
|
| 30 | <!-- START NAVIGATION -->
|
| 31 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 32 | <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>
|
| 33 | <div id="masthead">
|
| 34 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 35 | <tr>
|
admin | 41a1685 | 2006-09-26 18:17:19 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
|
| 38 | </tr>
|
| 39 | </table>
|
| 40 | </div>
|
| 41 | <!-- END NAVIGATION -->
|
| 42 |
|
| 43 |
|
| 44 | <!-- START BREADCRUMB -->
|
| 45 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 46 | <tr>
|
| 47 | <td id="breadcrumb">
|
| 48 | <a href="http://www.codeigniter.com/">Code Igniter Home</a> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Unit Testing Class
|
| 51 | </td>
|
| 52 | <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 <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>
|
| 53 | </tr>
|
| 54 | </table>
|
| 55 | <!-- END BREADCRUMB -->
|
| 56 |
|
| 57 | <br clear="all" />
|
| 58 |
|
| 59 |
|
| 60 | <!-- START CONTENT -->
|
| 61 | <div id="content">
|
| 62 |
|
| 63 |
|
| 64 | <h1>Unit Testing Class</h1>
|
| 65 |
|
| 66 | <p>Unit testing is an approach to software development in which tests are written for each function in your application.
|
| 67 | If you are not familiar with the concept you might do a little googling on the subject.</p>
|
| 68 |
|
| 69 | <p>Code Igniter's Unit Test class is quite simple, consisting of an evaluation function and two result functions.
|
| 70 | It's not intended to be a full-blown test suite but rather a simple mechanism to evaluate your code
|
| 71 | to determine if it is producing the correct data type and result.
|
| 72 | </p>
|
| 73 |
|
| 74 |
|
| 75 | <h2>Initializing the Class</h2>
|
| 76 |
|
| 77 | <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>
|
| 78 |
|
| 79 | <code>$this->load->library('unit_test');</code>
|
| 80 | <p>Once loaded, the Unit Test object will be available using: <dfn>$this->unit</dfn></p>
|
| 81 |
|
| 82 |
|
| 83 | <h2>Running Tests</h2>
|
| 84 |
|
| 85 | <p>Running a test involves supplying a test and an expected result to the following function:</p>
|
| 86 |
|
| 87 | <h2>$this->unit->run( <var>test</var>, <var>expected result</var>, '<var>test name</var>' );</h2>
|
| 88 |
|
| 89 | <p>Where <var>test</var> is the result of the code you wish to test,
|
| 90 | <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>
|
| 91 |
|
| 92 | <code>$test = 1 + 1;<br />
|
| 93 | <br />
|
| 94 | $expected_result = 2;<br />
|
| 95 | <br />
|
| 96 | $test_name = 'Adds one plus one';<br />
|
| 97 | <br />
|
| 98 | $this->unit->run($test, $expected_result, $test_name);</code>
|
| 99 |
|
| 100 | <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>
|
| 101 |
|
| 102 | <code>$this->unit->run('Foo', 'Foo');</code>
|
| 103 |
|
| 104 | <p>Here is an example of a data type match:</p>
|
| 105 |
|
| 106 | <code>$this->unit->run('Foo', 'is_string');</code>
|
| 107 |
|
| 108 | <p>Notice the use of "is_string" in the second parameter? This tells the function to evaluate whether your test is producing a string
|
| 109 | as the result. Here is a list of allowed comparison types:</p>
|
| 110 |
|
| 111 | <ul>
|
| 112 | <li>is_string</li>
|
| 113 | <li>is_bool</li>
|
| 114 | <li>is_true</li>
|
| 115 | <li>is_false</li>
|
| 116 | <li>is_int</li>
|
| 117 | <li>is_numeric</li>
|
| 118 | <li>is_float</li>
|
| 119 | <li>is_double</li>
|
| 120 | <li>is_array</li>
|
| 121 | <li>is_null</li>
|
| 122 | </ul>
|
| 123 |
|
| 124 |
|
| 125 | <h2>Generating Reports</h2>
|
| 126 |
|
| 127 | <p>You can either display results after each test, or your can run several tests and generate a report at the end.
|
| 128 | To show a report directly simply echo or return the <var>run</var> function:</p>
|
| 129 |
|
| 130 | <code>echo $this->unit->run($test, $expected_result);</code>
|
| 131 |
|
| 132 | <p>To run a full report of all tests, use this:</p>
|
| 133 |
|
| 134 | <code>echo $this->unit->report();</code>
|
| 135 |
|
| 136 | <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>
|
| 137 |
|
| 138 | <code>echo $this->unit->result();</code>
|
| 139 |
|
| 140 |
|
| 141 | <h2>Strict Mode</h2>
|
| 142 |
|
| 143 | <p>By default the unit test class evaluates literal matches loosely. Consider this example:</p>
|
| 144 |
|
| 145 | <code>$this->unit->run(1, TRUE);</code>
|
| 146 |
|
| 147 | <p>The test is evaluating an integer, but the expected result is a boolean. PHP, however, due to it's loose data-typing
|
| 148 | will evaluate the above code as TRUE using a normal equality test:</p>
|
| 149 |
|
| 150 | <code>if (1 == TRUE) echo 'This evaluates as true';</code>
|
| 151 |
|
| 152 | <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>
|
| 153 |
|
| 154 | <code>if (1 === TRUE) echo 'This evaluates as FALSE';</code>
|
| 155 |
|
| 156 | <p>To enable strict mode use this:</p>
|
| 157 |
|
| 158 | <code>$this->unit->strict(TRUE);</code>
|
| 159 |
|
| 160 | <h2>Enabling/Disabling Unit Testing</h2>
|
| 161 |
|
| 162 | <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
|
| 163 | unit testing using:</p>
|
| 164 |
|
| 165 | <code>$this->unit->active(FALSE)</code>
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 | <h2>Creating a Template</h2>
|
| 170 |
|
| 171 | <p>If you would like your test results formatted differently then the default you can set your own template. Here is an
|
| 172 | example of a simple template. Note the required pseudo-variables:</p>
|
| 173 |
|
| 174 | <code>
|
| 175 | $str = '<br />
|
| 176 | <table border="0" cellpadding="4" cellspacing="1"><br />
|
| 177 | <kbd>{rows}</kbd><br />
|
| 178 | <tr><br />
|
| 179 | <td><kbd>{item}</kbd></td><br />
|
| 180 | <td><kbd>{result}</kbd></td><br />
|
| 181 | </tr><br />
|
| 182 | <kbd>{/rows}</kbd><br />
|
| 183 | </table>';<br />
|
| 184 | <br />
|
| 185 | $this->unit->set_template($str);
|
| 186 | </code>
|
| 187 |
|
admin | c47289a | 2006-09-17 18:09:42 +0000 | [diff] [blame] | 188 | <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] | 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 | </div>
|
| 195 | <!-- END CONTENT -->
|
| 196 |
|
| 197 |
|
| 198 | <div id="footer">
|
| 199 | <p>
|
| 200 | Previous Topic: <a href="parser.html">Template Parser Class</a>
|
| 201 | ·
|
| 202 | <a href="#top">Top of Page</a> ·
|
| 203 | <a href="../index.html">User Guide Home</a> ·
|
| 204 | Next Topic: <a href="validation.html">Validation Class</a>
|
| 205 | <p>
|
| 206 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 207 | </div>
|
| 208 |
|
| 209 | </body>
|
| 210 | </html> |