|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Advanced Test Options</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: Advanced Test Options</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 how to use advanced test options, which allow you to specify additional information about how a test should be run. |
|
|
32 |
Each <a href="index.html#testcase"><code>TestCase</code></a> can specify up to three different options for tests, |
|
|
33 |
including tests that should be ignored, tests that should throw an error, and tests that should fail.</p> |
|
|
34 |
</div> |
|
|
35 |
|
|
|
36 |
<div class="example yui3-skin-sam"> |
|
|
37 |
<div id="testLogger"></div> |
|
|
38 |
|
|
|
39 |
<script> |
|
|
40 |
YUI().use('node', 'test-console', 'test', function (Y) { |
|
|
41 |
|
|
|
42 |
Y.namespace("example.test"); |
|
|
43 |
|
|
|
44 |
Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
45 |
|
|
|
46 |
//the name of the test case - if not provided, one is automatically generated |
|
|
47 |
name: "Advanced Options Tests", |
|
|
48 |
|
|
|
49 |
/* |
|
|
50 |
* Specifies tests that "should" be doing something other than the expected. |
|
|
51 |
*/ |
|
|
52 |
_should: { |
|
|
53 |
|
|
|
54 |
/* |
|
|
55 |
* Tests listed in here should fail, meaning that if they fail, the test |
|
|
56 |
* has passed. This is used mostly for YuiTest to test itself, but may |
|
|
57 |
* be helpful in other cases. |
|
|
58 |
*/ |
|
|
59 |
fail: { |
|
|
60 |
|
|
|
61 |
//the test named "testFail" should fail |
|
|
62 |
testFail: true |
|
|
63 |
|
|
|
64 |
}, |
|
|
65 |
|
|
|
66 |
/* |
|
|
67 |
* Tests listed here should throw an error of some sort. If they throw an |
|
|
68 |
* error, then they are considered to have passed. |
|
|
69 |
*/ |
|
|
70 |
error: { |
|
|
71 |
|
|
|
72 |
/* |
|
|
73 |
* You can specify "true" for each test, in which case any error will |
|
|
74 |
* cause the test to pass. |
|
|
75 |
*/ |
|
|
76 |
testGenericError: true, |
|
|
77 |
|
|
|
78 |
/* |
|
|
79 |
* You can specify an error message, in which case the test passes only |
|
|
80 |
* if the error thrown matches the given message. |
|
|
81 |
*/ |
|
|
82 |
testStringError: "I'm a specific error message.", |
|
|
83 |
testStringError2: "I'm a specific error message.", |
|
|
84 |
|
|
|
85 |
/* |
|
|
86 |
* You can also specify an error object, in which case the test passes only |
|
|
87 |
* if the error thrown is on the same type and has the same message. |
|
|
88 |
*/ |
|
|
89 |
testObjectError: new TypeError("Number expected."), |
|
|
90 |
testObjectError2: new TypeError("Number expected."), |
|
|
91 |
testObjectError3: new TypeError("Number expected.") |
|
|
92 |
|
|
|
93 |
}, |
|
|
94 |
|
|
|
95 |
/* |
|
|
96 |
* Tests listed here should be ignored when the test case is run. For these tests, |
|
|
97 |
* setUp() and tearDown() are not called. |
|
|
98 |
*/ |
|
|
99 |
ignore : { |
|
|
100 |
|
|
|
101 |
testIgnore: true |
|
|
102 |
|
|
|
103 |
} |
|
|
104 |
}, |
|
|
105 |
|
|
|
106 |
//------------------------------------------------------------------------- |
|
|
107 |
// Basic tests - all method names must begin with "test" |
|
|
108 |
//------------------------------------------------------------------------- |
|
|
109 |
|
|
|
110 |
testFail : function() { |
|
|
111 |
|
|
|
112 |
//force a failure - but since this test "should" fail, it will pass |
|
|
113 |
Y.Assert.fail("Something bad happened."); |
|
|
114 |
|
|
|
115 |
}, |
|
|
116 |
|
|
|
117 |
testGenericError : function() { |
|
|
118 |
throw new Error("Generic error"); |
|
|
119 |
}, |
|
|
120 |
|
|
|
121 |
testStringError : function() { |
|
|
122 |
|
|
|
123 |
//throw a specific error message - this will pass because it "should" happen |
|
|
124 |
throw new Error("I'm a specific error message."); |
|
|
125 |
}, |
|
|
126 |
|
|
|
127 |
testStringError2 : function() { |
|
|
128 |
|
|
|
129 |
//throw a specific error message - this will fail because the message isn't expected |
|
|
130 |
throw new Error("I'm a specific error message, but a wrong one."); |
|
|
131 |
}, |
|
|
132 |
|
|
|
133 |
testObjectError : function() { |
|
|
134 |
|
|
|
135 |
//throw a specific error and message - this will pass because it "should" happen |
|
|
136 |
throw new TypeError("Number expected."); |
|
|
137 |
}, |
|
|
138 |
|
|
|
139 |
testObjectError2 : function() { |
|
|
140 |
|
|
|
141 |
//throw a specific error and message - this will fail because the type doesn't match |
|
|
142 |
throw new Error("Number expected."); |
|
|
143 |
}, |
|
|
144 |
|
|
|
145 |
testObjectError3 : function() { |
|
|
146 |
|
|
|
147 |
//throw a specific error and message - this will fail because the message doesn't match |
|
|
148 |
throw new TypeError("String expected."); |
|
|
149 |
}, |
|
|
150 |
|
|
|
151 |
testIgnore : function () { |
|
|
152 |
alert("You'll never see this."); |
|
|
153 |
} |
|
|
154 |
|
|
|
155 |
}); |
|
|
156 |
|
|
|
157 |
|
|
|
158 |
//create the console |
|
|
159 |
(new Y.Test.Console({ |
|
|
160 |
newestOnTop : false, |
|
|
161 |
style: 'block' // to anchor in the example content |
|
|
162 |
})).render('#testLogger'); |
|
|
163 |
|
|
|
164 |
Y.Test.Runner.add(Y.example.test.AdvancedOptionsTestCase); |
|
|
165 |
|
|
|
166 |
//run the tests |
|
|
167 |
Y.Test.Runner.run(); |
|
|
168 |
}); |
|
|
169 |
|
|
|
170 |
</script> |
|
|
171 |
|
|
|
172 |
</div> |
|
|
173 |
|
|
|
174 |
<h2 class="first" id="advanced-test-options">Advanced Test Options</h2> |
|
|
175 |
|
|
|
176 |
<p>This example begins by creating a namespace and a <code>Y.Test.Case</code> object:</p> |
|
|
177 |
<pre class="code prettyprint">Y.namespace("example.test"); |
|
|
178 |
Y.example.test.AdvancedOptionsTestCase = new Y.TestCase({ |
|
|
179 |
name: "Advanced Options Tests" |
|
|
180 |
});</pre> |
|
|
181 |
|
|
|
182 |
|
|
|
183 |
<p>This <code>Y.Test.Case</code> serves as the basis for this example.</p> |
|
|
184 |
|
|
|
185 |
<h3 id="using-_should">Using <code>_should</code></h3> |
|
|
186 |
|
|
|
187 |
<p>Immediately after the <code>name</code> of the <code>Y.Test.Case</code> is defined, there is a <code>_should</code> property. |
|
|
188 |
This property specifies information about how tests <em>should</em> behave and is defined as an object literal with one |
|
|
189 |
or more of the following properties: <code>fail</code>, <code>error</code>, and <code>ignore</code>.Each of these three |
|
|
190 |
is also defined as an object literal whose property names map directly to the names of test methods in the <code>Y.Test.Case</code>. |
|
|
191 |
This example uses all three properties:</p> |
|
|
192 |
<pre class="code prettyprint">Y.example.test.AdvancedOptionsTestCase = new Y.TestCase({ |
|
|
193 |
|
|
|
194 |
//the name of the test case - if not provided, one is automatically generated |
|
|
195 |
name: "Advanced Options Tests", |
|
|
196 |
|
|
|
197 |
/* |
|
|
198 |
* Specifies tests that "should" be doing something other than the expected. |
|
|
199 |
*/ |
|
|
200 |
_should: { |
|
|
201 |
|
|
|
202 |
/* |
|
|
203 |
* Tests listed in here should fail, meaning that if they fail, the test |
|
|
204 |
* has passed. This is used mostly for YuiTest to test itself, but may |
|
|
205 |
* be helpful in other cases. |
|
|
206 |
*/ |
|
|
207 |
fail: { |
|
|
208 |
|
|
|
209 |
//the test named "testFail" should fail |
|
|
210 |
testFail: true |
|
|
211 |
|
|
|
212 |
}, |
|
|
213 |
|
|
|
214 |
/* |
|
|
215 |
* Tests listed here should throw an error of some sort. If they throw an |
|
|
216 |
* error, then they are considered to have passed. |
|
|
217 |
*/ |
|
|
218 |
error: { |
|
|
219 |
|
|
|
220 |
/* |
|
|
221 |
* You can specify "true" for each test, in which case any error will |
|
|
222 |
* cause the test to pass. |
|
|
223 |
*/ |
|
|
224 |
testGenericError: true, |
|
|
225 |
|
|
|
226 |
/* |
|
|
227 |
* You can specify an error message, in which case the test passes only |
|
|
228 |
* if the error thrown matches the given message. |
|
|
229 |
*/ |
|
|
230 |
testStringError: "I'm a specific error message.", |
|
|
231 |
testStringError2: "I'm a specific error message.", |
|
|
232 |
|
|
|
233 |
/* |
|
|
234 |
* You can also specify an error object, in which case the test passes only |
|
|
235 |
* if the error thrown is on the same type and has the same message. |
|
|
236 |
*/ |
|
|
237 |
testObjectError: new TypeError("Number expected."), |
|
|
238 |
testObjectError2: new TypeError("Number expected."), |
|
|
239 |
testObjectError3: new TypeError("Number expected.") |
|
|
240 |
|
|
|
241 |
}, |
|
|
242 |
|
|
|
243 |
/* |
|
|
244 |
* Tests listed here should be ignored when the test case is run. For these tests, |
|
|
245 |
* setUp() and tearDown() are not called. |
|
|
246 |
*/ |
|
|
247 |
ignore : { |
|
|
248 |
|
|
|
249 |
testIgnore: true |
|
|
250 |
|
|
|
251 |
} |
|
|
252 |
}, |
|
|
253 |
|
|
|
254 |
... |
|
|
255 |
});</pre> |
|
|
256 |
|
|
|
257 |
|
|
|
258 |
<p>This <code>Y.Test.Case</code> specifies one test that should fail, six that should throw an error, and one that should be ignored.</p> |
|
|
259 |
<p>In the <code>fail</code> section, the test method <code>testFail()</code> is specified as one that should fail. By adding the |
|
|
260 |
property <code>testFail</code> and settings its value to true, the <code>Y.Test.Runner</code> knows that this test is expected to fail. |
|
|
261 |
If the test were to be run without failing, it would be considered a failure of the test. This feature is useful when testing |
|
|
262 |
YUI Test itself or addon components to YUI Test.</p> |
|
|
263 |
<p>Moving on to the <code>error</code> section, there are six tests specified that should throw an error. There are three different ways |
|
|
264 |
to indicate that a test is expected to throw an error. The first is simply to add a property with the same name as the test method |
|
|
265 |
and set its value equal to true (similar to specifying tests that should fail). In this example, the <code>testGenericError()</code> |
|
|
266 |
method is specified this way. When specified like this, the test passes regardless of the type of error that occurs. This can be |
|
|
267 |
dangerous since unexpected errors will also cause the test to pass. To be more specific, set the property value for the test method |
|
|
268 |
to an error message string. When a string is used instead of the Boolean true, the test passes only when an error is thrown and that |
|
|
269 |
error message matches the string. In this example, <code>testStringError()</code> and <code>testStringError2()</code> expect an error |
|
|
270 |
to be thrown with an error message of "I'm a specific error message." If any other error occurs inside of the these methods, |
|
|
271 |
the test will fail because the error message doesn't match. The last way to specify an error should occur is to create an actual error |
|
|
272 |
object, which is the case with <code>testObjectError()</code>, <code>testObjectError2()</code>, and <code>testObjectError3()</code>. |
|
|
273 |
When specified in this way, a test will pass only when an error is thrown whose constructor and error message match that of the |
|
|
274 |
error object.</p> |
|
|
275 |
<p>The last section is <code>ignore</code>, which determines tests that should be ignored. In this example, the method <code>testIgnore()</code> |
|
|
276 |
is set to be ignored when the <code>Y.Test.Case</code> is executed. Test in the <code>ignore</code> section are specified the same way |
|
|
277 |
as those in the <code>fail</code> section, by adding the name as a property and setting its value to true.</p> |
|
|
278 |
|
|
|
279 |
<h3 id="creating-the-test-methods">Creating the test methods</h3> |
|
|
280 |
|
|
|
281 |
<p>The next part of the example contains the actual test methods. Since each test method is specified as having a certain behavior in |
|
|
282 |
<code>_should</code>, they each do something to show their particular functionality.</p> |
|
|
283 |
<p>The first method is <code>testFail()</code>, which does nothing but purposely fail. Since this method is specified as one that should |
|
|
284 |
fail, it means that this test will pass:</p> |
|
|
285 |
<pre class="code prettyprint">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
286 |
|
|
|
287 |
//the name of the test case - if not provided, one is automatically generated |
|
|
288 |
name: "Advanced Options Tests", |
|
|
289 |
|
|
|
290 |
... |
|
|
291 |
|
|
|
292 |
testFail : function() { |
|
|
293 |
|
|
|
294 |
//force a failure - but since this test "should" fail, it will pass |
|
|
295 |
Y.Assert.fail("Something bad happened."); |
|
|
296 |
|
|
|
297 |
}, |
|
|
298 |
|
|
|
299 |
... |
|
|
300 |
});</pre> |
|
|
301 |
|
|
|
302 |
<p>This method uses <code>Assert.fail()</code> to force the test to fail. This type of method is helpful if you are creating your own |
|
|
303 |
type of assert methods that should fail when certain data is passed in.</p> |
|
|
304 |
<p>Next, the test methods that should error are defined. The <code>testGenericError()</code> method is specified as needing to throw |
|
|
305 |
an error to pass. In the <code>error</code> section, <code>testGenericError</code> is set to true, meaning that any error causes |
|
|
306 |
this method to pass. To illustrate this, the method simply throws an error:</p> |
|
|
307 |
<pre class="code prettyprint">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
308 |
|
|
|
309 |
//the name of the test case - if not provided, one is automatically generated |
|
|
310 |
name: "Advanced Options Tests", |
|
|
311 |
|
|
|
312 |
... |
|
|
313 |
|
|
|
314 |
testGenericError : function() { |
|
|
315 |
throw new Error("Generic error"); |
|
|
316 |
}, |
|
|
317 |
|
|
|
318 |
... |
|
|
319 |
});</pre> |
|
|
320 |
|
|
|
321 |
<p>The fact that this method throws an error is enough to cause it to pass (the type of error and error message don't matter). The next |
|
|
322 |
two methods, <code>testStringError()</code> and <code>testStringError2()</code> are specified as throwing an error with a specific |
|
|
323 |
message ("I'm a specific error message."):</p> |
|
|
324 |
<pre class="code prettyprint">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
325 |
|
|
|
326 |
//the name of the test case - if not provided, one is automatically generated |
|
|
327 |
name: "Advanced Options Tests", |
|
|
328 |
|
|
|
329 |
... |
|
|
330 |
|
|
|
331 |
testStringError : function() { |
|
|
332 |
|
|
|
333 |
//throw a specific error message - this will pass because it "should" happen |
|
|
334 |
throw new Error("I'm a specific error message."); |
|
|
335 |
}, |
|
|
336 |
|
|
|
337 |
testStringError2 : function() { |
|
|
338 |
|
|
|
339 |
//throw a specific error message - this will fail because the message isn't expected |
|
|
340 |
throw new Error("I'm a specific error message, but a wrong one."); |
|
|
341 |
}, |
|
|
342 |
|
|
|
343 |
... |
|
|
344 |
});</pre> |
|
|
345 |
|
|
|
346 |
<p>The <code>testStringError()</code> method will pass when executed because the error message matches up exactly with the one |
|
|
347 |
specified in the <code>error</code> section. The <code>testStringError2()</code> method, however, will fail because its |
|
|
348 |
error message is different from the one specified.</p> |
|
|
349 |
<p>To be more specific, <code>testObjectError()</code>, <code>testObjectError2()</code>, and <code>testObjectError3()</code>, |
|
|
350 |
specified an error type (<code>TypeError</code>) and an error messsage ("Number expected."):</p> |
|
|
351 |
<pre class="code prettyprint">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
352 |
|
|
|
353 |
//the name of the test case - if not provided, one is automatically generated |
|
|
354 |
name: "Advanced Options Tests", |
|
|
355 |
|
|
|
356 |
... |
|
|
357 |
|
|
|
358 |
testObjectError : function() { |
|
|
359 |
|
|
|
360 |
//throw a specific error and message - this will pass because it "should" happen |
|
|
361 |
throw new TypeError("Number expected."); |
|
|
362 |
}, |
|
|
363 |
|
|
|
364 |
testObjectError2 : function() { |
|
|
365 |
|
|
|
366 |
//throw a specific error and message - this will fail because the type doesn't match |
|
|
367 |
throw new Error("Number expected."); |
|
|
368 |
}, |
|
|
369 |
|
|
|
370 |
testObjectError3 : function() { |
|
|
371 |
|
|
|
372 |
//throw a specific error and message - this will fail because the message doesn't match |
|
|
373 |
throw new TypeError("String expected."); |
|
|
374 |
}, |
|
|
375 |
|
|
|
376 |
... |
|
|
377 |
});</pre> |
|
|
378 |
|
|
|
379 |
<p>Of the these three methods, only <code>testObjectError()</code> will pass because it's the only one that throws a <code>TypeError</code> |
|
|
380 |
object with the message, "Number expected." The <code>testObjectError2()</code> method will fail because the type of error |
|
|
381 |
being thrown (<code>Error</code>) is different from the expected type (<code>TypeError</code>), as specified in the <code>error</code> |
|
|
382 |
section. The last method, <code>testObjectError3()</code>, also fails. Though it throws the right type of error, the error message |
|
|
383 |
doesn't match the one that was specified.</p> |
|
|
384 |
<p>The last method in the <code>Y.Test.Case</code> is <code>testIgnore()</code>, which is specified to be ignored. To be certain, this |
|
|
385 |
method pops up a message:</p> |
|
|
386 |
<pre class="code prettyprint">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
387 |
|
|
|
388 |
//the name of the test case - if not provided, one is automatically generated |
|
|
389 |
name: "Advanced Options Tests", |
|
|
390 |
|
|
|
391 |
... |
|
|
392 |
|
|
|
393 |
testIgnore : function () { |
|
|
394 |
alert("You'll never see this."); |
|
|
395 |
} |
|
|
396 |
});</pre> |
|
|
397 |
|
|
|
398 |
<p>If this test weren't ignored, then the alert should be displayed. Since it is ignored, though, you will never see the alert. Additionally, |
|
|
399 |
there is a special message displayed in the <code>Y.Console</code> when a test is ignored.</p> |
|
|
400 |
|
|
|
401 |
<h3 id="running-the-tests">Running the tests</h3> |
|
|
402 |
|
|
|
403 |
<p>With all of the tests defined, the last step is to run them:</p> |
|
|
404 |
|
|
|
405 |
<pre class="code prettyprint">//create the console |
|
|
406 |
(new Y.Test.Console({ |
|
|
407 |
verbose : true, |
|
|
408 |
newestOnTop : false |
|
|
409 |
})).render('#testLogger'); |
|
|
410 |
|
|
|
411 |
//add the test suite to the runner's queue |
|
|
412 |
Y.Test.Runner.add(Y.example.test.AdvancedOptionsTestCase); |
|
|
413 |
|
|
|
414 |
//run the tests |
|
|
415 |
Y.Test.Runner.run();</pre> |
|
|
416 |
|
|
|
417 |
|
|
|
418 |
<p>Before running the tests, it's necessary to create a <code>Y.Console</code> object to display the results (otherwise the tests would run |
|
|
419 |
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 |
|
|
420 |
<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>, |
|
|
421 |
this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its |
|
|
422 |
queue and displays the results in the <code>Y.Console</code>.</p> |
|
|
423 |
|
|
|
424 |
<h2 id="complete-example-source">Complete Example Source</h2> |
|
|
425 |
|
|
|
426 |
<pre class="code prettyprint"><div id="testLogger"></div> |
|
|
427 |
|
|
|
428 |
<script> |
|
|
429 |
YUI().use('node', 'test-console', 'test', function (Y) { |
|
|
430 |
|
|
|
431 |
Y.namespace("example.test"); |
|
|
432 |
|
|
|
433 |
Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
434 |
|
|
|
435 |
//the name of the test case - if not provided, one is automatically generated |
|
|
436 |
name: "Advanced Options Tests", |
|
|
437 |
|
|
|
438 |
/* |
|
|
439 |
* Specifies tests that "should" be doing something other than the expected. |
|
|
440 |
*/ |
|
|
441 |
_should: { |
|
|
442 |
|
|
|
443 |
/* |
|
|
444 |
* Tests listed in here should fail, meaning that if they fail, the test |
|
|
445 |
* has passed. This is used mostly for YuiTest to test itself, but may |
|
|
446 |
* be helpful in other cases. |
|
|
447 |
*/ |
|
|
448 |
fail: { |
|
|
449 |
|
|
|
450 |
//the test named "testFail" should fail |
|
|
451 |
testFail: true |
|
|
452 |
|
|
|
453 |
}, |
|
|
454 |
|
|
|
455 |
/* |
|
|
456 |
* Tests listed here should throw an error of some sort. If they throw an |
|
|
457 |
* error, then they are considered to have passed. |
|
|
458 |
*/ |
|
|
459 |
error: { |
|
|
460 |
|
|
|
461 |
/* |
|
|
462 |
* You can specify "true" for each test, in which case any error will |
|
|
463 |
* cause the test to pass. |
|
|
464 |
*/ |
|
|
465 |
testGenericError: true, |
|
|
466 |
|
|
|
467 |
/* |
|
|
468 |
* You can specify an error message, in which case the test passes only |
|
|
469 |
* if the error thrown matches the given message. |
|
|
470 |
*/ |
|
|
471 |
testStringError: "I'm a specific error message.", |
|
|
472 |
testStringError2: "I'm a specific error message.", |
|
|
473 |
|
|
|
474 |
/* |
|
|
475 |
* You can also specify an error object, in which case the test passes only |
|
|
476 |
* if the error thrown is on the same type and has the same message. |
|
|
477 |
*/ |
|
|
478 |
testObjectError: new TypeError("Number expected."), |
|
|
479 |
testObjectError2: new TypeError("Number expected."), |
|
|
480 |
testObjectError3: new TypeError("Number expected.") |
|
|
481 |
|
|
|
482 |
}, |
|
|
483 |
|
|
|
484 |
/* |
|
|
485 |
* Tests listed here should be ignored when the test case is run. For these tests, |
|
|
486 |
* setUp() and tearDown() are not called. |
|
|
487 |
*/ |
|
|
488 |
ignore : { |
|
|
489 |
|
|
|
490 |
testIgnore: true |
|
|
491 |
|
|
|
492 |
} |
|
|
493 |
}, |
|
|
494 |
|
|
|
495 |
//------------------------------------------------------------------------- |
|
|
496 |
// Basic tests - all method names must begin with "test" |
|
|
497 |
//------------------------------------------------------------------------- |
|
|
498 |
|
|
|
499 |
testFail : function() { |
|
|
500 |
|
|
|
501 |
//force a failure - but since this test "should" fail, it will pass |
|
|
502 |
Y.Assert.fail("Something bad happened."); |
|
|
503 |
|
|
|
504 |
}, |
|
|
505 |
|
|
|
506 |
testGenericError : function() { |
|
|
507 |
throw new Error("Generic error"); |
|
|
508 |
}, |
|
|
509 |
|
|
|
510 |
testStringError : function() { |
|
|
511 |
|
|
|
512 |
//throw a specific error message - this will pass because it "should" happen |
|
|
513 |
throw new Error("I'm a specific error message."); |
|
|
514 |
}, |
|
|
515 |
|
|
|
516 |
testStringError2 : function() { |
|
|
517 |
|
|
|
518 |
//throw a specific error message - this will fail because the message isn't expected |
|
|
519 |
throw new Error("I'm a specific error message, but a wrong one."); |
|
|
520 |
}, |
|
|
521 |
|
|
|
522 |
testObjectError : function() { |
|
|
523 |
|
|
|
524 |
//throw a specific error and message - this will pass because it "should" happen |
|
|
525 |
throw new TypeError("Number expected."); |
|
|
526 |
}, |
|
|
527 |
|
|
|
528 |
testObjectError2 : function() { |
|
|
529 |
|
|
|
530 |
//throw a specific error and message - this will fail because the type doesn't match |
|
|
531 |
throw new Error("Number expected."); |
|
|
532 |
}, |
|
|
533 |
|
|
|
534 |
testObjectError3 : function() { |
|
|
535 |
|
|
|
536 |
//throw a specific error and message - this will fail because the message doesn't match |
|
|
537 |
throw new TypeError("String expected."); |
|
|
538 |
}, |
|
|
539 |
|
|
|
540 |
testIgnore : function () { |
|
|
541 |
alert("You'll never see this."); |
|
|
542 |
} |
|
|
543 |
|
|
|
544 |
}); |
|
|
545 |
|
|
|
546 |
|
|
|
547 |
//create the console |
|
|
548 |
(new Y.Test.Console({ |
|
|
549 |
newestOnTop : false, |
|
|
550 |
style: 'block' // to anchor in the example content |
|
|
551 |
})).render('#testLogger'); |
|
|
552 |
|
|
|
553 |
Y.Test.Runner.add(Y.example.test.AdvancedOptionsTestCase); |
|
|
554 |
|
|
|
555 |
//run the tests |
|
|
556 |
Y.Test.Runner.run(); |
|
|
557 |
}); |
|
|
558 |
|
|
|
559 |
</script></pre> |
|
|
560 |
|
|
|
561 |
</div> |
|
|
562 |
</div> |
|
|
563 |
</div> |
|
|
564 |
|
|
|
565 |
<div class="yui3-u-1-4"> |
|
|
566 |
<div class="sidebar"> |
|
|
567 |
|
|
|
568 |
<div id="toc" class="sidebox"> |
|
|
569 |
<div class="hd"> |
|
|
570 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
571 |
</div> |
|
|
572 |
|
|
|
573 |
<div class="bd"> |
|
|
574 |
<ul class="toc"> |
|
|
575 |
<li> |
|
|
576 |
<a href="#advanced-test-options">Advanced Test Options</a> |
|
|
577 |
<ul class="toc"> |
|
|
578 |
<li> |
|
|
579 |
<a href="#using-_should">Using <code>_should</code></a> |
|
|
580 |
</li> |
|
|
581 |
<li> |
|
|
582 |
<a href="#creating-the-test-methods">Creating the test methods</a> |
|
|
583 |
</li> |
|
|
584 |
<li> |
|
|
585 |
<a href="#running-the-tests">Running the tests</a> |
|
|
586 |
</li> |
|
|
587 |
</ul> |
|
|
588 |
</li> |
|
|
589 |
<li> |
|
|
590 |
<a href="#complete-example-source">Complete Example Source</a> |
|
|
591 |
</li> |
|
|
592 |
</ul> |
|
|
593 |
</div> |
|
|
594 |
</div> |
|
|
595 |
|
|
|
596 |
|
|
|
597 |
|
|
|
598 |
<div class="sidebox"> |
|
|
599 |
<div class="hd"> |
|
|
600 |
<h2 class="no-toc">Examples</h2> |
|
|
601 |
</div> |
|
|
602 |
|
|
|
603 |
<div class="bd"> |
|
|
604 |
<ul class="examples"> |
|
|
605 |
|
|
|
606 |
|
|
|
607 |
<li data-description="Demonstrates basic usage of YUI Test for setting up and running tests."> |
|
|
608 |
<a href="test-simple-example.html">Simple Testing Example</a> |
|
|
609 |
</li> |
|
|
610 |
|
|
|
611 |
|
|
|
612 |
|
|
|
613 |
<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."> |
|
|
614 |
<a href="test-advanced-test-options.html">Advanced Test Options</a> |
|
|
615 |
</li> |
|
|
616 |
|
|
|
617 |
|
|
|
618 |
|
|
|
619 |
<li data-description="Demonstrates how to use the ArrayAssert object to test array data."> |
|
|
620 |
<a href="test-array-tests.html">Array Processing</a> |
|
|
621 |
</li> |
|
|
622 |
|
|
|
623 |
|
|
|
624 |
|
|
|
625 |
<li data-description="Demonstrates basic asynchronous tests."> |
|
|
626 |
<a href="test-async-test.html">Asynchronous Testing</a> |
|
|
627 |
</li> |
|
|
628 |
|
|
|
629 |
|
|
|
630 |
|
|
|
631 |
<li data-description="Demonstrates using events with asynchronous tests."> |
|
|
632 |
<a href="test-async-event-tests.html">Asynchronous Event Testing</a> |
|
|
633 |
</li> |
|
|
634 |
|
|
|
635 |
|
|
|
636 |
</ul> |
|
|
637 |
</div> |
|
|
638 |
</div> |
|
|
639 |
|
|
|
640 |
|
|
|
641 |
|
|
|
642 |
</div> |
|
|
643 |
</div> |
|
|
644 |
</div> |
|
|
645 |
</div> |
|
|
646 |
|
|
|
647 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
648 |
<script>prettyPrint();</script> |
|
|
649 |
|
|
|
650 |
<script> |
|
|
651 |
YUI.Env.Tests = { |
|
|
652 |
examples: [], |
|
|
653 |
project: '../assets', |
|
|
654 |
assets: '../assets/test', |
|
|
655 |
name: 'test-advanced-test-options', |
|
|
656 |
title: 'Advanced Test Options', |
|
|
657 |
newWindow: '', |
|
|
658 |
auto: false |
|
|
659 |
}; |
|
|
660 |
YUI.Env.Tests.examples.push('test-simple-example'); |
|
|
661 |
YUI.Env.Tests.examples.push('test-advanced-test-options'); |
|
|
662 |
YUI.Env.Tests.examples.push('test-array-tests'); |
|
|
663 |
YUI.Env.Tests.examples.push('test-async-test'); |
|
|
664 |
YUI.Env.Tests.examples.push('test-async-event-tests'); |
|
|
665 |
|
|
|
666 |
</script> |
|
|
667 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
668 |
|
|
|
669 |
|
|
|
670 |
|
|
|
671 |
</body> |
|
|
672 |
</html> |