5
<title>Example: Asynchronous Testing</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: Asynchronous Testing</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 how to create an asynchronous test with the YUI Test framework for testing browser-based JavaScript code.
24
A <code>Y.Test.Case</code> object is created with a test that waits for a
25
few seconds before continuing. The <code>Y.Test.Runner</code>
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.AsyncTestCase = new Y.Test.Case({
62
//name of the test case - if not provided, one is auto-generated
63
name : "Asynchronous Tests",
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
testWait : function (){
92
var Assert = Y.Assert;
94
//do some assertions now
95
Assert.isTrue(this.data.beta);
96
Assert.isNumber(this.data.year);
98
//wait five seconds and do some more
101
Assert.isString(this.data.name);
110
var r = new Y.Console({
112
style: 'block' // to anchor in the example content
115
r.render('#testLogger');
117
Y.Test.Runner.add(Y.example.test.AsyncTestCase);
127
<h2 class="first" id="asynchronous-test-example">Asynchronous Test Example</h2>
129
<p>This example begins by creating a namespace:</p>
130
<pre class="code prettyprint">Y.namespace("example.test");</pre>
132
<p>This namespace serves as the core object upon which others will be added (to prevent creating global objects).</p>
134
<h3 id="creating-the-testcase">Creating the TestCase</h3>
136
<p>The first step is to create a new <code>Y.Test.Case</code> object called <code>AsyncTestCase</code>.
137
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>
138
<pre class="code prettyprint">Y.example.test.AsyncTestCase = new Y.Test.Case({
140
//name of the test case - if not provided, one is auto-generated
141
name : "Asynchronous Tests",
143
//---------------------------------------------------------------------
144
// setUp and tearDown methods - optional
145
//---------------------------------------------------------------------
148
* Sets up data that is needed by each test.
150
setUp : function () {
152
name: "test",
159
* Cleans up everything that was created by setUp().
161
tearDown : function () {
165
//---------------------------------------------------------------------
166
// Test methods - names must begin with "test"
167
//---------------------------------------------------------------------
169
testWait : function (){
170
var Assert = Y.Assert;
172
//do some assertions now
173
Assert.isTrue(this.data.beta);
174
Assert.isNumber(this.data.year);
176
//wait five seconds and do some more
177
this.wait(function(){
179
Assert.isString(this.data.name);
187
<p>The object literal passed into the constructor contains two different sections. The first section contains the <code>name</code> property,
188
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>
189
<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>
190
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,
191
<code>setUp()</code> creates a data object. The <code>tearDown()</code> is responsible for undoing what was done in <code>setUp()</code>. It is
192
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>
193
<p>The second section contains the actual tests to be run. The only test is <code>testWait()</code>, which demonstrates using
194
the <code>wait()</code> method to delay test execution. There are two arguments passed in: a function to run once the test resumes
195
and the number of milliseconds to wait before running this function (same basic format as <code>setTimeout()</code>). When
196
the test resumes, the function is executed in the context of the <code>Y.Test.Case</code> object, meaning that it still has
197
access to all of the same data as the test that called <code>wait()</code>, including properties and methods on the <code>Y.Test.Case</code>
198
itself. This example shows the anonymous function using both the <code>Y.Assert</code> object and the <code>data</code> property
199
of the <code>Y.Test.Case</code>.</p>
201
<h3 id="running-the-tests">Running the tests</h3>
203
<p>With all of the tests defined, the last step is to run them:</p>
205
<pre class="code prettyprint">//create the console
206
var r = new Y.Console({
211
r.render('#testLogger');
213
//add the test suite to the runner's queue
214
Y.Test.Runner.add(Y.example.test.AsyncTestCase);
216
//run the tests
217
Y.Test.Runner.run();</pre>
220
<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
221
but you wouldn't see the results). After that, the <code>Y.Test.Runner</code> is loaded with the <code>Y.Test.Case</code> object by calling
222
<code>add()</code> (any number of <code>Y.Test.Case</code> and <code>TestSuite</code> objects can be added to a <code>TestRunner</code>,
223
this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its
224
queue and displays the results in the <code>Y.Console</code>.</p>
226
<h2 id="complete-example-source">Complete Example Source</h2>
228
<pre class="code prettyprint"><div id="testLogger"></div>
231
YUI().use('node', 'console', 'test',function (Y) {
233
Y.namespace("example.test");
235
Y.example.test.AsyncTestCase = new Y.Test.Case({
237
//name of the test case - if not provided, one is auto-generated
238
name : "Asynchronous Tests",
240
//---------------------------------------------------------------------
241
// setUp and tearDown methods - optional
242
//---------------------------------------------------------------------
245
* Sets up data that is needed by each test.
247
setUp : function () {
249
name: "test",
256
* Cleans up everything that was created by setUp().
258
tearDown : function () {
262
//---------------------------------------------------------------------
263
// Test methods - names must begin with "test"
264
//---------------------------------------------------------------------
266
testWait : function (){
267
var Assert = Y.Assert;
269
//do some assertions now
270
Assert.isTrue(this.data.beta);
271
Assert.isNumber(this.data.year);
273
//wait five seconds and do some more
274
this.wait(function(){
276
Assert.isString(this.data.name);
284
//create the console
285
var r = new Y.Console({
287
style: 'block' // to anchor in the example content
290
r.render('#testLogger');
292
Y.Test.Runner.add(Y.example.test.AsyncTestCase);
294
//run the tests
298
</script></pre>
303
<div id="sidebar" class="yui3-u">
305
<div id="toc" class="sidebox">
307
<h2 class="no-toc">Table of Contents</h2>
313
<a href="#asynchronous-test-example">Asynchronous Test Example</a>
316
<a href="#creating-the-testcase">Creating the TestCase</a>
319
<a href="#running-the-tests">Running the tests</a>
324
<a href="#complete-example-source">Complete Example Source</a>
332
<div class="sidebox">
334
<h2 class="no-toc">Examples</h2>
338
<ul class="examples">
341
<li data-description="Demonstrates basic usage of YUI Test for setting up and running tests.">
342
<a href="test-simple-example.html">Simple Testing Example</a>
347
<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.">
348
<a href="test-advanced-test-options.html">Advanced Test Options</a>
353
<li data-description="Demonstrates how to use the ArrayAssert object to test array data.">
354
<a href="test-array-tests.html">Array Processing</a>
359
<li data-description="Demonstrates basic asynchronous tests.">
360
<a href="test-async-test.html">Asynchronous Testing</a>
365
<li data-description="Demonstrates using events with asynchronous tests.">
366
<a href="test-async-event-tests.html">Asynchronous Event Testing</a>
380
<script src="../assets/vendor/prettify/prettify-min.js"></script>
381
<script>prettyPrint();</script>