YUI 3.x Home -

YUI Library Examples: Test: Asynchronous Testing

Test: Asynchronous Testing

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

Asynchronous Test Example

This example begins by creating a namespace:

  1. Y.namespace("example.test");
Y.namespace("example.test");

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 Y.Test.Case object called AsyncTestCase. To do so, using the Y.Test.Case constructor and pass in an object literal containing information about the tests to be run:

  1. Y.example.test.AsyncTestCase = new Y.Test.Case({
  2.  
  3. //name of the test case - if not provided, one is auto-generated
  4. name : "Asynchronous Tests",
  5.  
  6. //---------------------------------------------------------------------
  7. // setUp and tearDown methods - optional
  8. //---------------------------------------------------------------------
  9.  
  10. /*
  11.   * Sets up data that is needed by each test.
  12.   */
  13. setUp : function () {
  14. this.data = {
  15. name: "test",
  16. year: 2007,
  17. beta: true
  18. };
  19. },
  20.  
  21. /*
  22.   * Cleans up everything that was created by setUp().
  23.   */
  24. tearDown : function () {
  25. delete this.data;
  26. },
  27.  
  28. //---------------------------------------------------------------------
  29. // Test methods - names must begin with "test"
  30. //---------------------------------------------------------------------
  31.  
  32. testWait : function (){
  33. var Assert = Y.Assert;
  34.  
  35. //do some assertions now
  36. Assert.isTrue(this.data.beta);
  37. Assert.isNumber(this.data.year);
  38.  
  39. //wait five seconds and do some more
  40. this.wait(function(){
  41.  
  42. Assert.isString(this.data.name);
  43.  
  44. }, 5000);
  45.  
  46. }
  47.  
  48. });
Y.example.test.AsyncTestCase = new Y.Test.Case({
 
    //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: "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"
    //---------------------------------------------------------------------
 
    testWait : function (){
        var Assert = Y.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 Y.Test.Case 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 Y.Test.Case 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 Y.Test.Case 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 Y.Test.Case itself. This example shows the anonymous function using both the Y.Assert object and the data property of the Y.Test.Case.

Running the tests

With all of the tests defined, the last step is to run them:

  1. //create the console
  2. var r = new Y.Console({
  3. verbose : true,
  4. newestOnTop : false
  5. });
  6.  
  7. r.render('#testLogger');
  8.  
  9. //add the test suite to the runner's queue
  10. Y.Test.Runner.add(Y.example.test.AsyncTestCase);
  11.  
  12. //run the tests
  13. Y.Test.Runner.run();
//create the console
var r = new Y.Console({
    verbose : true,
    newestOnTop : false
});
 
r.render('#testLogger');
 
//add the test suite to the runner's queue
Y.Test.Runner.add(Y.example.test.AsyncTestCase);
 
//run the tests
Y.Test.Runner.run();

Before running the tests, it's necessary to create a Y.Console object to display the results (otherwise the tests would run but you wouldn't see the results). After that, the Y.Test.Runner is loaded with the Y.Test.Case object by calling add() (any number of Y.Test.Case 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 Y.Console.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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