Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>Unit Testing Class : CodeIgniter User Guide</title> |
| 7 | |
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style> |
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> |
| 10 | |
| 11 | <script type="text/javascript" src="../nav/nav.js"></script> |
| 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script> |
| 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script> |
| 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script> |
| 15 | |
| 16 | <meta http-equiv='expires' content='-1' /> |
| 17 | <meta http-equiv= 'pragma' content='no-cache' /> |
| 18 | <meta name='robots' content='all' /> |
| 19 | <meta name='author' content='ExpressionEngine Dev Team' /> |
| 20 | <meta name='description' content='CodeIgniter User Guide' /> |
| 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_darker.jpg" width="154" height="43" 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> |
Phil Sturgeon | 0e3263b | 2011-03-10 16:37:35 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 2.0.1</h1></td> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> |
| 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"> |
| 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 44 | <a href="../index.html">User Guide Home</a> › |
| 45 | Unit Testing Class |
| 46 | </td> |
| 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> |
| 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 | |
| 64 | <p>CodeIgniter's Unit Test class is quite simple, consisting of an evaluation function and two result functions. |
| 65 | It's not intended to be a full-blown test suite but rather a simple mechanism to evaluate your code |
| 66 | to determine if it is producing the correct data type and result. |
| 67 | </p> |
| 68 | |
| 69 | |
| 70 | <h2>Initializing the Class</h2> |
| 71 | |
| 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> |
| 73 | |
| 74 | <code>$this->load->library('unit_test');</code> |
| 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 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 82 | <h2>$this->unit->run( <var>test</var>, <var>expected result</var>, '<var>test name</var>', '<var>notes</var>');</h2> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | <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, |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 85 | <var>test name</var> is an optional name you can give your test, and <var>notes</var> are optional notes. Example:</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
| 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 |
| 104 | as the result. Here is a list of allowed comparison types:</p> |
| 105 | |
| 106 | <ul> |
Derek Allard | 82d6ec4 | 2009-12-22 13:53:53 +0000 | [diff] [blame] | 107 | <li>is_object</li> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | <li>is_string</li> |
| 109 | <li>is_bool</li> |
| 110 | <li>is_true</li> |
| 111 | <li>is_false</li> |
| 112 | <li>is_int</li> |
| 113 | <li>is_numeric</li> |
| 114 | <li>is_float</li> |
| 115 | <li>is_double</li> |
| 116 | <li>is_array</li> |
| 117 | <li>is_null</li> |
| 118 | </ul> |
| 119 | |
| 120 | |
| 121 | <h2>Generating Reports</h2> |
| 122 | |
| 123 | <p>You can either display results after each test, or your can run several tests and generate a report at the end. |
| 124 | To show a report directly simply echo or return the <var>run</var> function:</p> |
| 125 | |
| 126 | <code>echo $this->unit->run($test, $expected_result);</code> |
| 127 | |
| 128 | <p>To run a full report of all tests, use this:</p> |
| 129 | |
| 130 | <code>echo $this->unit->report();</code> |
| 131 | |
| 132 | <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> |
| 133 | |
| 134 | <code>echo $this->unit->result();</code> |
| 135 | |
| 136 | |
| 137 | <h2>Strict Mode</h2> |
| 138 | |
| 139 | <p>By default the unit test class evaluates literal matches loosely. Consider this example:</p> |
| 140 | |
| 141 | <code>$this->unit->run(1, TRUE);</code> |
| 142 | |
| 143 | <p>The test is evaluating an integer, but the expected result is a boolean. PHP, however, due to it's loose data-typing |
| 144 | will evaluate the above code as TRUE using a normal equality test:</p> |
| 145 | |
| 146 | <code>if (1 == TRUE) echo 'This evaluates as true';</code> |
| 147 | |
| 148 | <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> |
| 149 | |
| 150 | <code>if (1 === TRUE) echo 'This evaluates as FALSE';</code> |
| 151 | |
| 152 | <p>To enable strict mode use this:</p> |
| 153 | |
| 154 | <code>$this->unit->use_strict(TRUE);</code> |
| 155 | |
| 156 | <h2>Enabling/Disabling Unit Testing</h2> |
| 157 | |
| 158 | <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 |
| 159 | unit testing using:</p> |
| 160 | |
| 161 | <code>$this->unit->active(FALSE)</code> |
| 162 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 163 | <h2>Unit Test Display</h2> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 165 | <p>When your unit test results display, the following items show by default:</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | |
Derek Allard | 0ce73ef | 2010-01-18 15:48:25 +0000 | [diff] [blame] | 167 | <ul> |
| 168 | <li>Test Name (test_name)</li> |
| 169 | <li>Test Datatype (test_datatype)</li> |
| 170 | <li>Expected Datatype (res_datatype)</li> |
| 171 | <li>Result (result)</li> |
| 172 | <li>File Name (file)</li> |
| 173 | <li>Line Number (line)</li> |
| 174 | <li>Any notes you entered for the test (notes)</li> |
| 175 | </ul> |
| 176 | |
| 177 | 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> |
| 178 | |
| 179 | <h3>Customizing displayed tests</h3> |
| 180 | |
| 181 | <code> |
| 182 | $this->unit->set_test_items(array('test_name', 'result')); |
| 183 | </code> |
| 184 | |
| 185 | <h3>Creating a Template</h3> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | |
| 187 | <p>If you would like your test results formatted differently then the default you can set your own template. Here is an |
| 188 | example of a simple template. Note the required pseudo-variables:</p> |
| 189 | |
| 190 | <code> |
| 191 | $str = '<br /> |
| 192 | <table border="0" cellpadding="4" cellspacing="1"><br /> |
| 193 | <kbd>{rows}</kbd><br /> |
| 194 | <tr><br /> |
| 195 | <td><kbd>{item}</kbd></td><br /> |
| 196 | <td><kbd>{result}</kbd></td><br /> |
| 197 | </tr><br /> |
| 198 | <kbd>{/rows}</kbd><br /> |
| 199 | </table>';<br /> |
| 200 | <br /> |
| 201 | $this->unit->set_template($str); |
| 202 | </code> |
| 203 | |
| 204 | <p class="important"><strong>Note:</strong> Your template must be declared <strong>before</strong> running the unit test process.</p> |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | </div> |
| 211 | <!-- END CONTENT --> |
| 212 | |
| 213 | |
| 214 | <div id="footer"> |
| 215 | <p> |
| 216 | Previous Topic: <a href="typography.html">Typography Class</a> |
| 217 | · |
| 218 | <a href="#top">Top of Page</a> · |
| 219 | <a href="../index.html">User Guide Home</a> · |
| 220 | Next Topic: <a href="uri.html">URI Class</a> |
| 221 | </p> |
Derek Jones | 898949f | 2011-01-28 07:42:16 -0600 | [diff] [blame] | 222 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | </div> |
| 224 | |
| 225 | </body> |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 226 | </html> |