|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<title>YUI Library Examples: Test: Advanced Test Options</title> |
|
|
6 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
7 |
<link rel="stylesheet" type="text/css" href="../../assets/yui.css" > |
|
|
8 |
|
|
|
9 |
<style> |
|
|
10 |
/*Supplemental CSS for the YUI distribution*/ |
|
|
11 |
#custom-doc { width: 95%; min-width: 950px; } |
|
|
12 |
#pagetitle {background-image: url(../../assets/bg_hd.gif);} |
|
|
13 |
/* #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}*/ |
|
|
14 |
</style> |
|
|
15 |
|
|
|
16 |
<link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css"> |
|
|
17 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
18 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
19 |
<style type="text/css"> |
|
|
20 |
#testLogger { |
|
|
21 |
margin-bottom: 1em; |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
#testLogger .yui-console .yui-console-title { |
|
|
25 |
border: 0 none; |
|
|
26 |
color: #000; |
|
|
27 |
font-size: 13px; |
|
|
28 |
font-weight: bold; |
|
|
29 |
margin: 0; |
|
|
30 |
text-transform: none; |
|
|
31 |
} |
|
|
32 |
#testLogger .yui-console .yui-console-entry-meta { |
|
|
33 |
margin: 0; |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
.yui-skin-sam .yui-console-entry-pass .yui-console-entry-cat { |
|
|
37 |
background: #070; |
|
|
38 |
color: #fff; |
|
|
39 |
} |
|
|
40 |
.yui-skin-sam .yui-console-entry-fail .yui-console-entry-cat { |
|
|
41 |
background: #700; |
|
|
42 |
color: #fff; |
|
|
43 |
} |
|
|
44 |
</style> |
|
|
45 |
|
|
|
46 |
</head> |
|
|
47 |
<body id="yahoo-com" class=" yui-skin-sam"> |
|
|
48 |
<div id="custom-doc" class="yui-t2"> |
|
|
49 |
<div id="hd"> |
|
|
50 |
<div id="ygunav"> |
|
|
51 |
<p> |
|
|
52 |
<em> |
|
|
53 |
<a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> |
|
|
54 |
</em> |
|
|
55 |
</p> |
|
|
56 |
<form action="http://search.yahoo.com/search" id="sitesearchform"> |
|
|
57 |
<input name="vs" type="hidden" value="developer.yahoo.com"> |
|
|
58 |
<input name="vs" type="hidden" value="yuiblog.com"> |
|
|
59 |
<div id="sitesearch"> |
|
|
60 |
<label for="searchinput">Site Search (YDN & YUIBlog): </label> |
|
|
61 |
<input type="text" id="searchinput" name="p"> |
|
|
62 |
<input type="submit" value="Search" id="searchsubmit" class="ygbt"> |
|
|
63 |
</div> |
|
|
64 |
</form> |
|
|
65 |
</div> |
|
|
66 |
<div id="ygma"><a href="../../"><img src="../../assets/logo.gif" border="0" width="200" height="93"></a></div> |
|
|
67 |
<div id="pagetitle"><h1>YUI Library Examples: Test: Advanced Test Options</h1></div> |
|
|
68 |
</div> |
|
|
69 |
<div id="bd"> |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
<div id="yui-main"> |
|
|
73 |
<div class="yui-b"> |
|
|
74 |
<div class="yui-ge"> |
|
|
75 |
<div class="yui-u first example" id="main"> |
|
|
76 |
|
|
|
77 |
<h2>Test: Advanced Test Options</h2> |
|
|
78 |
|
|
|
79 |
<div id="example" class="promo"> |
|
|
80 |
<div class="example-intro"> |
|
|
81 |
<p>This example shows how to use advanced test options, which allow you to specify additional information about how a test should be run. |
|
|
82 |
Each <a href="/yui/test/#testcase"><code>TestCase</code></a> can specify up to three different options for tests, |
|
|
83 |
including tests that should be ignored, tests that should throw an error, and tests that should fail.</p> </div> |
|
|
84 |
|
|
|
85 |
<div class="module example-container "> |
|
|
86 |
<div class="hd exampleHd"> |
|
|
87 |
<p class="newWindowButton yui-skin-sam"> |
|
|
88 |
<a href="test-advanced-test-options_clean.html" target="_blank">View example in new window.</a> |
|
|
89 |
</p> |
|
|
90 |
</div> <div id="example-canvas" class="bd"> |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
94 |
|
|
|
95 |
<div id="testLogger"></div> |
|
|
96 |
<script type="text/javascript"> |
|
|
97 |
YUI({base:"../../build/", timeout: 10000}).use("node", "console", "test",function (Y) { |
|
|
98 |
|
|
|
99 |
Y.namespace("example.test"); |
|
|
100 |
|
|
|
101 |
Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
102 |
|
|
|
103 |
//the name of the test case - if not provided, one is automatically generated |
|
|
104 |
name: "Advanced Options Tests", |
|
|
105 |
|
|
|
106 |
/* |
|
|
107 |
* Specifies tests that "should" be doing something other than the expected. |
|
|
108 |
*/ |
|
|
109 |
_should: { |
|
|
110 |
|
|
|
111 |
/* |
|
|
112 |
* Tests listed in here should fail, meaning that if they fail, the test |
|
|
113 |
* has passed. This is used mostly for YuiTest to test itself, but may |
|
|
114 |
* be helpful in other cases. |
|
|
115 |
*/ |
|
|
116 |
fail: { |
|
|
117 |
|
|
|
118 |
//the test named "testFail" should fail |
|
|
119 |
testFail: true |
|
|
120 |
|
|
|
121 |
}, |
|
|
122 |
|
|
|
123 |
/* |
|
|
124 |
* Tests listed here should throw an error of some sort. If they throw an |
|
|
125 |
* error, then they are considered to have passed. |
|
|
126 |
*/ |
|
|
127 |
error: { |
|
|
128 |
|
|
|
129 |
/* |
|
|
130 |
* You can specify "true" for each test, in which case any error will |
|
|
131 |
* cause the test to pass. |
|
|
132 |
*/ |
|
|
133 |
testGenericError: true, |
|
|
134 |
|
|
|
135 |
/* |
|
|
136 |
* You can specify an error message, in which case the test passes only |
|
|
137 |
* if the error thrown matches the given message. |
|
|
138 |
*/ |
|
|
139 |
testStringError: "I'm a specific error message.", |
|
|
140 |
testStringError2: "I'm a specific error message.", |
|
|
141 |
|
|
|
142 |
/* |
|
|
143 |
* You can also specify an error object, in which case the test passes only |
|
|
144 |
* if the error thrown is on the same type and has the same message. |
|
|
145 |
*/ |
|
|
146 |
testObjectError: new TypeError("Number expected."), |
|
|
147 |
testObjectError2: new TypeError("Number expected."), |
|
|
148 |
testObjectError3: new TypeError("Number expected.") |
|
|
149 |
|
|
|
150 |
}, |
|
|
151 |
|
|
|
152 |
/* |
|
|
153 |
* Tests listed here should be ignored when the test case is run. For these tests, |
|
|
154 |
* setUp() and tearDown() are not called. |
|
|
155 |
*/ |
|
|
156 |
ignore : { |
|
|
157 |
|
|
|
158 |
testIgnore: true |
|
|
159 |
|
|
|
160 |
} |
|
|
161 |
}, |
|
|
162 |
|
|
|
163 |
//------------------------------------------------------------------------- |
|
|
164 |
// Basic tests - all method names must begin with "test" |
|
|
165 |
//------------------------------------------------------------------------- |
|
|
166 |
|
|
|
167 |
testFail : function() { |
|
|
168 |
|
|
|
169 |
//force a failure - but since this test "should" fail, it will pass |
|
|
170 |
Y.Assert.fail("Something bad happened."); |
|
|
171 |
|
|
|
172 |
}, |
|
|
173 |
|
|
|
174 |
testGenericError : function() { |
|
|
175 |
throw new Error("Generic error"); |
|
|
176 |
}, |
|
|
177 |
|
|
|
178 |
testStringError : function() { |
|
|
179 |
|
|
|
180 |
//throw a specific error message - this will pass because it "should" happen |
|
|
181 |
throw new Error("I'm a specific error message."); |
|
|
182 |
}, |
|
|
183 |
|
|
|
184 |
testStringError2 : function() { |
|
|
185 |
|
|
|
186 |
//throw a specific error message - this will fail because the message isn't expected |
|
|
187 |
throw new Error("I'm a specific error message, but a wrong one."); |
|
|
188 |
}, |
|
|
189 |
|
|
|
190 |
testObjectError : function() { |
|
|
191 |
|
|
|
192 |
//throw a specific error and message - this will pass because it "should" happen |
|
|
193 |
throw new TypeError("Number expected."); |
|
|
194 |
}, |
|
|
195 |
|
|
|
196 |
testObjectError2 : function() { |
|
|
197 |
|
|
|
198 |
//throw a specific error and message - this will fail because the type doesn't match |
|
|
199 |
throw new Error("Number expected."); |
|
|
200 |
}, |
|
|
201 |
|
|
|
202 |
testObjectError3 : function() { |
|
|
203 |
|
|
|
204 |
//throw a specific error and message - this will fail because the message doesn't match |
|
|
205 |
throw new TypeError("String expected."); |
|
|
206 |
}, |
|
|
207 |
|
|
|
208 |
testIgnore : function () { |
|
|
209 |
alert("You'll never see this."); |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
}); |
|
|
213 |
|
|
|
214 |
|
|
|
215 |
//create the console |
|
|
216 |
var r = new Y.Console({ |
|
|
217 |
newestOnTop : false, |
|
|
218 |
style: 'block' // to anchor in the example content |
|
|
219 |
}); |
|
|
220 |
|
|
|
221 |
r.render('#testLogger'); |
|
|
222 |
|
|
|
223 |
Y.Test.Runner.add(Y.example.test.AdvancedOptionsTestCase); |
|
|
224 |
|
|
|
225 |
//run the tests |
|
|
226 |
Y.Test.Runner.run(); |
|
|
227 |
}); |
|
|
228 |
|
|
|
229 |
</script> |
|
|
230 |
|
|
|
231 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
232 |
|
|
|
233 |
|
|
|
234 |
</div> |
|
|
235 |
</div> |
|
|
236 |
</div> |
|
|
237 |
|
|
|
238 |
<h2 class="first">Advanced Test Options</h2> |
|
|
239 |
|
|
|
240 |
<p>This example begins by creating a namespace and a <code>Y.Test.Case</code> object:</p> |
|
|
241 |
<div id="syntax1" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Y.<span class="kw2">namespace</span><span class="br0">(</span><span class="st0">"example.test"</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">TestCase</span><span class="br0">(</span><span class="br0">{</span></div></li><li class="li1"><div class="de1"> <span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span></div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;">Y.<span class="kw2">namespace</span><span class="br0">(</span><span class="st0">"example.test"</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
242 |
Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">TestCase</span><span class="br0">(</span><span class="br0">{</span> |
|
|
243 |
<span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span> |
|
|
244 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax1-plain">Y.namespace("example.test"); |
|
|
245 |
Y.example.test.AdvancedOptionsTestCase = new Y.TestCase({ |
|
|
246 |
name: "Advanced Options Tests" |
|
|
247 |
});</textarea></div> |
|
|
248 |
<p>This <code>Y.Test.Case</code> serves as the basis for this example.</p> |
|
|
249 |
|
|
|
250 |
<h3>Using <code>_should</code></h3> |
|
|
251 |
|
|
|
252 |
<p>Immediately after the <code>name</code> of the <code>Y.Test.Case</code> is defined, there is a <code>_should</code> property. |
|
|
253 |
This property specifies information about how tests <em>should</em> behave and is defined as an object literal with one |
|
|
254 |
or more of the following properties: <code>fail</code>, <code>error</code>, and <code>ignore</code>.Each of these three |
|
|
255 |
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>. |
|
|
256 |
This example uses all three properties:</p> |
|
|
257 |
<div id="syntax2" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">TestCase</span><span class="br0">(</span><span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">//the name of the test case - if not provided, one is automatically generated</span></div></li><li class="li1"><div class="de1"> <span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="coMULTI">/*</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * Specifies tests that "should" be doing something other than the expected.</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> */</span></div></li><li class="li1"><div class="de1"> _should<span class="sy0">:</span> <span class="br0">{</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="coMULTI">/*</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * Tests listed in here should fail, meaning that if they fail, the test</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * has passed. This is used mostly for YuiTest to test itself, but may</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * be helpful in other cases.</span></div></li><li class="li2"><div class="de2"><span class="coMULTI"> */</span></div></li><li class="li1"><div class="de1"> fail<span class="sy0">:</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">//the test named "testFail" should fail</span></div></li><li class="li1"><div class="de1"> testFail<span class="sy0">:</span> <span class="kw2">true</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="coMULTI">/*</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * Tests listed here should throw an error of some sort. If they throw an</span></div></li><li class="li2"><div class="de2"><span class="coMULTI"> * error, then they are considered to have passed.</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> */</span></div></li><li class="li1"><div class="de1"> error<span class="sy0">:</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="coMULTI">/*</span></div></li><li class="li2"><div class="de2"><span class="coMULTI"> * You can specify "true" for each test, in which case any error will</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * cause the test to pass.</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> */</span></div></li><li class="li1"><div class="de1"> testGenericError<span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> <span class="coMULTI">/*</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * You can specify an error message, in which case the test passes only</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * if the error thrown matches the given message.</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> */</span></div></li><li class="li1"><div class="de1"> testStringError<span class="sy0">:</span> <span class="st0">"I'm a specific error message."</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> testStringError2<span class="sy0">:</span> <span class="st0">"I'm a specific error message."</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="coMULTI">/*</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * You can also specify an error object, in which case the test passes only</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * if the error thrown is on the same type and has the same message.</span></div></li><li class="li2"><div class="de2"><span class="coMULTI"> */</span></div></li><li class="li1"><div class="de1"> testObjectError<span class="sy0">:</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> testObjectError2<span class="sy0">:</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> testObjectError3<span class="sy0">:</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="coMULTI">/*</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * Tests listed here should be ignored when the test case is run. For these tests,</span></div></li><li class="li1"><div class="de1"><span class="coMULTI"> * setUp() and tearDown() are not called.</span></div></li><li class="li2"><div class="de2"><span class="coMULTI"> */</span></div></li><li class="li1"><div class="de1"> ignore <span class="sy0">:</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> testIgnore<span class="sy0">:</span> <span class="kw2">true</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> <span class="br0">}</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> ...</div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">TestCase</span><span class="br0">(</span><span class="br0">{</span> |
|
|
258 |
|
|
|
259 |
<span class="co1">//the name of the test case - if not provided, one is automatically generated</span> |
|
|
260 |
<span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span> |
|
|
261 |
|
|
|
262 |
<span class="coMULTI">/* |
|
|
263 |
* Specifies tests that "should" be doing something other than the expected. |
|
|
264 |
*/</span> |
|
|
265 |
_should<span class="sy0">:</span> <span class="br0">{</span> |
|
|
266 |
|
|
|
267 |
<span class="coMULTI">/* |
|
|
268 |
* Tests listed in here should fail, meaning that if they fail, the test |
|
|
269 |
* has passed. This is used mostly for YuiTest to test itself, but may |
|
|
270 |
* be helpful in other cases. |
|
|
271 |
*/</span> |
|
|
272 |
fail<span class="sy0">:</span> <span class="br0">{</span> |
|
|
273 |
|
|
|
274 |
<span class="co1">//the test named "testFail" should fail</span> |
|
|
275 |
testFail<span class="sy0">:</span> <span class="kw2">true</span> |
|
|
276 |
|
|
|
277 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
278 |
|
|
|
279 |
<span class="coMULTI">/* |
|
|
280 |
* Tests listed here should throw an error of some sort. If they throw an |
|
|
281 |
* error, then they are considered to have passed. |
|
|
282 |
*/</span> |
|
|
283 |
error<span class="sy0">:</span> <span class="br0">{</span> |
|
|
284 |
|
|
|
285 |
<span class="coMULTI">/* |
|
|
286 |
* You can specify "true" for each test, in which case any error will |
|
|
287 |
* cause the test to pass. |
|
|
288 |
*/</span> |
|
|
289 |
testGenericError<span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span> |
|
|
290 |
|
|
|
291 |
<span class="coMULTI">/* |
|
|
292 |
* You can specify an error message, in which case the test passes only |
|
|
293 |
* if the error thrown matches the given message. |
|
|
294 |
*/</span> |
|
|
295 |
testStringError<span class="sy0">:</span> <span class="st0">"I'm a specific error message."</span><span class="sy0">,</span> |
|
|
296 |
testStringError2<span class="sy0">:</span> <span class="st0">"I'm a specific error message."</span><span class="sy0">,</span> |
|
|
297 |
|
|
|
298 |
<span class="coMULTI">/* |
|
|
299 |
* You can also specify an error object, in which case the test passes only |
|
|
300 |
* if the error thrown is on the same type and has the same message. |
|
|
301 |
*/</span> |
|
|
302 |
testObjectError<span class="sy0">:</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span><span class="sy0">,</span> |
|
|
303 |
testObjectError2<span class="sy0">:</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span><span class="sy0">,</span> |
|
|
304 |
testObjectError3<span class="sy0">:</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span> |
|
|
305 |
|
|
|
306 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
307 |
|
|
|
308 |
<span class="coMULTI">/* |
|
|
309 |
* Tests listed here should be ignored when the test case is run. For these tests, |
|
|
310 |
* setUp() and tearDown() are not called. |
|
|
311 |
*/</span> |
|
|
312 |
ignore <span class="sy0">:</span> <span class="br0">{</span> |
|
|
313 |
|
|
|
314 |
testIgnore<span class="sy0">:</span> <span class="kw2">true</span> |
|
|
315 |
|
|
|
316 |
<span class="br0">}</span> |
|
|
317 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
318 |
|
|
|
319 |
... |
|
|
320 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax2-plain">Y.example.test.AdvancedOptionsTestCase = new Y.TestCase({ |
|
|
321 |
|
|
|
322 |
//the name of the test case - if not provided, one is automatically generated |
|
|
323 |
name: "Advanced Options Tests", |
|
|
324 |
|
|
|
325 |
/* |
|
|
326 |
* Specifies tests that "should" be doing something other than the expected. |
|
|
327 |
*/ |
|
|
328 |
_should: { |
|
|
329 |
|
|
|
330 |
/* |
|
|
331 |
* Tests listed in here should fail, meaning that if they fail, the test |
|
|
332 |
* has passed. This is used mostly for YuiTest to test itself, but may |
|
|
333 |
* be helpful in other cases. |
|
|
334 |
*/ |
|
|
335 |
fail: { |
|
|
336 |
|
|
|
337 |
//the test named "testFail" should fail |
|
|
338 |
testFail: true |
|
|
339 |
|
|
|
340 |
}, |
|
|
341 |
|
|
|
342 |
/* |
|
|
343 |
* Tests listed here should throw an error of some sort. If they throw an |
|
|
344 |
* error, then they are considered to have passed. |
|
|
345 |
*/ |
|
|
346 |
error: { |
|
|
347 |
|
|
|
348 |
/* |
|
|
349 |
* You can specify "true" for each test, in which case any error will |
|
|
350 |
* cause the test to pass. |
|
|
351 |
*/ |
|
|
352 |
testGenericError: true, |
|
|
353 |
|
|
|
354 |
/* |
|
|
355 |
* You can specify an error message, in which case the test passes only |
|
|
356 |
* if the error thrown matches the given message. |
|
|
357 |
*/ |
|
|
358 |
testStringError: "I'm a specific error message.", |
|
|
359 |
testStringError2: "I'm a specific error message.", |
|
|
360 |
|
|
|
361 |
/* |
|
|
362 |
* You can also specify an error object, in which case the test passes only |
|
|
363 |
* if the error thrown is on the same type and has the same message. |
|
|
364 |
*/ |
|
|
365 |
testObjectError: new TypeError("Number expected."), |
|
|
366 |
testObjectError2: new TypeError("Number expected."), |
|
|
367 |
testObjectError3: new TypeError("Number expected.") |
|
|
368 |
|
|
|
369 |
}, |
|
|
370 |
|
|
|
371 |
/* |
|
|
372 |
* Tests listed here should be ignored when the test case is run. For these tests, |
|
|
373 |
* setUp() and tearDown() are not called. |
|
|
374 |
*/ |
|
|
375 |
ignore : { |
|
|
376 |
|
|
|
377 |
testIgnore: true |
|
|
378 |
|
|
|
379 |
} |
|
|
380 |
}, |
|
|
381 |
|
|
|
382 |
... |
|
|
383 |
});</textarea></div> |
|
|
384 |
<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> |
|
|
385 |
<p>In the <code>fail</code> section, the test method <code>testFail()</code> is specified as one that should fail. By adding the |
|
|
386 |
property <code>testFail</code> and settings its value to true, the <code>Y.Test.Runner</code> knows that this test is expected to fail. |
|
|
387 |
If the test were to be run without failing, it would be considered a failure of the test. This feature is useful when testing |
|
|
388 |
YUI Test itself or addon components to YUI Test.</p> |
|
|
389 |
<p>Moving on to the <code>error</code> section, there are six tests specified that should throw an error. There are three different ways |
|
|
390 |
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 |
|
|
391 |
and set its value equal to true (similar to specifying tests that should fail). In this example, the <code>testGenericError()</code> |
|
|
392 |
method is specified this way. When specified like this, the test passes regardless of the type of error that occurs. This can be |
|
|
393 |
dangerous since unexpected errors will also cause the test to pass. To be more specific, set the property value for the test method |
|
|
394 |
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 |
|
|
395 |
error message matches the string. In this example, <code>testStringError()</code> and <code>testStringError2()</code> expect an error |
|
|
396 |
to be thrown with an error message of "I'm a specific error message." If any other error occurs inside of the these methods, |
|
|
397 |
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 |
|
|
398 |
object, which is the case with <code>testObjectError()</code>, <code>testObjectError2()</code>, and <code>testObjectError3()</code>. |
|
|
399 |
When specified in this way, a test will pass only when an error is thrown whose constructor and error message match that of the |
|
|
400 |
error object.</p> |
|
|
401 |
<p>The last section is <code>ignore</code>, which determines tests that should be ignored. In this example, the method <code>testIgnore()</code> |
|
|
402 |
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 |
|
|
403 |
as those in the <code>fail</code> section, by adding the name as a property and setting its value to true.</p> |
|
|
404 |
|
|
|
405 |
<h3>Creating the test methods</h3> |
|
|
406 |
|
|
|
407 |
<p>The next part of the example contains the actual test methods. Since each test method is specified as having a certain behavior in |
|
|
408 |
<code>_should</code>, they each do something to show their particular functionality.</p> |
|
|
409 |
<p>The first method is <code>testFail()</code>, which does nothing but purposely fail. Since this method is specified as one that should |
|
|
410 |
fail, it means that this test will pass:</p> |
|
|
411 |
<div id="syntax3" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">//the name of the test case - if not provided, one is automatically generated</span></div></li><li class="li1"><div class="de1"> <span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> ...</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="me1">testFail</span> <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> <span class="co1">//force a failure - but since this test "should" fail, it will pass</span></div></li><li class="li1"><div class="de1"> Y.<span class="me1">Assert</span>.<span class="me1">fail</span><span class="br0">(</span><span class="st0">"Something bad happened."</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> ...</div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span> |
|
|
412 |
|
|
|
413 |
<span class="co1">//the name of the test case - if not provided, one is automatically generated</span> |
|
|
414 |
<span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span> |
|
|
415 |
|
|
|
416 |
... |
|
|
417 |
|
|
|
418 |
<span class="me1">testFail</span> <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
419 |
|
|
|
420 |
<span class="co1">//force a failure - but since this test "should" fail, it will pass</span> |
|
|
421 |
Y.<span class="me1">Assert</span>.<span class="me1">fail</span><span class="br0">(</span><span class="st0">"Something bad happened."</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
422 |
|
|
|
423 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
424 |
|
|
|
425 |
... |
|
|
426 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax3-plain">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
427 |
|
|
|
428 |
//the name of the test case - if not provided, one is automatically generated |
|
|
429 |
name: "Advanced Options Tests", |
|
|
430 |
|
|
|
431 |
... |
|
|
432 |
|
|
|
433 |
testFail : function() { |
|
|
434 |
|
|
|
435 |
//force a failure - but since this test "should" fail, it will pass |
|
|
436 |
Y.Assert.fail("Something bad happened."); |
|
|
437 |
|
|
|
438 |
}, |
|
|
439 |
|
|
|
440 |
... |
|
|
441 |
});</textarea></div><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 |
|
|
442 |
type of assert methods that should fail when certain data is passed in.</p> |
|
|
443 |
<p>Next, the test methods that should error are defined. The <code>testGenericError()</code> method is specified as needing to throw |
|
|
444 |
an error to pass. In the <code>error</code> section, <code>testGenericError</code> is set to true, meaning that any error causes |
|
|
445 |
this method to pass. To illustrate this, the method simply throws an error:</p> |
|
|
446 |
<div id="syntax4" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">//the name of the test case - if not provided, one is automatically generated</span></div></li><li class="li1"><div class="de1"> <span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> ...</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="me1">testGenericError</span> <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> <span class="kw1">throw</span> <span class="kw2">new</span> Error<span class="br0">(</span><span class="st0">"Generic error"</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> ...</div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span> |
|
|
447 |
|
|
|
448 |
<span class="co1">//the name of the test case - if not provided, one is automatically generated</span> |
|
|
449 |
<span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span> |
|
|
450 |
|
|
|
451 |
... |
|
|
452 |
|
|
|
453 |
<span class="me1">testGenericError</span> <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
454 |
<span class="kw1">throw</span> <span class="kw2">new</span> Error<span class="br0">(</span><span class="st0">"Generic error"</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
455 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
456 |
|
|
|
457 |
... |
|
|
458 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax4-plain">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
459 |
|
|
|
460 |
//the name of the test case - if not provided, one is automatically generated |
|
|
461 |
name: "Advanced Options Tests", |
|
|
462 |
|
|
|
463 |
... |
|
|
464 |
|
|
|
465 |
testGenericError : function() { |
|
|
466 |
throw new Error("Generic error"); |
|
|
467 |
}, |
|
|
468 |
|
|
|
469 |
... |
|
|
470 |
});</textarea></div><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 |
|
|
471 |
two methods, <code>testStringError()</code> and <code>testStringError2()</code> are specified as throwing an error with a specific |
|
|
472 |
message ("I'm a specific error message."):</p> |
|
|
473 |
<div id="syntax5" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">//the name of the test case - if not provided, one is automatically generated</span></div></li><li class="li1"><div class="de1"> <span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> ...</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="me1">testStringError</span> <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> <span class="co1">//throw a specific error message - this will pass because it "should" happen</span></div></li><li class="li1"><div class="de1"> <span class="kw1">throw</span> <span class="kw2">new</span> Error<span class="br0">(</span><span class="st0">"I'm a specific error message."</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> testStringError2 <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="co1">//throw a specific error message - this will fail because the message isn't expected</span></div></li><li class="li1"><div class="de1"> <span class="kw1">throw</span> <span class="kw2">new</span> Error<span class="br0">(</span><span class="st0">"I'm a specific error message, but a wrong one."</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> ...</div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span> |
|
|
474 |
|
|
|
475 |
<span class="co1">//the name of the test case - if not provided, one is automatically generated</span> |
|
|
476 |
<span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span> |
|
|
477 |
|
|
|
478 |
... |
|
|
479 |
|
|
|
480 |
<span class="me1">testStringError</span> <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
481 |
|
|
|
482 |
<span class="co1">//throw a specific error message - this will pass because it "should" happen</span> |
|
|
483 |
<span class="kw1">throw</span> <span class="kw2">new</span> Error<span class="br0">(</span><span class="st0">"I'm a specific error message."</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
484 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
485 |
|
|
|
486 |
testStringError2 <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
487 |
|
|
|
488 |
<span class="co1">//throw a specific error message - this will fail because the message isn't expected</span> |
|
|
489 |
<span class="kw1">throw</span> <span class="kw2">new</span> Error<span class="br0">(</span><span class="st0">"I'm a specific error message, but a wrong one."</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
490 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
491 |
|
|
|
492 |
... |
|
|
493 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax5-plain">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
494 |
|
|
|
495 |
//the name of the test case - if not provided, one is automatically generated |
|
|
496 |
name: "Advanced Options Tests", |
|
|
497 |
|
|
|
498 |
... |
|
|
499 |
|
|
|
500 |
testStringError : function() { |
|
|
501 |
|
|
|
502 |
//throw a specific error message - this will pass because it "should" happen |
|
|
503 |
throw new Error("I'm a specific error message."); |
|
|
504 |
}, |
|
|
505 |
|
|
|
506 |
testStringError2 : function() { |
|
|
507 |
|
|
|
508 |
//throw a specific error message - this will fail because the message isn't expected |
|
|
509 |
throw new Error("I'm a specific error message, but a wrong one."); |
|
|
510 |
}, |
|
|
511 |
|
|
|
512 |
... |
|
|
513 |
});</textarea></div><p>The <code>testStringError()</code> method will pass when executed because the error message matches up exactly with the one |
|
|
514 |
specified in the <code>error</code> section. The <code>testStringError2()</code> method, however, will fail because its |
|
|
515 |
error message is different from the one specified.</p> |
|
|
516 |
<p>To be more specific, <code>testObjectError()</code>, <code>testObjectError2()</code>, and <code>testObjectError3()</code>, |
|
|
517 |
specified an error type (<code>TypeError</code>) and an error messsage ("Number expected."):</p> |
|
|
518 |
<div id="syntax6" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">//the name of the test case - if not provided, one is automatically generated</span></div></li><li class="li1"><div class="de1"> <span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> ...</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="me1">testObjectError</span> <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> <span class="co1">//throw a specific error and message - this will pass because it "should" happen</span></div></li><li class="li1"><div class="de1"> <span class="kw1">throw</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> testObjectError2 <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="co1">//throw a specific error and message - this will fail because the type doesn't match</span></div></li><li class="li1"><div class="de1"> <span class="kw1">throw</span> <span class="kw2">new</span> Error<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li2"><div class="de2"> testObjectError3 <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">//throw a specific error and message - this will fail because the message doesn't match</span></div></li><li class="li1"><div class="de1"> <span class="kw1">throw</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"String expected."</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> ...</div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span> |
|
|
519 |
|
|
|
520 |
<span class="co1">//the name of the test case - if not provided, one is automatically generated</span> |
|
|
521 |
<span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span> |
|
|
522 |
|
|
|
523 |
... |
|
|
524 |
|
|
|
525 |
<span class="me1">testObjectError</span> <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
526 |
|
|
|
527 |
<span class="co1">//throw a specific error and message - this will pass because it "should" happen</span> |
|
|
528 |
<span class="kw1">throw</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
529 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
530 |
|
|
|
531 |
testObjectError2 <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
532 |
|
|
|
533 |
<span class="co1">//throw a specific error and message - this will fail because the type doesn't match</span> |
|
|
534 |
<span class="kw1">throw</span> <span class="kw2">new</span> Error<span class="br0">(</span><span class="st0">"Number expected."</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
535 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
536 |
|
|
|
537 |
testObjectError3 <span class="sy0">:</span> <span class="kw2">function</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
538 |
|
|
|
539 |
<span class="co1">//throw a specific error and message - this will fail because the message doesn't match</span> |
|
|
540 |
<span class="kw1">throw</span> <span class="kw2">new</span> TypeError<span class="br0">(</span><span class="st0">"String expected."</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
541 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
542 |
|
|
|
543 |
... |
|
|
544 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax6-plain">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
545 |
|
|
|
546 |
//the name of the test case - if not provided, one is automatically generated |
|
|
547 |
name: "Advanced Options Tests", |
|
|
548 |
|
|
|
549 |
... |
|
|
550 |
|
|
|
551 |
testObjectError : function() { |
|
|
552 |
|
|
|
553 |
//throw a specific error and message - this will pass because it "should" happen |
|
|
554 |
throw new TypeError("Number expected."); |
|
|
555 |
}, |
|
|
556 |
|
|
|
557 |
testObjectError2 : function() { |
|
|
558 |
|
|
|
559 |
//throw a specific error and message - this will fail because the type doesn't match |
|
|
560 |
throw new Error("Number expected."); |
|
|
561 |
}, |
|
|
562 |
|
|
|
563 |
testObjectError3 : function() { |
|
|
564 |
|
|
|
565 |
//throw a specific error and message - this will fail because the message doesn't match |
|
|
566 |
throw new TypeError("String expected."); |
|
|
567 |
}, |
|
|
568 |
|
|
|
569 |
... |
|
|
570 |
});</textarea></div><p>Of the these three methods, only <code>testObjectError()</code> will pass because it's the only one that throws a <code>TypeError</code> |
|
|
571 |
object with the message, "Number expected." The <code>testObjectError2()</code> method will fail because the type of error |
|
|
572 |
being thrown (<code>Error</code>) is different from the expected type (<code>TypeError</code>), as specified in the <code>error</code> |
|
|
573 |
section. The last method, <code>testObjectError3()</code>, also fails. Though it throws the right type of error, the error message |
|
|
574 |
doesn't match the one that was specified.</p> |
|
|
575 |
<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 |
|
|
576 |
method pops up a message:</p> |
|
|
577 |
<div id="syntax7" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">//the name of the test case - if not provided, one is automatically generated</span></div></li><li class="li1"><div class="de1"> <span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> ...</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="me1">testIgnore</span> <span class="sy0">:</span> <span class="kw2">function</span> <span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> <span class="kw3">alert</span><span class="br0">(</span><span class="st0">"You'll never see this."</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> <span class="br0">}</span></div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;">Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span> <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Test</span>.<span class="kw1">Case</span><span class="br0">(</span><span class="br0">{</span> |
|
|
578 |
|
|
|
579 |
<span class="co1">//the name of the test case - if not provided, one is automatically generated</span> |
|
|
580 |
<span class="kw3">name</span><span class="sy0">:</span> <span class="st0">"Advanced Options Tests"</span><span class="sy0">,</span> |
|
|
581 |
|
|
|
582 |
... |
|
|
583 |
|
|
|
584 |
<span class="me1">testIgnore</span> <span class="sy0">:</span> <span class="kw2">function</span> <span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
585 |
<span class="kw3">alert</span><span class="br0">(</span><span class="st0">"You'll never see this."</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
586 |
<span class="br0">}</span> |
|
|
587 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax7-plain">Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ |
|
|
588 |
|
|
|
589 |
//the name of the test case - if not provided, one is automatically generated |
|
|
590 |
name: "Advanced Options Tests", |
|
|
591 |
|
|
|
592 |
... |
|
|
593 |
|
|
|
594 |
testIgnore : function () { |
|
|
595 |
alert("You'll never see this."); |
|
|
596 |
} |
|
|
597 |
});</textarea></div><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, |
|
|
598 |
there is a special message displayed in the <code>Y.Console</code> when a test is ignored.</p> |
|
|
599 |
|
|
|
600 |
<h3>Running the tests</h3> |
|
|
601 |
|
|
|
602 |
<p>With all of the tests defined, the last step is to run them:</p> |
|
|
603 |
|
|
|
604 |
<div id="syntax8" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="co1">//create the console</span></div></li><li class="li1"><div class="de1"><span class="kw2">var</span> r <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Console</span><span class="br0">(</span><span class="br0">{</span></div></li><li class="li1"><div class="de1"> verbose <span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> newestOnTop <span class="sy0">:</span> <span class="kw2">false</span></div></li><li class="li2"><div class="de2"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">r.<span class="me1">render</span><span class="br0">(</span><span class="st0">'#testLogger'</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"><span class="co1">//add the test suite to the runner's queue</span></div></li><li class="li2"><div class="de2">Y.<span class="me1">Test</span>.<span class="me1">Runner</span>.<span class="me1">add</span><span class="br0">(</span>Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"><span class="co1">//run the tests</span></div></li><li class="li1"><div class="de1">Y.<span class="me1">Test</span>.<span class="me1">Runner</span>.<span class="me1">run</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;"><span class="co1">//create the console</span> |
|
|
605 |
<span class="kw2">var</span> r <span class="sy0">=</span> <span class="kw2">new</span> Y.<span class="me1">Console</span><span class="br0">(</span><span class="br0">{</span> |
|
|
606 |
verbose <span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span> |
|
|
607 |
newestOnTop <span class="sy0">:</span> <span class="kw2">false</span> |
|
|
608 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
609 |
|
|
|
610 |
r.<span class="me1">render</span><span class="br0">(</span><span class="st0">'#testLogger'</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
611 |
|
|
|
612 |
<span class="co1">//add the test suite to the runner's queue</span> |
|
|
613 |
Y.<span class="me1">Test</span>.<span class="me1">Runner</span>.<span class="me1">add</span><span class="br0">(</span>Y.<span class="me1">example</span>.<span class="me1">test</span>.<span class="me1">AdvancedOptionsTestCase</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
614 |
|
|
|
615 |
<span class="co1">//run the tests</span> |
|
|
616 |
Y.<span class="me1">Test</span>.<span class="me1">Runner</span>.<span class="me1">run</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax8-plain">//create the console |
|
|
617 |
var r = new Y.Console({ |
|
|
618 |
verbose : true, |
|
|
619 |
newestOnTop : false |
|
|
620 |
}); |
|
|
621 |
|
|
|
622 |
r.render('#testLogger'); |
|
|
623 |
|
|
|
624 |
//add the test suite to the runner's queue |
|
|
625 |
Y.Test.Runner.add(Y.example.test.AdvancedOptionsTestCase); |
|
|
626 |
|
|
|
627 |
//run the tests |
|
|
628 |
Y.Test.Runner.run();</textarea></div> |
|
|
629 |
<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 |
|
|
630 |
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 |
|
|
631 |
<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>, |
|
|
632 |
this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its |
|
|
633 |
queue and displays the results in the <code>Y.Console</code>.</p> </div> |
|
|
634 |
<div class="yui-u sidebar"> |
|
|
635 |
|
|
|
636 |
|
|
|
637 |
<div id="examples" class="mod box4"> |
|
|
638 |
<div class="hd"> |
|
|
639 |
<h4> |
|
|
640 |
Test Examples:</h4> |
|
|
641 |
</div> |
|
|
642 |
<div class="bd"> |
|
|
643 |
<ul> |
|
|
644 |
<li><a href='../test/test-simple-example.html'>Simple Testing Example</a></li><li class='selected'><a href='../test/test-advanced-test-options.html'>Advanced Test Options</a></li><li><a href='../test/test-array-tests.html'>Array Processing</a></li><li><a href='../test/test-async-test.html'>Asynchronous Testing</a></li><li><a href='../test/test-async-event-tests.html'>Asynchronous Event Testing</a></li> </ul> |
|
|
645 |
</div> |
|
|
646 |
</div> |
|
|
647 |
|
|
|
648 |
<div class="mod box4"> |
|
|
649 |
<div class="hd"> |
|
|
650 |
<h4>More Test Resources:</h4> |
|
|
651 |
</div> |
|
|
652 |
<div class="bd"> |
|
|
653 |
<ul> |
|
|
654 |
<!-- <li><a href="http://developer.yahoo.com/yui/test/">User's Guide</a> (external)</li> --> |
|
|
655 |
<li><a href="../../api/module_test.html">API Documentation</a></li></ul> |
|
|
656 |
</div> |
|
|
657 |
</div> |
|
|
658 |
</div> |
|
|
659 |
</div> |
|
|
660 |
|
|
|
661 |
</div> |
|
|
662 |
</div> |
|
|
663 |
|
|
|
664 |
|
|
|
665 |
<div class="yui-b toc3" id="tocWrapper"> |
|
|
666 |
<!-- TABLE OF CONTENTS --> |
|
|
667 |
<div id="toc"> |
|
|
668 |
|
|
|
669 |
<ul> |
|
|
670 |
<li class="sect first">YUI 3 Resources</li><li class="item"><a title="YUI 3 -- Yahoo! User Interface (YUI) Library" href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site</a></li><li class="item"><a title="Examples of every YUI utility and control in action" href="../../examples/">YUI 3 Examples</a></li><li class="item"><a title="Instantly searchable API documentation for the entire YUI library." href="../../api/">YUI 3 API Docs</a></li><li class="item"><a title="YUI 3 Dependency Configurator -- configure your custom YUI implementation" href="http://developer.yahoo.com/yui/3/configurator">YUI 3 Dependency Configurator</a></li><li class="item"><a title="The YUI 3 Forum on YUILibrary.com" href="http://yuilibrary.com/forum/viewforum.php?f=15">YUI 3 Forums (external)</a></li><li class="item"><a title="Found a bug or a missing feature? Let us know on YUILibrary.com." href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/license.html">YUI License</a></li><li class="item"><a title="Download and fork the YUI project on GitHub" href="http://github.com/yui">YUI on Github</a></li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="item"><a title="YUI Global Object - Functional Examples" href="../../examples/yui/index.html">YUI Global Object</a></li><li class="item"><a title="Event - Functional Examples" href="../../examples/event/index.html">Event</a></li><li class="item"><a title="Node - Functional Examples" href="../../examples/node/index.html">Node</a></li><li class="sect">YUI 3 Component Infrastructure - Examples</li><li class="item"><a title="Attribute - Functional Examples" href="../../examples/attribute/index.html">Attribute</a></li><li class="item"><a title="Plugin - Functional Examples" href="../../examples/plugin/index.html">Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Widget - Functional Examples" href="../../examples/widget/index.html">Widget <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 Utilities - Examples</li><li class="item"><a title="Animation - Functional Examples" href="../../examples/anim/index.html">Animation</a></li><li class="item"><a title="AsyncQueue - Functional Examples" href="../../examples/async-queue/index.html">AsyncQueue</a></li><li class="item"><a title="Browser History - Functional Examples" href="../../examples/history/index.html">Browser History</a></li><li class="item"><a title="Cache - Functional Examples" href="../../examples/cache/index.html">Cache</a></li><li class="item"><a title="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="DataSchema - Functional Examples" href="../../examples/dataschema/index.html">DataSchema <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="DataSource - Functional Examples" href="../../examples/datasource/index.html">DataSource <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="DataType - Functional Examples" href="../../examples/datatype/index.html">DataType <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Drag & Drop - Functional Examples" href="../../examples/dd/index.html">Drag & Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</a></li><li class="item"><a title="ImageLoader - Functional Examples" href="../../examples/imageloader/index.html">ImageLoader</a></li><li class="item"><a title="IO - Functional Examples" href="../../examples/io/index.html">IO</a></li><li class="item"><a title="JSON (JavaScript Object Notation) - Functional Examples" href="../../examples/json/index.html">JSON</a></li><li class="item"><a title="Stylesheet - Functional Examples" href="../../examples/stylesheet/index.html">Stylesheet</a></li><li class="sect">YUI 3 Widgets - Examples</li><li class="item"><a title="Overlay - Functional Examples" href="../../examples/overlay/index.html">Overlay <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Slider - Functional Examples" href="../../examples/slider/index.html">Slider <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 Node Plugins - Examples</li><li class="item"><a title="FocusManager Node Plugin - Functional Examples" href="../../examples/node-focusmanager/index.html">FocusManager Node Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="MenuNav Node Plugin - Functional Examples" href="../../examples/node-menunav/index.html">MenuNav Node Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 CSS - Examples</li><li class="item"><a title="YUI CSS Reset - Functional Examples" href="../../examples/cssreset/index.html">CSS Reset</a></li><li class="item"><a title="YUI Fonts - Functional Examples" href="../../examples/cssfonts/index.html">CSS Fonts</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</a></li><li class="sect">YUI 3 Developer Tools - Examples</li><li class="item"><a title="Console - Functional Examples" href="../../examples/console/index.html">Console <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Console Filters Plugin- Functional Examples" href="../../examples/console-filters/index.html">Plugin.ConsoleFilters <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Profiler - Functional Examples" href="../../examples/profiler/index.html">Profiler</a></li><li class="selected "><a title="Test - Functional Examples" href="../../examples/test/index.html">Test</a></li><li class="sect">Other Useful YUI 3 Resources</li><li class="item"><a title="Answers to Frequently Asked Questions about the YUI Library" href="http://developer.yahoo.com/yui/articles/faq/">YUI FAQ (external)</a></li><li class="item"><a title="Yahoo!'s philosophy of Graded Browser Support" href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support (external)</a></li><li class="item"><a title="Videos and podcasts from the YUI Team and from the Yahoo! frontend engineering community." href="http://developer.yahoo.com/yui/theater/">YUI Theater (external)</a></li></ul> |
|
|
671 |
</div> |
|
|
672 |
</div> |
|
|
673 |
</div><!--closes bd--> |
|
|
674 |
|
|
|
675 |
<div id="ft"> |
|
|
676 |
<p class="first">Copyright © 2009 Yahoo! Inc. All rights reserved.</p> |
|
|
677 |
<p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> - |
|
|
678 |
<a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> - |
|
|
679 |
<a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> - |
|
|
680 |
<a href="http://careers.yahoo.com/">Job Openings</a></p> |
|
|
681 |
</div> |
|
|
682 |
</div> |
|
|
683 |
<script language="javascript"> |
|
|
684 |
var yuiConfig = {base:"../../build/", timeout: 10000}; |
|
|
685 |
</script> |
|
|
686 |
<script src="../../assets/syntax.js"></script> |
|
|
687 |
<script src="../../assets/dpSyntaxHighlighter.js"></script> |
|
|
688 |
<script language="javascript"> |
|
|
689 |
dp.SyntaxHighlighter.HighlightAll('code'); |
|
|
690 |
</script> |
|
|
691 |
</body> |
|
|
692 |
</html> |