5
<title>Example: Simple Testing Example</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Simple Testing Example</h1>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro">
23
<p>This example shows basic usage of the YUI Test framework for testing browser-based JavaScript code.
24
Two different <a href="index.html#testcase"><code>TestCase</code></a> objects are created and added to a
25
<a href="index.html#testsuite"><code>TestSuite</code></a> object. The <a href="index.html#testrunner"><code>TestRunner</code></a>
26
is then used to run the tests once the page has loaded.</p>
29
<div class="example yui3-skin-sam">
35
#testLogger .yui3-console .yui3-console-title {
43
#testLogger .yui3-console .yui3-console-entry-meta {
47
.yui3-skin-sam .yui3-console-entry-pass .yui3-console-entry-cat {
53
<div id="testLogger"></div>
56
YUI().use('node', 'console', 'test', function (Y) {
58
Y.namespace("example.test");
60
Y.example.test.DataTestCase = new Y.Test.Case({
62
//name of the test case - if not provided, one is auto-generated
65
//---------------------------------------------------------------------
66
// setUp and tearDown methods - optional
67
//---------------------------------------------------------------------
70
* Sets up data that is needed by each test.
81
* Cleans up everything that was created by setUp().
83
tearDown : function () {
87
//---------------------------------------------------------------------
88
// Test methods - names must begin with "test"
89
//---------------------------------------------------------------------
91
testName : function () {
92
var Assert = Y.Assert;
94
Assert.isObject(this.data);
95
Assert.isString(this.data.name);
96
Assert.areEqual("test", this.data.name);
99
testYear : function () {
100
var Assert = Y.Assert;
102
Assert.isObject(this.data);
103
Assert.isNumber(this.data.year);
104
Assert.areEqual(2007, this.data.year);
107
testBeta : function () {
108
var Assert = Y.Assert;
110
Assert.isObject(this.data);
111
Assert.isBoolean(this.data.beta);
112
Assert.isTrue(this.data.beta);
117
Y.example.test.ArrayTestCase = new Y.Test.Case({
119
//name of the test case - if not provided, one is auto-generated
120
name : "Array Tests",
122
//---------------------------------------------------------------------
123
// setUp and tearDown methods - optional
124
//---------------------------------------------------------------------
127
* Sets up data that is needed by each test.
129
setUp : function () {
130
this.data = [0,1,2,3,4]
134
* Cleans up everything that was created by setUp().
136
tearDown : function () {
140
//---------------------------------------------------------------------
141
// Test methods - names must begin with "test"
142
//---------------------------------------------------------------------
144
testPop : function () {
145
var Assert = Y.Assert;
147
var value = this.data.pop();
149
Assert.areEqual(4, this.data.length);
150
Assert.areEqual(4, value);
153
testPush : function () {
154
var Assert = Y.Assert;
158
Assert.areEqual(6, this.data.length);
159
Assert.areEqual(5, this.data[5]);
162
testSplice : function () {
163
var Assert = Y.Assert;
165
this.data.splice(2, 1, 6, 7);
167
Assert.areEqual(6, this.data.length);
168
Assert.areEqual(6, this.data[2]);
169
Assert.areEqual(7, this.data[3]);
174
Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite");
175
Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase);
176
Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase);
179
var r = new Y.Console({
181
style: 'block' // to anchor in the example content
184
r.render('#testLogger');
186
Y.Test.Runner.add(Y.example.test.ExampleSuite);
196
<h2 class="first" id="simple-test-example">Simple Test Example</h2>
198
<p>This example begins by creating a namespace:</p>
199
<pre class="code prettyprint">Y.namespace("example.test");</pre>
201
<p>This namespace serves as the core object upon which others will be added (to prevent creating global objects).</p>
203
<h3 id="creating-the-first-testcase">Creating the first TestCase</h3>
205
<p>The first step is to create a new <code>Y.Test.Case</code> object called <code>DataTestCase</code>.
206
To do so, using the <code>Y.Test.Case</code> constructor and pass in an object literal containing information about the tests to be run:</p>
207
<pre class="code prettyprint">Y.example.test.DataTestCase = new Y.Test.Case({
209
//name of the test case - if not provided, one is auto-generated
210
name : "Data Tests",
212
//---------------------------------------------------------------------
213
// setUp and tearDown methods - optional
214
//---------------------------------------------------------------------
217
* Sets up data that is needed by each test.
219
setUp : function () {
221
name: "test",
228
* Cleans up everything that was created by setUp().
230
tearDown : function () {
234
//---------------------------------------------------------------------
235
// Test methods - names must begin with "test"
236
//---------------------------------------------------------------------
238
testName : function () {
239
var Assert = Y.Assert;
241
Assert.isObject(this.data);
242
Assert.isString(this.data.name);
243
Assert.areEqual("test", this.data.name);
246
testYear : function () {
247
var Assert = Y.Assert;
249
Assert.isObject(this.data);
250
Assert.isNumber(this.data.year);
251
Assert.areEqual(2007, this.data.year);
254
testBeta : function () {
255
var Assert = Y.Assert;
257
Assert.isObject(this.data);
258
Assert.isBoolean(this.data.beta);
259
Assert.isTrue(this.data.beta);
264
<p>The object literal passed into the constructor contains a number of different sections. The first section contains the <code>name</code> property,
265
which is used to determine which <code>Y.Test.Case</code> is being executed. A name is necessary, so one is generated if it isn't specified.</p>
266
<p>Next, the <code>setUp()</code> and <code>tearDown()</code> methods are included. The <code>setUp()</code> method is used in a <code>Y.Test.Case</code>
267
to set up data that may be needed for tests to be completed. This method is called immediately before each test is executed. For this example,
268
<code>setUp()</code> creates a data object. The <code>tearDown()</code> is responsible for undoing what was done in <code>setUp()</code>. It is
269
run immediately after each test is run and, in this case, deletes the data object that was created by <code>setUp</code>. These methods are optional.</p>
270
<p>The last section contains the actual tests to be run. Test method names must always begin with the word "test" (all lowercase) in order
271
to differentiate them from other methods that may be added to the object.</p>
272
<p>The first test in this object is <code>testName()</code>, which runs
273
various assertions on <code>data.name</code>. Inside of this method, a shortcut to <code>Y.Assert</code> is set up and used to run three
274
assertions: <code>isObject()</code> on <code>data</code>, <code>isString()</code> on <code>data.name</code> and <code>areEqual()</code> to compare
275
<code>data.name</code> to the expected value, "test". These assertions are arranged in order from least-specific to most-specific,
276
which is the recommended way to arrange your assertions. Basically, the third assertion is useless to run unless the second has passes and the second
277
can't possibly pass unless the first passed. Both <code>isObject()</code> and <code>isString()</code> accept a single argument, which is the value
278
to test (you could optionally include a failure message as a second argument, though this is not required). The <code>areEqual()</code> method
279
expects two arguments, the first being the expected value ("test") and the second being the actual value (<code>data.name</code>).</p>
280
<p>The second and third tests follow the same pattern as the first with the exception that they work with different data types. The <code>testYear()</code>
281
method works with <code>data.year</code>, which is a number and so runs tests specifically for numbers (<code>areEqual()</code> can be used with
282
all data types). The <code>testBeta()</code> method works with <code>data.beta</code>, which is a Boolean, and so it uses the <code>isTrue()</code>
283
assertion instead of <code>areEqual()</code> (though it could also use <code>areEqual(true, this.data.beta)</code>).</p>
285
<h3 id="creating-the-second-testcase">Creating the second TestCase</h3>
287
<p>Although it's possible that you'll have only one <code>Y.Test.Case</code> object, typically there is more than one, and so this example includes
288
a second <code>Y.Test.Case</code>. This one tests some of the built-in functions of the <code>Array</code> object:</p>
289
<pre class="code prettyprint">Y.example.test.ArrayTestCase = new Y.Test.Case({
291
//name of the test case - if not provided, one is auto-generated
292
name : "Array Tests",
294
//---------------------------------------------------------------------
295
// setUp and tearDown methods - optional
296
//---------------------------------------------------------------------
299
* Sets up data that is needed by each test.
301
setUp : function () {
302
this.data = [0,1,2,3,4]
306
* Cleans up everything that was created by setUp().
308
tearDown : function () {
312
//---------------------------------------------------------------------
313
// Test methods - names must begin with "test"
314
//---------------------------------------------------------------------
316
testPop : function () {
317
var Assert = Y.Assert;
319
var value = this.data.pop();
321
Assert.areEqual(4, this.data.length);
322
Assert.areEqual(4, value);
325
testPush : function () {
326
var Assert = Y.Assert;
330
Assert.areEqual(6, this.data.length);
331
Assert.areEqual(5, this.data[5]);
334
testSplice : function () {
335
var Assert = Y.Assert;
337
this.data.splice(2, 1, 6, 7);
339
Assert.areEqual(6, this.data.length);
340
Assert.areEqual(6, this.data[2]);
341
Assert.areEqual(7, this.data[3]);
346
<p>As with the first <code>Y.Test.Case</code>, this one is split up into three sections: the name, the <code>setUp()</code> and <code>tearDown()</code>
347
methods, and the test methods. The <code>setUp()</code> method in this <code>Y.Test.Case</code> creates an array of data to be used by the various
348
tests while the <code>tearDown()</code> method destroys the array. The test methods are very simple, testing the <code>pop()</code>, <code>push()</code>,
349
and <code>splice()</code> methods. Each test method uses <code>areEqual()</code> exclusively, to show the different ways that it can be used.
350
The <code>testPop()</code> method calls <code>pop()</code> on the array of values, then verifies that the length of the array has changed and
351
that the value popped off is 4; the <code>testPush()</code> pushes a new value (5) onto the array and then verifies that the length of the array has
352
changed and that the value is included in the correct location; the <code>testSplice()</code> method tests <code>splice()</code> by removing one
353
value that's already in the array and inserting two in its place.</p>
355
<h3 id="creating-the-testsuite">Creating the TestSuite</h3>
356
<p>To better organize the two <code>Y.Test.Case</code> objects, a <code>Y.Test.Suite</code> is created and those two <code>Y.Test.Case</code> objects are
358
<pre class="code prettyprint">Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite");
359
Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase);
360
Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase);</pre>
362
<p>The first line creates a new <code>Y.Test.Suite</code> object using its constructor, which accepts a single argument - the name of the suite. As with
363
the name of a <code>Y.Test.Case</code>, the <code>Y.Test.Suite</code> name is used to determine where execution is when tests are being executed. Although
364
not required (one is generated if it's not provided), it is recommended that you select a meaningful name to aid in debugging.</p>
365
<p>Any number of <code>Y.Test.Case</code> and <code>Y.Test.Suite</code> objects can be added to a <code>Y.Test.Suite</code> by using the <code>add()</code>
366
method. In this example, the two <code>Y.Test.Case</code> objects created earlier are added to the <code>Y.Test.Suite</code>.</p>
368
<h3 id="running-the-tests">Running the tests</h3>
370
<p>With all of the tests defined, the last step is to run them. This initialization is assigned to take place when all of the YUI
371
components have been loaded:</p>
373
<pre class="code prettyprint">//create the console
374
var r = new Y.Console({
379
r.render('#testLogger');
381
Y.Test.Runner.add(Y.example.test.ExampleSuite);
383
//run the tests
384
Y.Test.Runner.run();</pre>
387
<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
388
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
389
<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>,
390
this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its
391
queue and displays the results in the <code>Y.Console</code>.</p>
393
<h2 id="complete-example-source">Complete Example Source</h2>
395
<pre class="code prettyprint"><div id="testLogger"></div>
398
YUI().use('node', 'console', 'test', function (Y) {
400
Y.namespace("example.test");
402
Y.example.test.DataTestCase = new Y.Test.Case({
404
//name of the test case - if not provided, one is auto-generated
405
name : "Data Tests",
407
//---------------------------------------------------------------------
408
// setUp and tearDown methods - optional
409
//---------------------------------------------------------------------
412
* Sets up data that is needed by each test.
414
setUp : function () {
416
name: "test",
423
* Cleans up everything that was created by setUp().
425
tearDown : function () {
429
//---------------------------------------------------------------------
430
// Test methods - names must begin with "test"
431
//---------------------------------------------------------------------
433
testName : function () {
434
var Assert = Y.Assert;
436
Assert.isObject(this.data);
437
Assert.isString(this.data.name);
438
Assert.areEqual("test", this.data.name);
441
testYear : function () {
442
var Assert = Y.Assert;
444
Assert.isObject(this.data);
445
Assert.isNumber(this.data.year);
446
Assert.areEqual(2007, this.data.year);
449
testBeta : function () {
450
var Assert = Y.Assert;
452
Assert.isObject(this.data);
453
Assert.isBoolean(this.data.beta);
454
Assert.isTrue(this.data.beta);
459
Y.example.test.ArrayTestCase = new Y.Test.Case({
461
//name of the test case - if not provided, one is auto-generated
462
name : "Array Tests",
464
//---------------------------------------------------------------------
465
// setUp and tearDown methods - optional
466
//---------------------------------------------------------------------
469
* Sets up data that is needed by each test.
471
setUp : function () {
472
this.data = [0,1,2,3,4]
476
* Cleans up everything that was created by setUp().
478
tearDown : function () {
482
//---------------------------------------------------------------------
483
// Test methods - names must begin with "test"
484
//---------------------------------------------------------------------
486
testPop : function () {
487
var Assert = Y.Assert;
489
var value = this.data.pop();
491
Assert.areEqual(4, this.data.length);
492
Assert.areEqual(4, value);
495
testPush : function () {
496
var Assert = Y.Assert;
500
Assert.areEqual(6, this.data.length);
501
Assert.areEqual(5, this.data[5]);
504
testSplice : function () {
505
var Assert = Y.Assert;
507
this.data.splice(2, 1, 6, 7);
509
Assert.areEqual(6, this.data.length);
510
Assert.areEqual(6, this.data[2]);
511
Assert.areEqual(7, this.data[3]);
516
Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite");
517
Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase);
518
Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase);
520
//create the console
521
var r = new Y.Console({
523
style: 'block' // to anchor in the example content
526
r.render('#testLogger');
528
Y.Test.Runner.add(Y.example.test.ExampleSuite);
530
//run the tests
534
</script></pre>
539
<div id="sidebar" class="yui3-u">
541
<div id="toc" class="sidebox">
543
<h2 class="no-toc">Table of Contents</h2>
549
<a href="#simple-test-example">Simple Test Example</a>
552
<a href="#creating-the-first-testcase">Creating the first TestCase</a>
555
<a href="#creating-the-second-testcase">Creating the second TestCase</a>
558
<a href="#creating-the-testsuite">Creating the TestSuite</a>
561
<a href="#running-the-tests">Running the tests</a>
566
<a href="#complete-example-source">Complete Example Source</a>
574
<div class="sidebox">
576
<h2 class="no-toc">Examples</h2>
580
<ul class="examples">
583
<li data-description="Demonstrates basic usage of YUI Test for setting up and running tests.">
584
<a href="test-simple-example.html">Simple Testing Example</a>
589
<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.">
590
<a href="test-advanced-test-options.html">Advanced Test Options</a>
595
<li data-description="Demonstrates how to use the ArrayAssert object to test array data.">
596
<a href="test-array-tests.html">Array Processing</a>
601
<li data-description="Demonstrates basic asynchronous tests.">
602
<a href="test-async-test.html">Asynchronous Testing</a>
607
<li data-description="Demonstrates using events with asynchronous tests.">
608
<a href="test-async-event-tests.html">Asynchronous Event Testing</a>
622
<script src="../assets/vendor/prettify/prettify-min.js"></script>
623
<script>prettyPrint();</script>