|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Simple Testing Example</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>Example: Simple Testing Example</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p>This example shows basic usage of the YUI Test framework for testing browser-based JavaScript code. |
|
|
32 |
Two different <a href="index.html#testcase"><code>TestCase</code></a> objects are created and added to a |
|
|
33 |
<a href="index.html#testsuite"><code>TestSuite</code></a> object. The <a href="index.html#testrunner"><code>TestRunner</code></a> |
|
|
34 |
is then used to run the tests once the page has loaded.</p> |
|
|
35 |
</div> |
|
|
36 |
|
|
|
37 |
<div class="example yui3-skin-sam"> |
|
|
38 |
<div id="testLogger"></div> |
|
|
39 |
|
|
|
40 |
<script> |
|
|
41 |
YUI().use('node', 'test-console', 'test', function (Y) { |
|
|
42 |
|
|
|
43 |
Y.namespace("example.test"); |
|
|
44 |
|
|
|
45 |
Y.example.test.DataTestCase = new Y.Test.Case({ |
|
|
46 |
|
|
|
47 |
//name of the test case - if not provided, one is auto-generated |
|
|
48 |
name : "Data Tests", |
|
|
49 |
|
|
|
50 |
//--------------------------------------------------------------------- |
|
|
51 |
// setUp and tearDown methods - optional |
|
|
52 |
//--------------------------------------------------------------------- |
|
|
53 |
|
|
|
54 |
/* |
|
|
55 |
* Sets up data that is needed by each test. |
|
|
56 |
*/ |
|
|
57 |
setUp : function () { |
|
|
58 |
this.data = { |
|
|
59 |
name: "test", |
|
|
60 |
year: 2007, |
|
|
61 |
beta: true |
|
|
62 |
}; |
|
|
63 |
}, |
|
|
64 |
|
|
|
65 |
/* |
|
|
66 |
* Cleans up everything that was created by setUp(). |
|
|
67 |
*/ |
|
|
68 |
tearDown : function () { |
|
|
69 |
delete this.data; |
|
|
70 |
}, |
|
|
71 |
|
|
|
72 |
//--------------------------------------------------------------------- |
|
|
73 |
// Test methods - names must begin with "test" |
|
|
74 |
//--------------------------------------------------------------------- |
|
|
75 |
|
|
|
76 |
testName : function () { |
|
|
77 |
var Assert = Y.Assert; |
|
|
78 |
|
|
|
79 |
Assert.isObject(this.data); |
|
|
80 |
Assert.isString(this.data.name); |
|
|
81 |
Assert.areEqual("test", this.data.name); |
|
|
82 |
}, |
|
|
83 |
|
|
|
84 |
testYear : function () { |
|
|
85 |
var Assert = Y.Assert; |
|
|
86 |
|
|
|
87 |
Assert.isObject(this.data); |
|
|
88 |
Assert.isNumber(this.data.year); |
|
|
89 |
Assert.areEqual(2007, this.data.year); |
|
|
90 |
}, |
|
|
91 |
|
|
|
92 |
testBeta : function () { |
|
|
93 |
var Assert = Y.Assert; |
|
|
94 |
|
|
|
95 |
Assert.isObject(this.data); |
|
|
96 |
Assert.isBoolean(this.data.beta); |
|
|
97 |
Assert.isTrue(this.data.beta); |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
}); |
|
|
101 |
|
|
|
102 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
103 |
|
|
|
104 |
//name of the test case - if not provided, one is auto-generated |
|
|
105 |
name : "Array Tests", |
|
|
106 |
|
|
|
107 |
//--------------------------------------------------------------------- |
|
|
108 |
// setUp and tearDown methods - optional |
|
|
109 |
//--------------------------------------------------------------------- |
|
|
110 |
|
|
|
111 |
/* |
|
|
112 |
* Sets up data that is needed by each test. |
|
|
113 |
*/ |
|
|
114 |
setUp : function () { |
|
|
115 |
this.data = [0,1,2,3,4] |
|
|
116 |
}, |
|
|
117 |
|
|
|
118 |
/* |
|
|
119 |
* Cleans up everything that was created by setUp(). |
|
|
120 |
*/ |
|
|
121 |
tearDown : function () { |
|
|
122 |
delete this.data; |
|
|
123 |
}, |
|
|
124 |
|
|
|
125 |
//--------------------------------------------------------------------- |
|
|
126 |
// Test methods - names must begin with "test" |
|
|
127 |
//--------------------------------------------------------------------- |
|
|
128 |
|
|
|
129 |
testPop : function () { |
|
|
130 |
var Assert = Y.Assert; |
|
|
131 |
|
|
|
132 |
var value = this.data.pop(); |
|
|
133 |
|
|
|
134 |
Assert.areEqual(4, this.data.length); |
|
|
135 |
Assert.areEqual(4, value); |
|
|
136 |
}, |
|
|
137 |
|
|
|
138 |
testPush : function () { |
|
|
139 |
var Assert = Y.Assert; |
|
|
140 |
|
|
|
141 |
this.data.push(5); |
|
|
142 |
|
|
|
143 |
Assert.areEqual(6, this.data.length); |
|
|
144 |
Assert.areEqual(5, this.data[5]); |
|
|
145 |
}, |
|
|
146 |
|
|
|
147 |
testSplice : function () { |
|
|
148 |
var Assert = Y.Assert; |
|
|
149 |
|
|
|
150 |
this.data.splice(2, 1, 6, 7); |
|
|
151 |
|
|
|
152 |
Assert.areEqual(6, this.data.length); |
|
|
153 |
Assert.areEqual(6, this.data[2]); |
|
|
154 |
Assert.areEqual(7, this.data[3]); |
|
|
155 |
} |
|
|
156 |
|
|
|
157 |
}); |
|
|
158 |
|
|
|
159 |
Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite"); |
|
|
160 |
Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase); |
|
|
161 |
Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase); |
|
|
162 |
|
|
|
163 |
//create the console |
|
|
164 |
(new Y.Test.Console({ |
|
|
165 |
newestOnTop : false, |
|
|
166 |
filters: { |
|
|
167 |
pass: true, |
|
|
168 |
fail: true |
|
|
169 |
} |
|
|
170 |
})).render('#testLogger'); |
|
|
171 |
|
|
|
172 |
Y.Test.Runner.add(Y.example.test.ExampleSuite); |
|
|
173 |
|
|
|
174 |
//run the tests |
|
|
175 |
Y.Test.Runner.run(); |
|
|
176 |
|
|
|
177 |
}); |
|
|
178 |
</script> |
|
|
179 |
|
|
|
180 |
</div> |
|
|
181 |
|
|
|
182 |
<h2 class="first" id="simple-test-example">Simple Test Example</h2> |
|
|
183 |
|
|
|
184 |
<p>This example begins by creating a namespace:</p> |
|
|
185 |
<pre class="code prettyprint">Y.namespace("example.test");</pre> |
|
|
186 |
|
|
|
187 |
<p>This namespace serves as the core object upon which others will be added (to prevent creating global objects).</p> |
|
|
188 |
|
|
|
189 |
<h3 id="creating-the-first-testcase">Creating the first TestCase</h3> |
|
|
190 |
|
|
|
191 |
<p>The first step is to create a new <code>Y.Test.Case</code> object called <code>DataTestCase</code>. |
|
|
192 |
To do so, using the <code>Y.Test.Case</code> constructor and pass in an object literal containing information about the tests to be run:</p> |
|
|
193 |
<pre class="code prettyprint">Y.example.test.DataTestCase = new Y.Test.Case({ |
|
|
194 |
|
|
|
195 |
//name of the test case - if not provided, one is auto-generated |
|
|
196 |
name : "Data Tests", |
|
|
197 |
|
|
|
198 |
//--------------------------------------------------------------------- |
|
|
199 |
// setUp and tearDown methods - optional |
|
|
200 |
//--------------------------------------------------------------------- |
|
|
201 |
|
|
|
202 |
/* |
|
|
203 |
* Sets up data that is needed by each test. |
|
|
204 |
*/ |
|
|
205 |
setUp : function () { |
|
|
206 |
this.data = { |
|
|
207 |
name: "test", |
|
|
208 |
year: 2007, |
|
|
209 |
beta: true |
|
|
210 |
}; |
|
|
211 |
}, |
|
|
212 |
|
|
|
213 |
/* |
|
|
214 |
* Cleans up everything that was created by setUp(). |
|
|
215 |
*/ |
|
|
216 |
tearDown : function () { |
|
|
217 |
delete this.data; |
|
|
218 |
}, |
|
|
219 |
|
|
|
220 |
//--------------------------------------------------------------------- |
|
|
221 |
// Test methods - names must begin with "test" |
|
|
222 |
//--------------------------------------------------------------------- |
|
|
223 |
|
|
|
224 |
testName : function () { |
|
|
225 |
var Assert = Y.Assert; |
|
|
226 |
|
|
|
227 |
Assert.isObject(this.data); |
|
|
228 |
Assert.isString(this.data.name); |
|
|
229 |
Assert.areEqual("test", this.data.name); |
|
|
230 |
}, |
|
|
231 |
|
|
|
232 |
testYear : function () { |
|
|
233 |
var Assert = Y.Assert; |
|
|
234 |
|
|
|
235 |
Assert.isObject(this.data); |
|
|
236 |
Assert.isNumber(this.data.year); |
|
|
237 |
Assert.areEqual(2007, this.data.year); |
|
|
238 |
}, |
|
|
239 |
|
|
|
240 |
testBeta : function () { |
|
|
241 |
var Assert = Y.Assert; |
|
|
242 |
|
|
|
243 |
Assert.isObject(this.data); |
|
|
244 |
Assert.isBoolean(this.data.beta); |
|
|
245 |
Assert.isTrue(this.data.beta); |
|
|
246 |
} |
|
|
247 |
|
|
|
248 |
});</pre> |
|
|
249 |
|
|
|
250 |
<p>The object literal passed into the constructor contains a number of different sections. The first section contains the <code>name</code> property, |
|
|
251 |
which is used to determine which <code>Y.Test.Case</code> is being executed. A name is necessary, so one is generated if it isn't specified.</p> |
|
|
252 |
<p>Next, the <code>setUp()</code> and <code>tearDown()</code> methods are included. The <code>setUp()</code> method is used in a <code>Y.Test.Case</code> |
|
|
253 |
to set up data that may be needed for tests to be completed. This method is called immediately before each test is executed. For this example, |
|
|
254 |
<code>setUp()</code> creates a data object. The <code>tearDown()</code> is responsible for undoing what was done in <code>setUp()</code>. It is |
|
|
255 |
run immediately after each test is run and, in this case, deletes the data object that was created by <code>setUp</code>. These methods are optional.</p> |
|
|
256 |
<p>The last section contains the actual tests to be run. Test method names must always begin with the word "test" (all lowercase) in order |
|
|
257 |
to differentiate them from other methods that may be added to the object.</p> |
|
|
258 |
<p>The first test in this object is <code>testName()</code>, which runs |
|
|
259 |
various assertions on <code>data.name</code>. Inside of this method, a shortcut to <code>Y.Assert</code> is set up and used to run three |
|
|
260 |
assertions: <code>isObject()</code> on <code>data</code>, <code>isString()</code> on <code>data.name</code> and <code>areEqual()</code> to compare |
|
|
261 |
<code>data.name</code> to the expected value, "test". These assertions are arranged in order from least-specific to most-specific, |
|
|
262 |
which is the recommended way to arrange your assertions. Basically, the third assertion is useless to run unless the second has passes and the second |
|
|
263 |
can't possibly pass unless the first passed. Both <code>isObject()</code> and <code>isString()</code> accept a single argument, which is the value |
|
|
264 |
to test (you could optionally include a failure message as a second argument, though this is not required). The <code>areEqual()</code> method |
|
|
265 |
expects two arguments, the first being the expected value ("test") and the second being the actual value (<code>data.name</code>).</p> |
|
|
266 |
<p>The second and third tests follow the same pattern as the first with the exception that they work with different data types. The <code>testYear()</code> |
|
|
267 |
method works with <code>data.year</code>, which is a number and so runs tests specifically for numbers (<code>areEqual()</code> can be used with |
|
|
268 |
all data types). The <code>testBeta()</code> method works with <code>data.beta</code>, which is a Boolean, and so it uses the <code>isTrue()</code> |
|
|
269 |
assertion instead of <code>areEqual()</code> (though it could also use <code>areEqual(true, this.data.beta)</code>).</p> |
|
|
270 |
|
|
|
271 |
<h3 id="creating-the-second-testcase">Creating the second TestCase</h3> |
|
|
272 |
|
|
|
273 |
<p>Although it's possible that you'll have only one <code>Y.Test.Case</code> object, typically there is more than one, and so this example includes |
|
|
274 |
a second <code>Y.Test.Case</code>. This one tests some of the built-in functions of the <code>Array</code> object:</p> |
|
|
275 |
<pre class="code prettyprint">Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
276 |
|
|
|
277 |
//name of the test case - if not provided, one is auto-generated |
|
|
278 |
name : "Array Tests", |
|
|
279 |
|
|
|
280 |
//--------------------------------------------------------------------- |
|
|
281 |
// setUp and tearDown methods - optional |
|
|
282 |
//--------------------------------------------------------------------- |
|
|
283 |
|
|
|
284 |
/* |
|
|
285 |
* Sets up data that is needed by each test. |
|
|
286 |
*/ |
|
|
287 |
setUp : function () { |
|
|
288 |
this.data = [0,1,2,3,4] |
|
|
289 |
}, |
|
|
290 |
|
|
|
291 |
/* |
|
|
292 |
* Cleans up everything that was created by setUp(). |
|
|
293 |
*/ |
|
|
294 |
tearDown : function () { |
|
|
295 |
delete this.data; |
|
|
296 |
}, |
|
|
297 |
|
|
|
298 |
//--------------------------------------------------------------------- |
|
|
299 |
// Test methods - names must begin with "test" |
|
|
300 |
//--------------------------------------------------------------------- |
|
|
301 |
|
|
|
302 |
testPop : function () { |
|
|
303 |
var Assert = Y.Assert; |
|
|
304 |
|
|
|
305 |
var value = this.data.pop(); |
|
|
306 |
|
|
|
307 |
Assert.areEqual(4, this.data.length); |
|
|
308 |
Assert.areEqual(4, value); |
|
|
309 |
}, |
|
|
310 |
|
|
|
311 |
testPush : function () { |
|
|
312 |
var Assert = Y.Assert; |
|
|
313 |
|
|
|
314 |
this.data.push(5); |
|
|
315 |
|
|
|
316 |
Assert.areEqual(6, this.data.length); |
|
|
317 |
Assert.areEqual(5, this.data[5]); |
|
|
318 |
}, |
|
|
319 |
|
|
|
320 |
testSplice : function () { |
|
|
321 |
var Assert = Y.Assert; |
|
|
322 |
|
|
|
323 |
this.data.splice(2, 1, 6, 7); |
|
|
324 |
|
|
|
325 |
Assert.areEqual(6, this.data.length); |
|
|
326 |
Assert.areEqual(6, this.data[2]); |
|
|
327 |
Assert.areEqual(7, this.data[3]); |
|
|
328 |
} |
|
|
329 |
|
|
|
330 |
});</pre> |
|
|
331 |
|
|
|
332 |
<p>As with the first <code>Y.Test.Case</code>, this one is split up into three sections: the name, the <code>setUp()</code> and <code>tearDown()</code> |
|
|
333 |
methods, and the test methods. The <code>setUp()</code> method in this <code>Y.Test.Case</code> creates an array of data to be used by the various |
|
|
334 |
tests while the <code>tearDown()</code> method destroys the array. The test methods are very simple, testing the <code>pop()</code>, <code>push()</code>, |
|
|
335 |
and <code>splice()</code> methods. Each test method uses <code>areEqual()</code> exclusively, to show the different ways that it can be used. |
|
|
336 |
The <code>testPop()</code> method calls <code>pop()</code> on the array of values, then verifies that the length of the array has changed and |
|
|
337 |
that the value popped off is 4; the <code>testPush()</code> pushes a new value (5) onto the array and then verifies that the length of the array has |
|
|
338 |
changed and that the value is included in the correct location; the <code>testSplice()</code> method tests <code>splice()</code> by removing one |
|
|
339 |
value that's already in the array and inserting two in its place.</p> |
|
|
340 |
|
|
|
341 |
<h3 id="creating-the-testsuite">Creating the TestSuite</h3> |
|
|
342 |
<p>To better organize the two <code>Y.Test.Case</code> objects, a <code>Y.Test.Suite</code> is created and those two <code>Y.Test.Case</code> objects are |
|
|
343 |
added to it:</p> |
|
|
344 |
<pre class="code prettyprint">Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite"); |
|
|
345 |
Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase); |
|
|
346 |
Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase);</pre> |
|
|
347 |
|
|
|
348 |
<p>The first line creates a new <code>Y.Test.Suite</code> object using its constructor, which accepts a single argument - the name of the suite. As with |
|
|
349 |
the name of a <code>Y.Test.Case</code>, the <code>Y.Test.Suite</code> name is used to determine where execution is when tests are being executed. Although |
|
|
350 |
not required (one is generated if it's not provided), it is recommended that you select a meaningful name to aid in debugging.</p> |
|
|
351 |
<p>Any number of <code>Y.Test.Case</code> and <code>Y.Test.Suite</code> objects can be added to a <code>Y.Test.Suite</code> by using the <code>add()</code> |
|
|
352 |
method. In this example, the two <code>Y.Test.Case</code> objects created earlier are added to the <code>Y.Test.Suite</code>.</p> |
|
|
353 |
|
|
|
354 |
<h3 id="running-the-tests">Running the tests</h3> |
|
|
355 |
|
|
|
356 |
<p>With all of the tests defined, the last step is to run them. This initialization is assigned to take place when all of the YUI |
|
|
357 |
components have been loaded:</p> |
|
|
358 |
|
|
|
359 |
<pre class="code prettyprint">//create the console |
|
|
360 |
(new Y.Test.Console({ |
|
|
361 |
verbose : true, |
|
|
362 |
newestOnTop : false |
|
|
363 |
})).render('#testLogger'); |
|
|
364 |
|
|
|
365 |
Y.Test.Runner.add(Y.example.test.ExampleSuite); |
|
|
366 |
|
|
|
367 |
//run the tests |
|
|
368 |
Y.Test.Runner.run();</pre> |
|
|
369 |
|
|
|
370 |
|
|
|
371 |
<p>Before running the tests, it's necessary to create a <code>Y.Test.Console</code> object to display the results (otherwise the tests would run |
|
|
372 |
but you wouldn't see the results). After that, the <code>Y.Test.Runner</code> is loaded with the <code>Y.Test.Suite</code> object by calling |
|
|
373 |
<code>add()</code> (any number of <code>Y.Test.Case</code> and <code>Y.Test.Suite</code> objects can be added to a <code>Y.Test.Runner</code>, |
|
|
374 |
this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its |
|
|
375 |
queue and displays the results in the <code>Y.Test.Console</code>.</p> |
|
|
376 |
|
|
|
377 |
<h2 id="complete-example-source">Complete Example Source</h2> |
|
|
378 |
|
|
|
379 |
<pre class="code prettyprint"><div id="testLogger"></div> |
|
|
380 |
|
|
|
381 |
<script> |
|
|
382 |
YUI().use('node', 'test-console', 'test', function (Y) { |
|
|
383 |
|
|
|
384 |
Y.namespace("example.test"); |
|
|
385 |
|
|
|
386 |
Y.example.test.DataTestCase = new Y.Test.Case({ |
|
|
387 |
|
|
|
388 |
//name of the test case - if not provided, one is auto-generated |
|
|
389 |
name : "Data Tests", |
|
|
390 |
|
|
|
391 |
//--------------------------------------------------------------------- |
|
|
392 |
// setUp and tearDown methods - optional |
|
|
393 |
//--------------------------------------------------------------------- |
|
|
394 |
|
|
|
395 |
/* |
|
|
396 |
* Sets up data that is needed by each test. |
|
|
397 |
*/ |
|
|
398 |
setUp : function () { |
|
|
399 |
this.data = { |
|
|
400 |
name: "test", |
|
|
401 |
year: 2007, |
|
|
402 |
beta: true |
|
|
403 |
}; |
|
|
404 |
}, |
|
|
405 |
|
|
|
406 |
/* |
|
|
407 |
* Cleans up everything that was created by setUp(). |
|
|
408 |
*/ |
|
|
409 |
tearDown : function () { |
|
|
410 |
delete this.data; |
|
|
411 |
}, |
|
|
412 |
|
|
|
413 |
//--------------------------------------------------------------------- |
|
|
414 |
// Test methods - names must begin with "test" |
|
|
415 |
//--------------------------------------------------------------------- |
|
|
416 |
|
|
|
417 |
testName : function () { |
|
|
418 |
var Assert = Y.Assert; |
|
|
419 |
|
|
|
420 |
Assert.isObject(this.data); |
|
|
421 |
Assert.isString(this.data.name); |
|
|
422 |
Assert.areEqual("test", this.data.name); |
|
|
423 |
}, |
|
|
424 |
|
|
|
425 |
testYear : function () { |
|
|
426 |
var Assert = Y.Assert; |
|
|
427 |
|
|
|
428 |
Assert.isObject(this.data); |
|
|
429 |
Assert.isNumber(this.data.year); |
|
|
430 |
Assert.areEqual(2007, this.data.year); |
|
|
431 |
}, |
|
|
432 |
|
|
|
433 |
testBeta : function () { |
|
|
434 |
var Assert = Y.Assert; |
|
|
435 |
|
|
|
436 |
Assert.isObject(this.data); |
|
|
437 |
Assert.isBoolean(this.data.beta); |
|
|
438 |
Assert.isTrue(this.data.beta); |
|
|
439 |
} |
|
|
440 |
|
|
|
441 |
}); |
|
|
442 |
|
|
|
443 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
444 |
|
|
|
445 |
//name of the test case - if not provided, one is auto-generated |
|
|
446 |
name : "Array Tests", |
|
|
447 |
|
|
|
448 |
//--------------------------------------------------------------------- |
|
|
449 |
// setUp and tearDown methods - optional |
|
|
450 |
//--------------------------------------------------------------------- |
|
|
451 |
|
|
|
452 |
/* |
|
|
453 |
* Sets up data that is needed by each test. |
|
|
454 |
*/ |
|
|
455 |
setUp : function () { |
|
|
456 |
this.data = [0,1,2,3,4] |
|
|
457 |
}, |
|
|
458 |
|
|
|
459 |
/* |
|
|
460 |
* Cleans up everything that was created by setUp(). |
|
|
461 |
*/ |
|
|
462 |
tearDown : function () { |
|
|
463 |
delete this.data; |
|
|
464 |
}, |
|
|
465 |
|
|
|
466 |
//--------------------------------------------------------------------- |
|
|
467 |
// Test methods - names must begin with "test" |
|
|
468 |
//--------------------------------------------------------------------- |
|
|
469 |
|
|
|
470 |
testPop : function () { |
|
|
471 |
var Assert = Y.Assert; |
|
|
472 |
|
|
|
473 |
var value = this.data.pop(); |
|
|
474 |
|
|
|
475 |
Assert.areEqual(4, this.data.length); |
|
|
476 |
Assert.areEqual(4, value); |
|
|
477 |
}, |
|
|
478 |
|
|
|
479 |
testPush : function () { |
|
|
480 |
var Assert = Y.Assert; |
|
|
481 |
|
|
|
482 |
this.data.push(5); |
|
|
483 |
|
|
|
484 |
Assert.areEqual(6, this.data.length); |
|
|
485 |
Assert.areEqual(5, this.data[5]); |
|
|
486 |
}, |
|
|
487 |
|
|
|
488 |
testSplice : function () { |
|
|
489 |
var Assert = Y.Assert; |
|
|
490 |
|
|
|
491 |
this.data.splice(2, 1, 6, 7); |
|
|
492 |
|
|
|
493 |
Assert.areEqual(6, this.data.length); |
|
|
494 |
Assert.areEqual(6, this.data[2]); |
|
|
495 |
Assert.areEqual(7, this.data[3]); |
|
|
496 |
} |
|
|
497 |
|
|
|
498 |
}); |
|
|
499 |
|
|
|
500 |
Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite"); |
|
|
501 |
Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase); |
|
|
502 |
Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase); |
|
|
503 |
|
|
|
504 |
//create the console |
|
|
505 |
(new Y.Test.Console({ |
|
|
506 |
newestOnTop : false, |
|
|
507 |
filters: { |
|
|
508 |
pass: true, |
|
|
509 |
fail: true |
|
|
510 |
} |
|
|
511 |
})).render('#testLogger'); |
|
|
512 |
|
|
|
513 |
Y.Test.Runner.add(Y.example.test.ExampleSuite); |
|
|
514 |
|
|
|
515 |
//run the tests |
|
|
516 |
Y.Test.Runner.run(); |
|
|
517 |
|
|
|
518 |
}); |
|
|
519 |
</script></pre> |
|
|
520 |
|
|
|
521 |
</div> |
|
|
522 |
</div> |
|
|
523 |
</div> |
|
|
524 |
|
|
|
525 |
<div class="yui3-u-1-4"> |
|
|
526 |
<div class="sidebar"> |
|
|
527 |
|
|
|
528 |
<div id="toc" class="sidebox"> |
|
|
529 |
<div class="hd"> |
|
|
530 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
531 |
</div> |
|
|
532 |
|
|
|
533 |
<div class="bd"> |
|
|
534 |
<ul class="toc"> |
|
|
535 |
<li> |
|
|
536 |
<a href="#simple-test-example">Simple Test Example</a> |
|
|
537 |
<ul class="toc"> |
|
|
538 |
<li> |
|
|
539 |
<a href="#creating-the-first-testcase">Creating the first TestCase</a> |
|
|
540 |
</li> |
|
|
541 |
<li> |
|
|
542 |
<a href="#creating-the-second-testcase">Creating the second TestCase</a> |
|
|
543 |
</li> |
|
|
544 |
<li> |
|
|
545 |
<a href="#creating-the-testsuite">Creating the TestSuite</a> |
|
|
546 |
</li> |
|
|
547 |
<li> |
|
|
548 |
<a href="#running-the-tests">Running the tests</a> |
|
|
549 |
</li> |
|
|
550 |
</ul> |
|
|
551 |
</li> |
|
|
552 |
<li> |
|
|
553 |
<a href="#complete-example-source">Complete Example Source</a> |
|
|
554 |
</li> |
|
|
555 |
</ul> |
|
|
556 |
</div> |
|
|
557 |
</div> |
|
|
558 |
|
|
|
559 |
|
|
|
560 |
|
|
|
561 |
<div class="sidebox"> |
|
|
562 |
<div class="hd"> |
|
|
563 |
<h2 class="no-toc">Examples</h2> |
|
|
564 |
</div> |
|
|
565 |
|
|
|
566 |
<div class="bd"> |
|
|
567 |
<ul class="examples"> |
|
|
568 |
|
|
|
569 |
|
|
|
570 |
<li data-description="Demonstrates basic usage of YUI Test for setting up and running tests."> |
|
|
571 |
<a href="test-simple-example.html">Simple Testing Example</a> |
|
|
572 |
</li> |
|
|
573 |
|
|
|
574 |
|
|
|
575 |
|
|
|
576 |
<li data-description="Demonstrates how to use advanced testing features such as defining tests that should fail, tests that should be ignored, and tests that should throw an error."> |
|
|
577 |
<a href="test-advanced-test-options.html">Advanced Test Options</a> |
|
|
578 |
</li> |
|
|
579 |
|
|
|
580 |
|
|
|
581 |
|
|
|
582 |
<li data-description="Demonstrates how to use the ArrayAssert object to test array data."> |
|
|
583 |
<a href="test-array-tests.html">Array Processing</a> |
|
|
584 |
</li> |
|
|
585 |
|
|
|
586 |
|
|
|
587 |
|
|
|
588 |
<li data-description="Demonstrates basic asynchronous tests."> |
|
|
589 |
<a href="test-async-test.html">Asynchronous Testing</a> |
|
|
590 |
</li> |
|
|
591 |
|
|
|
592 |
|
|
|
593 |
|
|
|
594 |
<li data-description="Demonstrates using events with asynchronous tests."> |
|
|
595 |
<a href="test-async-event-tests.html">Asynchronous Event Testing</a> |
|
|
596 |
</li> |
|
|
597 |
|
|
|
598 |
|
|
|
599 |
</ul> |
|
|
600 |
</div> |
|
|
601 |
</div> |
|
|
602 |
|
|
|
603 |
|
|
|
604 |
|
|
|
605 |
</div> |
|
|
606 |
</div> |
|
|
607 |
</div> |
|
|
608 |
</div> |
|
|
609 |
|
|
|
610 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
611 |
<script>prettyPrint();</script> |
|
|
612 |
|
|
|
613 |
<script> |
|
|
614 |
YUI.Env.Tests = { |
|
|
615 |
examples: [], |
|
|
616 |
project: '../assets', |
|
|
617 |
assets: '../assets/test', |
|
|
618 |
name: 'test-simple-example', |
|
|
619 |
title: 'Simple Testing Example', |
|
|
620 |
newWindow: '', |
|
|
621 |
auto: false |
|
|
622 |
}; |
|
|
623 |
YUI.Env.Tests.examples.push('test-simple-example'); |
|
|
624 |
YUI.Env.Tests.examples.push('test-advanced-test-options'); |
|
|
625 |
YUI.Env.Tests.examples.push('test-array-tests'); |
|
|
626 |
YUI.Env.Tests.examples.push('test-async-test'); |
|
|
627 |
YUI.Env.Tests.examples.push('test-async-event-tests'); |
|
|
628 |
|
|
|
629 |
</script> |
|
|
630 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
631 |
|
|
|
632 |
|
|
|
633 |
|
|
|
634 |
</body> |
|
|
635 |
</html> |