YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: YUI Test Utility: Asynchronous Testing

# YUI Test Utility: Asynchronous Testing

This example shows how to create an asynchronous test with the YUI Test framework for testing browser-based JavaScript code. A `TestCase` object is created with a test that waits for a few seconds before continuing. The `TestRunner` is then used to run the tests once the page has loaded.

View example in new window.

## Asynchronous Test Example

This example begins by creating a namespace:

YAHOO.namespace("example.yuitest");

This namespace serves as the core object upon which others will be added (to prevent creating global objects).

### Creating the TestCase

The first step is to create a new `TestCase` object called `AsyncTestCase`. To do so, using the `TestCase` constructor and pass in an object literal containing information about the tests to be run:

YAHOO.example.yuitest.AsyncTestCase = new YAHOO.tool.TestCase({ //name of the test case - if not provided, one is auto-generated name : "Asynchronous Tests", //--------------------------------------------------------------------- // setUp and tearDown methods - optional //--------------------------------------------------------------------- /* * Sets up data that is needed by each test. */ setUp : function () { this.data = { name: "yuitest", year: 2007, beta: true }; }, /* * Cleans up everything that was created by setUp(). */ tearDown : function () { delete this.data; }, //--------------------------------------------------------------------- // Test methods - names must begin with "test" //--------------------------------------------------------------------- testWait : function (){ var Assert = YAHOO.util.Assert; //do some assertions now Assert.isTrue(this.data.beta); Assert.isNumber(this.data.year); //wait five seconds and do some more this.wait(function(){ Assert.isString(this.data.name); }, 5000); } });

The object literal passed into the constructor contains two different sections. The first section contains the `name` property, which is used to determine which `TestCase` is being executed. A name is necessary, so one is generated if it isn't specified.

Next, the `setUp()` and `tearDown()` methods are included. The `setUp()` method is used in a `TestCase` 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, `setUp()` creates a data object. The `tearDown()` is responsible for undoing what was done in `setUp()`. It is run immediately after each test is run and, in this case, deletes the data object that was created by `setUp`. These methods are optional.

The second section contains the actual tests to be run. The only test is `testWait()`, which demonstrates using the `wait()` method to delay test execution. There are two arguments passed in: a function to run once the test resumes and the number of milliseconds to wait before running this function (same basic format as `setTimeout()`). When the test resumes, the function is executed in the context of the `TestCase` object, meaning that it still has access to all of the same data as the test that called `wait()`, including properties and methods on the `TestCase` itself. This example shows the anonymous function using both the `Assert` object and the `data` property of the `TestCase`.

### Running the tests

With all of the tests defined, the last step is to run them. This initialization is assigned to take place when the document tree has been completely loaded by using the Event utility's `onDOMReady()` method:

YAHOO.util.Event.onDOMReady(function (){ //create the logger var logger = new YAHOO.tool.TestLogger(); //add the test suite to the runner's queue YAHOO.tool.TestRunner.add(YAHOO.example.yuitest.AsyncTestCase); //run the tests YAHOO.tool.TestRunner.run(); });

Before running the tests, it's necessary to create a `TestLogger` object to display the results (otherwise the tests would run but you wouldn't see the results). After that, the `TestRunner` is loaded with the `TestCase` object by calling `add()` (any number of `TestCase` and `TestSuite` objects can be added to a `TestRunner`, this example only adds one for simplicity). The very last step is to call `run()`, which begins executing the tests in its queue and displays the results in the `TestLogger`.

### YUI Test Utility Examples:

- Simple Testing Example
- Advanced Test Options
- Array Processing
- Asynchronous Testing
- Asynchronous Event Testing

### More YUI Test Utility Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings