| 1 |
lars |
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>
|
|
|
31 |
<td><h1>CodeIgniter User Guide Version 1.7.1</h1></td>
|
|
|
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 |
|
|
|
82 |
<h2>$this->unit->run( <var>test</var>, <var>expected result</var>, '<var>test name</var>' );</h2>
|
|
|
83 |
|
|
|
84 |
<p>Where <var>test</var> is the result of the code you wish to test,
|
|
|
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 |
|
|
|
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>
|
|
|
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 |
|
|
|
122 |
<p>You can either display results after each test, or your can run several tests and generate a report at the end.
|
|
|
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 |
|
|
|
153 |
<code>$this->unit->use_strict(TRUE);</code>
|
|
|
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 |
|
|
|
183 |
<p class="important"><strong>Note:</strong> Your template must be declared <strong>before</strong> running the unit test process.</p>
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
</div>
|
|
|
190 |
<!-- END CONTENT -->
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
<div id="footer">
|
|
|
194 |
<p>
|
|
|
195 |
Previous Topic: <a href="typography.html">Typography Class</a>
|
|
|
196 |
·
|
|
|
197 |
<a href="#top">Top of Page</a> ·
|
|
|
198 |
<a href="../index.html">User Guide Home</a> ·
|
|
|
199 |
Next Topic: <a href="uri.html">URI Class</a>
|
|
|
200 |
</p>
|
|
|
201 |
<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
|
|
202 |
</div>
|
|
|
203 |
|
|
|
204 |
</body>
|
|
|
205 |
</html>
|