diff -r 000000000000 -r 40c8f766c9b8 src/cm/media/js/lib/yui/yui3.0.0/examples/test/test-simple-example.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/cm/media/js/lib/yui/yui3.0.0/examples/test/test-simple-example.html Mon Nov 23 15:14:29 2009 +0100 @@ -0,0 +1,608 @@ + + + + + YUI Library Examples: Test: Simple Testing Example + + + + + + + + + + + + +
+
+
+

+ + YUI 3.x Home - + +

+
+ + +
+ + + +
+
+
+
+

YUI Library Examples: Test: Simple Testing Example

+
+
+ + +
+
+
+
+ +

Test: Simple Testing Example

+ +
+
+

This example shows basic usage of the YUI Test framework for testing browser-based JavaScript code. + Two different TestCase objects are created and added to a + TestSuite object. The TestRunner + is then used to run the tests once the page has loaded.

+ +
+
+ + + + +
+ + + + + +
+
+
+ +

Simple 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 first TestCase

+ +

The first step is to create a new Y.Test.Case object called DataTestCase. + 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.DataTestCase = new Y.Test.Case({
  2.  
  3. //name of the test case - if not provided, one is auto-generated
  4. name : "Data 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. testName : function () {
  33. var Assert = Y.Assert;
  34.  
  35. Assert.isObject(this.data);
  36. Assert.isString(this.data.name);
  37. Assert.areEqual("test", this.data.name);
  38. },
  39.  
  40. testYear : function () {
  41. var Assert = Y.Assert;
  42.  
  43. Assert.isObject(this.data);
  44. Assert.isNumber(this.data.year);
  45. Assert.areEqual(2007, this.data.year);
  46. },
  47.  
  48. testBeta : function () {
  49. var Assert = Y.Assert;
  50.  
  51. Assert.isObject(this.data);
  52. Assert.isBoolean(this.data.beta);
  53. Assert.isTrue(this.data.beta);
  54. }
  55.  
  56. });
Y.example.test.DataTestCase = new Y.Test.Case({
+ 
+    //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 = Y.Assert;
+ 
+        Assert.isObject(this.data);
+        Assert.isString(this.data.name);
+        Assert.areEqual("test", this.data.name);
+    },
+ 
+    testYear : function () {
+        var Assert = Y.Assert;
+ 
+        Assert.isObject(this.data);
+        Assert.isNumber(this.data.year);
+        Assert.areEqual(2007, this.data.year);
+    },
+ 
+    testBeta : function () {
+        var Assert = Y.Assert;
+ 
+        Assert.isObject(this.data);
+        Assert.isBoolean(this.data.beta);
+        Assert.isTrue(this.data.beta);
+    }
+ 
+});

The object literal passed into the constructor contains a number of 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 last section contains the actual tests to be run. Test method names must always begin with the word "test" (all lowercase) in order + to differentiate them from other methods that may be added to the object.

+

The first test in this object is testName(), which runs + various assertions on data.name. Inside of this method, a shortcut to Y.Assert is set up and used to run three + assertions: isObject() on data, isString() on data.name and areEqual() to compare + data.name to the expected value, "test". These assertions are arranged in order from least-specific to most-specific, + 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 + can't possibly pass unless the first passed. Both isObject() and isString() accept a single argument, which is the value + to test (you could optionally include a failure message as a second argument, though this is not required). The areEqual() method + expects two arguments, the first being the expected value ("test") and the second being the actual value (data.name).

+

The second and third tests follow the same pattern as the first with the exception that they work with different data types. The testYear() + method works with data.year, which is a number and so runs tests specifically for numbers (areEqual() can be used with + all data types). The testBeta() method works with data.beta, which is a Boolean, and so it uses the isTrue() + assertion instead of areEqual() (though it could also use areEqual(true, this.data.beta)).

+ +

Creating the second TestCase

+ +

Although it's possible that you'll have only one Y.Test.Case object, typically there is more than one, and so this example includes + a second Y.Test.Case. This one tests some of the built-in functions of the Array object:

+
  1. Y.example.test.ArrayTestCase = new Y.Test.Case({
  2.  
  3. //name of the test case - if not provided, one is auto-generated
  4. name : "Array 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 = [0,1,2,3,4]
  15. },
  16.  
  17. /*
  18.   * Cleans up everything that was created by setUp().
  19.   */
  20. tearDown : function () {
  21. delete this.data;
  22. },
  23.  
  24. //---------------------------------------------------------------------
  25. // Test methods - names must begin with "test"
  26. //---------------------------------------------------------------------
  27.  
  28. testPop : function () {
  29. var Assert = Y.Assert;
  30.  
  31. var value = this.data.pop();
  32.  
  33. Assert.areEqual(4, this.data.length);
  34. Assert.areEqual(4, value);
  35. },
  36.  
  37. testPush : function () {
  38. var Assert = Y.Assert;
  39.  
  40. this.data.push(5);
  41.  
  42. Assert.areEqual(6, this.data.length);
  43. Assert.areEqual(5, this.data[5]);
  44. },
  45.  
  46. testSplice : function () {
  47. var Assert = Y.Assert;
  48.  
  49. this.data.splice(2, 1, 6, 7);
  50.  
  51. Assert.areEqual(6, this.data.length);
  52. Assert.areEqual(6, this.data[2]);
  53. Assert.areEqual(7, this.data[3]);
  54. }
  55.  
  56. });
 Y.example.test.ArrayTestCase = new Y.Test.Case({
+ 
+    //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 = Y.Assert;
+ 
+        var value = this.data.pop();
+ 
+        Assert.areEqual(4, this.data.length);
+        Assert.areEqual(4, value);
+    },
+ 
+    testPush : function () {
+        var Assert = Y.Assert;
+ 
+        this.data.push(5);
+ 
+        Assert.areEqual(6, this.data.length);
+        Assert.areEqual(5, this.data[5]);
+    },
+ 
+    testSplice : function () {
+        var Assert = Y.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]);
+    }
+ 
+});

As with the first Y.Test.Case, this one is split up into three sections: the name, the setUp() and tearDown() + methods, and the test methods. The setUp() method in this Y.Test.Case creates an array of data to be used by the various + tests while the tearDown() method destroys the array. The test methods are very simple, testing the pop(), push(), + and splice() methods. Each test method uses areEqual() exclusively, to show the different ways that it can be used. + The testPop() method calls pop() on the array of values, then verifies that the length of the array has changed and + that the value popped off is 4; the testPush() pushes a new value (5) onto the array and then verifies that the length of the array has + changed and that the value is included in the correct location; the testSplice() method tests splice() by removing one + value that's already in the array and inserting two in its place.

+ +

Creating the TestSuite

+

To better organize the two Y.Test.Case objects, a Y.Test.Suite is created and those two Y.Test.Case objects are + added to it:

+
  1. Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite");
  2. Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase);
  3. Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase);
Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite");
+Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase);
+Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase);

The first line creates a new Y.Test.Suite object using its constructor, which accepts a single argument - the name of the suite. As with + the name of a Y.Test.Case, the Y.Test.Suite name is used to determine where execution is when tests are being executed. Although + not required (one is generated if it's not provided), it is recommended that you select a meaningful name to aid in debugging.

+

Any number of Y.Test.Case and Y.Test.Suite objects can be added to a Y.Test.Suite by using the add() + method. In this example, the two Y.Test.Case objects created earlier are added to the Y.Test.Suite.

+ +

Running the tests

+ +

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 + components have been loaded:

+ +
  1.  
  2. //create the console
  3. var r = new Y.Console({
  4. verbose : true,
  5. newestOnTop : false
  6. });
  7.  
  8. r.render('#testLogger');
  9.  
  10. Y.Test.Runner.add(Y.example.test.ExampleSuite);
  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');
+ 
+Y.Test.Runner.add(Y.example.test.ExampleSuite);
+ 
+//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.Suite object by calling + add() (any number of Y.Test.Case and Y.Test.Suite objects can be added to a Y.Test.Runner, + 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

+
+
+ + + + + +