forked from yui/yuitest
/
basic-usage-example.htm
270 lines (207 loc) · 9.01 KB
/
basic-usage-example.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Basic Usage Example</title>
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script>
<script type="text/javascript" src="../javascript/build/yuitest/yuitest-min.js"></script>
<style type="text/css">
.yui3-skin-sam .yui3-console-entry-pass .yui3-console-entry-cat {
background-color: green;
color: #fff;
}
.yui3-skin-sam .yui3-console-entry-fail .yui3-console-entry-cat {
background-color: red;
color: #fff;
}
.yui3-skin-sam .yui3-console-entry-ignore .yui3-console-entry-cat {
background-color: #666;
}
</style>
<body class="yui3-skin-sam yui-skin-sam">
<h1>Basic Usage Example</h1>
<p>This example shows basic usage of the YUI Test standalone for testing browser-based JavaScript code. Two different <code>TestCase</code> objects are created and added to a <code>TestSuite</code> object. The <code>TestRunner</code> is then used to run the tests once the page has loaded. The YUI 3 <code>Console</code> is used to output the results of the tests.</p>
<div id="testLogger"></div>
<script type="text/javascript">
YUI().use("console", function (Y) {
var dataTestCase = new YUITest.TestCase({
//name of the test case - if not provided, one is auto-generated
name : "Data Tests",
//---------------------------------------------------------------------
// setUp and tearDown methods - optional
//---------------------------------------------------------------------
/*
* Sets up data that is needed by each test.
*/
setUp : function () {
this.data = {
name: "test",
year: 2007,
beta: true
};
},
/*
* Cleans up everything that was created by setUp().
*/
tearDown : function () {
delete this.data;
},
//---------------------------------------------------------------------
// Test methods - names must begin with "test"
//---------------------------------------------------------------------
testName : function () {
var Assert = YUITest.Assert;
Assert.isObject(this.data);
Assert.isString(this.data.name);
Assert.areEqual("test", this.data.name);
},
testYear : function () {
var Assert = YUITest.Assert;
Assert.isObject(this.data);
Assert.isNumber(this.data.year);
Assert.areEqual(2007, this.data.year);
},
testBeta : function () {
var Assert = YUITest.Assert;
Assert.isObject(this.data);
Assert.isBoolean(this.data.beta);
Assert.isTrue(this.data.beta);
},
//---------------------------------------------------------------------
// Test methods - also, may just have a space in the name
//---------------------------------------------------------------------
"This test should fail": function(){
var Assert = YUITest.Assert;
Assert.fail("This test was supposed to fail.");
}
});
var arrayTestCase = new YUITest.TestCase({
//name of the test case - if not provided, one is auto-generated
name : "Array Tests",
//---------------------------------------------------------------------
// setUp and tearDown methods - optional
//---------------------------------------------------------------------
/*
* Sets up data that is needed by each test.
*/
setUp : function () {
this.data = [0,1,2,3,4]
},
/*
* Cleans up everything that was created by setUp().
*/
tearDown : function () {
delete this.data;
},
//---------------------------------------------------------------------
// Test methods - names must begin with "test"
//---------------------------------------------------------------------
testPop : function () {
var Assert = YUITest.Assert;
var value = this.data.pop();
Assert.areEqual(4, this.data.length);
Assert.areEqual(4, value);
},
testPush : function () {
var Assert = YUITest.Assert;
this.data.push(5);
Assert.areEqual(6, this.data.length);
Assert.areEqual(5, this.data[5]);
},
testSplice : function () {
var Assert = YUITest.Assert;
this.data.splice(2, 1, 6, 7);
Assert.areEqual(6, this.data.length);
Assert.areEqual(6, this.data[2]);
Assert.areEqual(7, this.data[3]);
}
});
var suite = new YUITest.TestSuite("Example Suite");
suite.add(dataTestCase);
suite.add(arrayTestCase);
//create the console
var r = new Y.Console({
newestOnTop : false,
style: 'block' // to anchor in the example content
});
r.render('#testLogger');
var TestRunner = YUITest.TestRunner;
TestRunner.add(suite);
//function to handle events generated by the testrunner
function logEvent(event){
//data variables
var message = "",
messageType = "";
switch(event.type){
case TestRunner.BEGIN_EVENT:
message = "Testing began at " + (new Date()).toString() + ".";
messageType = "info";
break;
case TestRunner.COMPLETE_EVENT:
message = Y.substitute("Testing completed at " +
(new Date()).toString() + ".\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
case TestRunner.TEST_FAIL_EVENT:
message = event.testName + ": failed.\n" + event.error.getMessage();
messageType = "fail";
break;
case TestRunner.TEST_IGNORE_EVENT:
message = event.testName + ": ignored.";
messageType = "ignore";
break;
case TestRunner.TEST_PASS_EVENT:
message = event.testName + ": passed.";
messageType = "pass";
break;
case TestRunner.TEST_SUITE_BEGIN_EVENT:
message = "Test suite \"" + event.testSuite.name + "\" started.";
messageType = "info";
break;
case TestRunner.TEST_SUITE_COMPLETE_EVENT:
message = Y.substitute("Test suite \"" +
event.testSuite.name + "\" completed" + ".\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
case TestRunner.TEST_CASE_BEGIN_EVENT:
message = "Test case \"" + event.testCase.name + "\" started.";
messageType = "info";
break;
case TestRunner.TEST_CASE_COMPLETE_EVENT:
message = Y.substitute("Test case \"" +
event.testCase.name + "\" completed.\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
default:
message = "Unexpected event " + event.type;
message = "info";
}
//only log if required
Y.log(message, messageType, "TestRunner");
}
//listen for events to publish to the logger
TestRunner.attach(TestRunner.BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_CASE_BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_CASE_COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_SUITE_BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_SUITE_COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_PASS_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_FAIL_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_IGNORE_EVENT, logEvent);
//run the tests
TestRunner.run();
});
</script>
</body>
</html>