diff --git a/src/test/docs/component.json b/src/test/docs/component.json new file mode 100644 index 00000000000..8f3d4e53499 --- /dev/null +++ b/src/test/docs/component.json @@ -0,0 +1,46 @@ +{ + "name" : "test", + "displayName": "Test", + "description": "A JavaScript testing framework with a comprehensive assertion syntax. Suitable for testing YUI-based code, but designed to support test-driven development across any JavaScript project, regardless of whether YUI is involved.", + "author" : ["nzakas"], + + "tags": ["utility", "test", "testing", "unit", "tdd"], + "use" : ["test"], + + "examples": [ + { + "name" : "test-simple-example", + "displayName": "Simple Testing Example", + "description": "Demonstrates basic usage of YUI Test for setting up and running tests.", + "modules" : ["test"] + }, + + { + "name": "test-advanced-test-options", + "displayName": "Advanced Test Options", + "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.", + "modules": ["test"] + }, + + { + "name": "test-array-tests", + "displayName": "Array Processing", + "description": "Demonstrates how to use the ArrayAssert object to test array data.", + "modules": ["test"] + }, + + { + "name": "test-async-test", + "displayName": "Asynchronous Testing", + "description": "Demonstrates basic asynchronous tests.", + "modules": ["test"] + }, + + { + "name": "test-async-event-tests", + "displayName": "Asynchronous Event Testing", + "description": "Demonstrates using events with asynchronous tests.", + "modules": ["test"] + } + ] +} diff --git a/src/test/docs/index.mustache b/src/test/docs/index.mustache new file mode 100644 index 00000000000..95665040535 --- /dev/null +++ b/src/test/docs/index.mustache @@ -0,0 +1,1280 @@ +
+

YUI Test is a testing framework for browser-based JavaScript solutions. Using YUI Test, you can easily add unit testing to your JavaScript solutions. While not a direct port from any specific xUnit framework, YUI Test does derive some characteristics from nUnit and JUnit.

+

YUI Test features:

+ +
+ +{{>getting-started}} + +

Using Test Cases

+

The basis of Test is the Y.Test.Case object. A TestCase object is created by using the + Y.Test.Case constructor and passing in an object containing methods and other information with which + to initialize the test case. Typically, the argument is an object literal, for example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //traditional test names + testSomething : function () { + //... + }, + + testSomethingElse : function () { + //... + } +}); +``` +

In this example, a simple test case is created named "TestCase Name". The name property is automatically applied to the test case so that it can be distinguished from other test cases that may be run during the same cycle. The two methods in this example are tests methods ( testSomething() and testSomethingElse()), which means that they are methods designed to test a specific piece of functional code. Test methods are indicatd by their name, either using the traditional manner of prepending the word test to the method name, or using a "friendly name," which is a sentence containing the word "should" that describes the test's purpose. For example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //friendly test names + "Something should happen here" : function () { + //... + }, + + "Something else should happen here" : function () { + //... + } +}); +``` +

Regardless of the naming convention used for test names, each should contain one or more assertions that test data for validity.

+ +

setUp() and tearDown()

+

As each test method is called, it may be necessary to setup information before it's run and then potentially clean up that information + after the test is run. The setUp() method is run before each and every test in the test case and likewise the tearDown() method is run + after each test is run. These methods should be used in conjunction to create objects before a test is run and free up memory after the + test is run. For example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //--------------------------------------------- + // Setup and tear down + //--------------------------------------------- + + setUp : function () { + this.data = { name : "Nicholas", age : 28 }; + }, + + tearDown : function () { + delete this.data; + }, + + //--------------------------------------------- + // Tests + //--------------------------------------------- + + testName: function () { + Y.Assert.areEqual("Nicholas", this.data.name, "Name should be 'Nicholas'"); + }, + + testAge: function () { + Y.Assert.areEqual(28, this.data.age, "Age should be 28"); + } +}); +``` +

In this example, a setUp() method creates a data object with some basic information. Each property of the data object is checked with + a different test, testName() tests the value of data.name while testAge() tests the value of data.age. Afterwards, the data object is deleted + to free up the memory. Real-world implementations will have more complex tests, of course, but they should follow the basic pattern you see in the above code.

+

Note: Both setUp() and tearDown() are optional methods and are only used when defined.

+ +

Ignoring Tests

+

There may be times when you want to ignore a test (perhaps the test is invalid for your purposes or the functionality is being re-engineered and so it shouldn't be tested at this time). To specify tests to ignore, + use the _should.ignore property and name each test to skip as a property whose value is set to true:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //--------------------------------------------- + // Special instructions + //--------------------------------------------- + + _should: { + ignore: { + testName: true //ignore this test + } + }, + + //--------------------------------------------- + // Setup and tear down + //--------------------------------------------- + + setUp : function () { + this.data = { name : "Nicholas", age : 28 }; + }, + + tearDown : function () { + delete this.data; + }, + + //--------------------------------------------- + // Tests + //--------------------------------------------- + + testName: function () { + Y.Assert.areEqual("Nicholas", this.data.name, "Name should be 'Nicholas'"); + }, + + testAge: function () { + Y.Assert.areEqual(28, this.data.age, "Age should be 28"); + } + +}); +``` + +

Here the testName() method will be ignored when the test case is run. This is accomplished by first defining the special _should + property and within it, an ignore property. The ignore property is an object containing name-value pairs representing the names of the tests + to ignore. By defining a property named "testName" and setting its value to true, it says that the method named "testName" + should not be executed.

+ +

Intentional Errors

+

There may be a time that a test throws an error that was expected. For instance, perhaps you're testing a function that should throw an error if invalid data + is passed in. A thrown error in this case can signify that the test has passed. To indicate that + a test should throw an error, use the _should.error property. For example:

+``` +function sortArray(array) { + if (array instanceof Array){ + array.sort(); + } else { + throw new TypeError("Expected an array"); + } +} + +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //--------------------------------------------- + // Special instructions + //--------------------------------------------- + + _should: { + error: { + testSortArray: true //this test should throw an error + } + }, + + //--------------------------------------------- + // Tests + //--------------------------------------------- + + testSortArray: function () { + sortArray(12); //this should throw an error + } + +}); +``` +

In this example, a test case is created to test the standalone sortArray() function, which simply accepts an array and calls its sort() method. + But if the argument is not an array, an error is thrown. When testSortArray() is called, it throws an error because a number is passed into sortArray(). + Since the _should.error object has a property called "testSortArray" set to true, this indicates that testSortArray() should + pass only if an error is thrown.

+

It is possible to be more specific about the error that should be thrown. By setting a property in _should.error to a string, you can + specify that only a specific error message can be construed as a passed test. Here's an example:

+ + +``` +function sortArray(array) { + if (array instanceof Array){ + array.sort(); + } else { + throw new TypeError("Expected an array"); + } +} + +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //--------------------------------------------- + // Special instructions + //--------------------------------------------- + + _should: { + error: { + testSortArray: "Expected an array" + } + }, + + //--------------------------------------------- + // Tests + //--------------------------------------------- + + testSortArray: function () { + sortArray(12); //this should throw an error + } + +}); +``` +

In this example, the testSortArray() test will only pass if the error that is thrown has a message of "Expected an array". + If a different error occurs within the course of executing testSortArray(), then the test will fail due to an unexpected error.

+

If you're unsure of the message but know the type of error that will be thrown, you can specify the error constructor for the error + you're expecting to occur:

+``` +function sortArray(array) { + if (array instanceof Array){ + array.sort(); + } else { + throw new TypeError("Expected an array"); + } +} + +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //--------------------------------------------- + // Special instructions + //--------------------------------------------- + + _should: { + error: { + testSortArray: TypeError + } + }, + + //--------------------------------------------- + // Tests + //--------------------------------------------- + + testSortArray: function () { + sortArray(12); //this should throw an error + } + +}); +``` + +

In this example, the test will pass if a TypeError gets thrown; if any other type of error is thrown, + the test will fail. A word of caution: TypeError is the most frequently thrown error by browsers, + so specifying a TypeError as expected may give false passes.

+

To narrow the margin of error between checking for an error message and checking the error type, you can create a specific error + object and set that in the _should.error property, such as:

+``` +function sortArray(array) { + if (array instanceof Array){ + array.sort(); + } else { + throw new TypeError("Expected an array"); + } +} + +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //--------------------------------------------- + // Special instructions + //--------------------------------------------- + + _should: { + error: { + testSortArray: new TypeError("Expected an array") + } + }, + + //--------------------------------------------- + // Tests + //--------------------------------------------- + + testSortArray: function () { + sortArray(12); //this should throw an error + } + +}); +``` +

Using this code, the testSortArray() method will only pass if a TypeError object is thrown with a message of + "Expected an array"; if any other type of error occurs, then the test fails due to an unexpected error.

+

Note: If a test is marked as expecting an error, the test will fail unless that specific error is thrown. If the test completes without an error being thrown, then it fails.

+ +

Assertions

+

Test methods use assertions to check the validity of a particular action or function. An assertion method tests (asserts) that a condition is valid; if not, it throws an error that causes the test to fail. If all assertions pass within a test method, it is said that the test has passed. The simplest assertion is Y.assert(), which takes two arguments: a condition to test and a message. If the condition is not true, then an assertion error is thrown with the specified message. For example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testUsingAsserts : function () { + Y.assert(value == 5, "The value should be five."); + Y.assert(flag, "Flag should be true."); + } +}); +``` +

In this example, testUsingAsserts() will fail if value is not equal to 5 of flag is not set to true. The Y.assert() method may be all that you need, but there are advanced options available. The Y.Assert object contains several assertion methods that can be used to validate data.

+ +

Equality Assertions

+

The simplest assertions are areEqual() and areNotEqual(). Both methods accept three arguments: the expected value, + the actual value, and an optional failure message (a default one is generated if this argument is omitted). For example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testEqualityAsserts : function () { + + Y.Assert.areEqual(5, 5); //passes + Y.Assert.areEqual(5, "5"); //passes + Y.Assert.areNotEqual(5, 6); //passes + Y.Assert.areEqual(5, 6, "Five was expected."); //fails + } +}); +``` +

These methods use the double equals (==) operator to determine if two values are equal, so type coercion may occur. This means + that the string "5" and the number 5 are considered equal because the double equals sign converts the number to + a string before doing the comparison. If you don't want values to be converted for comparison purposes, use the sameness assertions instead.

+ +

Sameness Assertions

+

The sameness assertions are areSame() and areNotSame(), and these accept the same three arguments as the equality + assertions: the expected value, the actual value, and an optional failure message. Unlike the equality assertions, these methods use + the triple equals operator (===) for comparisions, assuring that no type coercion will occur. For example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testSamenessAsserts : function () { + Y.Assert.areSame(5, 5); //passes + Y.Assert.areSame(5, "5"); //fails + Y.Assert.areNotSame(5, 6); //passes + Y.Assert.areNotSame(5, "5"); //passes + Y.Assert.areSame(5, 6, "Five was expected."); //fails + } +}); +``` + +

Note: Even though this example shows multiple assertions failing, a test will stop as soon as one + assertion fails, causing all others to be skipped.

+ +

Data Type Assertions

+

There may be times when some data should be of a particular type. To aid in this case, there are several methods that test the data type + of variables. Each of these methods acce +

+

These are used as in the following example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testDataTypeAsserts : function () { + Y.Assert.isString("Hello world"); //passes + Y.Assert.isNumber(1); //passes + Y.Assert.isArray([]); //passes + Y.Assert.isObject([]); //passes + Y.Assert.isFunction(function(){}); //passes + Y.Assert.isBoolean(true); //passes + Y.Assert.isObject(function(){}); //passes + + Y.Assert.isNumber("1", "Value should be a number."); //fails + Y.Assert.isString(1, "Value should be a string."); //fails + } +}); +``` +

In addition to these specific data type assertions, there are two generic data type assertions.

+

The isTypeOf() method tests the string returned when the typeof operator is applied to a value. This + method accepts three arguments: the type that the value should be ("string", "number", + "boolean", "undefined", "object", or "function"), the value to test, and an optional failure message. + For example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testTypeOf : function () { + + Y.Assert.isTypeOf("string", "Hello world"); //passes + Y.Assert.isTypeOf("number", 1); //passes + Y.Assert.isTypeOf("boolean", true); //passes + Y.Assert.isTypeOf("number", 1.5); //passes + Y.Assert.isTypeOf("function", function(){}); //passes + Y.Assert.isTypeOf("object", {}); //passes + Y.Assert.isTypeOf("undefined", this.blah); //passes + + Y.Assert.isTypeOf("number", "Hello world", "Value should be a number."); //fails + + } +}); +``` + +

If you need to test object types instead of simple data types, you can also use the isInstanceOf() assertion, which accepts three + arguments: the constructor function to test for, the value to test, and an optional failure message. This assertion uses the instanceof + operator to determine if it should pass or fail. Example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testInstanceOf : function () { + Y.Assert.isInstanceOf(Object, {}); //passes + Y.Assert.isInstanceOf(Array, []); //passes + Y.Assert.isInstanceOf(Object, []); //passes + Y.Assert.isInstanceOf(Function, function(){}); //passes + Y.Assert.isInstanceOf(Object, function(){}); //passes + + Y.Assert.isTypeOf(Array, {}, "Value should be an array."); //fails + + } +}); +``` + +

Special Value Assertions

+

There are numerous special values in JavaScript that may occur in code. These include true, false, NaN, + null, and undefined. There are a number of assertions designed to test for these values specifically:

+ +

Each of these methods accepts two arguments: the value to test and an optional failure message. All of the assertions expect the + exact value (no type coercion occurs), so for example calling isFalse(0) will fail.

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testSpecialValues : function () { + Y.Assert.isFalse(false); //passes + Y.Assert.isTrue(true); //passes + Y.Assert.isNaN(NaN); //passes + Y.Assert.isNaN(5 / "5"); //passes + Y.Assert.isNotNaN(5); //passes + Y.Assert.isNull(null); //passes + Y.Assert.isNotNull(undefined); //passes + Y.Assert.isUndefined(undefined); //passes + Y.Assert.isNotUndefined(null); //passes + + Y.Assert.isUndefined({}, "Value should be undefined."); //fails + + } +}); +``` + +

Forced Failures

+

While most tests fail as a result of an assertion, there may be times when + you want to force a test to fail or create your own assertion method. To do this, use the + fail() method to force a test method to fail immediately:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testForceFail : function () { + Y.Assert.fail(); //causes the test to fail + } +}); +``` +

In this case, the testForceFail() method does nothing but force + the method to fail. Optionally, you can pass in a message to fail() + which will be displayed as the failure message:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + testForceFail : function () { + Y.Assert.fail("I decided this should fail."); + } +}); +``` +

When the failure of this method is reported, the message "I decided this should fail." will be reported.

+

Mock Objects

+

Mock objects are used to eliminate test dependencies on other objects. In complex software systems, there's often multiple + object that have dependence on one another to do their job. Perhaps part of your code relies on the XMLHttpRequest + object to get more information; if you're running the test without a network connection, you can't really be sure if the test + is failing because of your error or because the network connection is down. In reality, you just want to be sure that the correct + data was passed to the open() and send() methods because you can assume that, after that point, + the XMLHttpRequest object works as expected. This is the perfect case for using a mock object.

+

To create a mock object, use the Y.Mock() method to create a new object and then use Y.Mock.expect() + to define expectations for that object. Expectations define which methods you're expecting to call, what the arguments should be, + and what the expected result is. When you believe all of the appropriate methods have been called, you call Y.Mock.verify() + on the mock object to check that everything happened as it should. For example:

+``` +//code being tested +function logToServer(message, xhr){ + xhr.open("get", "/log.php?msg=" + encodeURIComponent(message), true); + xhr.send(null); +} + +//test case for testing the above function +var testCase = new Y.Test.Case({ + + name: "logToServer Tests", + + testPassingDataToXhr : function () { + var mockXhr = Y.Mock(); + + //I expect the open() method to be called with the given arguments + Y.Mock.expect(mockXhr, { + method: "open", + args: ["get", "/log.php?msg=hi", true] + }); + + //I expect the send() method to be called with the given arguments + Y.Mock.expect(mockXhr, { + method: "send", + args: [null] + }); + + //now call the function + logToServer("hi", mockXhr); + + //verify the expectations were met + Y.Mock.verify(mockXhr); + } +}); +``` +

In this code, a mock XMLHttpRequest object is created to aid in testing. The mock object defines two + expectations: that the open() method will be called with a given set of arguments and that the send() + method will be called with a given set of arguments. This is done by using Y.Mock.expect() and passing in the + mock object as well as some information about the expectation. The method property indicates the method name + that will be called and the args property is an array of arguments that should be passed into the method. Each + argument is compared against the actual arguments using the identically equal (===) operator, and if any of the + arguments doesn't match, an assertion failure is thrown when the method is called (it "fails fast" to allow easier debugging).

+

The call to Y.Mock.verify() is the final step in making sure that all expectations have been met. It's at this stage + that the mock object checks to see that all methods have been called. If open() was called but send() + was not, then an assertion failure is thrown and the test fails. It's very important to call Y.Mock.verify() to test + all expectations; failing to do so can lead to false passes when the test should actually fail.

+

In order to use mock objects, your code must be able to swap in and out objects that it uses. For example, a hardcoded + reference to XMLHttpRequest in your code would prevent you from using a mock object in its place. It's sometimes + necessary to refactor code in such a way that referenced objects are passed in rather than hardcoded so that mock objects + can be used.

+

Note that you can use assertions and mock objects together; either will correctly indicate a test failure.

+

Special Argument Values

+

There may be times when you don't necessarily care about a specific argument's value. Since you must always specify the correct + number of arguments being passed in, you still need to indicate that an argument is expected. There are several special values + you can use as placeholders for real values. These values do a minimum amount of data validation:

+ +

Each of these special values can be used in the args property of an expectation, such as:

+``` +Y.Mock.expect(mockXhr, { + method: "open", + args: [Y.Mock.Value.String, "/log.php?msg=hi", Y.Mock.Value.Boolean] +}); +``` +

The expecation here will allow any string value as the first argument and any Boolean value as the last argument. + These special values should be used with care as they can let invalid values through if they are too general. The + Y.Mock.Value.Any special value should be used only if you're absolutely sure that the argument doesn't + matter.

+

Property Expectations

+

Since it's not possible to create property getters and setters in all browsers, creating a true cross-browser property + expectation isn't feasible. YUI Test mock objects allow you to specify a property name and it's expected value when + Y.Mock.verify() is called. This isn't a true property expectation but rather an expectation that the property + will have a certain value at the end of the test. You can specify a property expectation like this:

+``` +//expect that the status property will be set to 404 +Y.Mock.expect(mockXhr, { + property: "status", + value: 404 +}); +``` +

This example indicates that the status property of the mock object should be set to 404 before + the test is completed. When Y.Mock.verify() is called on mockXhr, it will check + the property and throw an assertion failure if it has not been set appropriately.

+

Asynchronous Tests

+

YUI Test allows you to pause a currently running test and resume either after a set amount of time or + at another designated time. The TestCase object has a method called wait(). When wait() + is called, the test immediately exits (meaning that any code after that point will be ignored) and waits for a signal to resume + the test.

+

A test may be resumed after a certain amount of time by passing in two arguments to wait(): a function to execute + and the number of milliseconds to wait before executing the function (similar to using setTimeout()). The function + passed in as the first argument will be executed as part of the current test (in the same scope) after the specified amount of time. + For example:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //--------------------------------------------- + // Setup and tear down + //--------------------------------------------- + + setUp : function () { + this.data = { name : "Nicholas", age : 29 }; + }, + + tearDown : function () { + delete this.data; + }, + + //--------------------------------------------- + // Tests + //--------------------------------------------- + + testAsync: function () { + Y.Assert.areEqual("Nicholas", this.data.name, "Name should be 'Nicholas'"); + + //wait 1000 milliseconds and then run this function + this.wait(function(){ + Y.Assert.areEqual(29, this.data.age, "Age should be 29"); + + }, 1000); + } +}); +``` + +

In this code, the testAsync() function does one assertion, then waits 1000 milliseconds before performing + another assertion. The function passed into wait() is still in the scope of the original test, so it has + access to this.data just as the original part of the test does. Timed waits are helpful in situations when + there are no events to indicate when the test should resume.

+

If you want a test to wait until a specific event occurs before resuming, the wait() method can be called + with a timeout argument (the number of milliseconds to wait before considering the test a failure). At that point, testing will resume only when the resume() method is called. The + resume() method accepts a single argument, which is a function to run when the test resumes. This function + should specify additional assertions. If resume() isn't called before the timeout expires, then the test fails. The following tests to see if the Anim object has performed its + animation completely:

+``` +var testCase = new Y.Test.Case({ + + name: "TestCase Name", + + //--------------------------------------------- + // Tests + //--------------------------------------------- + + testAnimation : function (){ + + //animate width to 400px + var myAnim = new Y.Anim({ + node: '#testDiv', + to: { + width: 400 + }, + duration: 3 + }); + + var test = this; + + //assign oncomplete handler + myAnim.on("end", function(){ + + //tell the TestRunner to resume + test.resume(function(){ + + Y.Assert.areEqual(myAnim.get("node").get("offsetWidth"), 400, "Width of the DIV should be 400."); + + }); + + }); + + //start the animation + myAnim.run(); + + //wait until something happens + this.wait(3100); + + } +}); +``` + +

In this example, an Anim object is used to animate the width of an element to 400 pixels. When the animation + is complete, the end event is fired, so that is where the resume() method is called. The + function passed into resume() simply tests that the final width of the element is indeed 400 pixels. Once the event handler is set up, the animation begins. + In order to allow enough time for the animation to complete, the wait() method is called + with a timeout of 3.1 seconds (just longer than the 3 seconds needed to complete the animation). At that point, testing stops until the animation completes and resume() is called or until 3100 milliseconds have passed.

+

Test Suites

+

For large web applications, you'll probably have many test cases that should be run during a testing phase. A test suite helps to handle multiple test cases + by grouping them together into functional units that can be run together. To create new test suite, use the Y.Test.Suite + constructor and pass in the name of the test suite. The name you pass in is for logging purposes and allows you to discern which TestSuite instance currently running. For example:

+``` +//create the test suite +var suite = new Y.Test.Suite("TestSuite Name"); + +//add test cases +suite.add(new Y.Test.Case({ + //... +})); +suite.add(new Y.Test.Case({ + //... +})); +suite.add(new Y.Test.Case({ + //... +})); +``` +

Here, a test suite is created and three test cases are added to it using the add() method. The test suite now contains all of the + information to run a series of tests.

+

It's also possible to add other multiple TestSuite instances together under a parent TestSuite using the same add() method:

+``` +//create a test suite +var suite = new Y.Test.Suite("TestSuite Name"); + +//add a test case +suite.add(new Y.Test.Case({ + //... +}); + +//create another suite +var anotherSuite = new Y.Test.Suite("test_suite_name"); + +//add a test case +anotherSuite.add(new Y.Test.Case({ + //... +}); + +//add the second suite to the first +suite.add(anotherSuite); +``` +

By grouping test suites together under a parent test suite you can more effectively manage testing of particular aspects of an application.

+

Test suites may also have setUp() and tearDown() methods. A test suite's setUp() method is called before + the first test in the first test case is executed (prior to the test case's setUp() method); a test suite's tearDown() + method executes after all tests in all test cases/suites have been executed (after the last test case's tearDown() method). To specify + these methods, pass an object literal into the Y.Test.Suite constructor:

+``` +//create a test suite +var suite = new Y.Test.Suite({ + name : "TestSuite Name", + + setUp : function () { + //test-suite-level setup + }, + + tearDown: function () { + //test-suite-level teardown + } +}); +``` +

Test suite setUp() and tearDown() may be helpful in setting up global objects that are necessary for a multitude of tests + and test cases.

+

Running Tests

+

In order to run test cases and test suites, use the Y.Test.Runner object. This object is a singleton that + simply runs all of the tests in test cases and suites, reporting back on passes and failures. To determine which test cases/suites + will be run, add them to the Y.Test.Runner using the add() method. Then, to run the tests, call the run() + method:

+``` +//add the test cases and suites +Y.Test.Runner.add(testCase); +Y.Test.Runner.add(oTestSuite); + +//run all tests +Y.Test.Runner.run(); +``` + +

If at some point you decide not to run the tests that have already been added to the TestRunner, they can be removed by calling clear():

+``` +Y.Test.Runner.clear(); +``` + +

Making this call removes all test cases and test suites that were added using the add() method.

+ +

TestRunner Events

+

The Y.Test.Runner provides results and information about the process by publishing several events. These events can occur at four + different points of interest: at the test level, at the test case level, at the test suite level, and at the Y.Test.Runner level. + The data available for each event depends completely on the type of event and the level at which the event occurs.

+ +

Test-Level Events

+

Test-level events occur during the execution of specific test methods. There are three test-level events:

+ +

For each of these events, the event data object has three properties:

+ +

For Y.Test.Runner.TEST_FAIL_EVENT, an error property containing the error object + that caused the test to fail.

+ +

TestCase-Level Events

+

There are two events that occur at the test case level:

+ +

For these two events, the event data object has three properties:

+ +

For TEST_CASE_COMPLETE_EVENT, an additional property called results is included. The results + property is an object containing the aggregated results for all tests in the test case (it does not include information about tests that + were ignored). Each test that was run has an entry in the result object where the property name is the name of the test method + and the value is an object with two properties: result, which is either "pass" or "fail", and message, which is a + text description of the result (simply "Test passed" when a test passed or the error message when a test fails). Additionally, the + failed property indicates the number of tests that failed in the test case, the passed property indicates the + number of tests that passed, and the total property indicates the total number of tests executed. A typical results + object looks like this:

+``` +{ + failed: 1, + passed: 1, + ignored: 0, + total: 2, + type: "testcase", + name: "Test Case 0", + + test0: { + result: "pass", + message: "Test passed", + type: "test", + name: "test0" + }, + + test1: { + result: "fail", + message: "Assertion failed", + type: "test", + name: "test1" + } +} +``` + +

The TEST_CASE_COMPLETE_EVENT provides this information for transparency into the testing process.

+ +

TestSuite-Level Events

+

There are two events that occur at the test suite level:

+ +

For these two events, the event data object has three properties:

+ +

The TEST_SUITE_COMPLETE_EVENT also has a results property, which contains aggregated results for all of the + test cases (and other test suites) it contains. Each test case and test suite contained within the main suite has an entry in the + results object, forming a hierarchical structure of data. A typical results object may look like this:

+``` +{ + failed: 2, + passed: 2, + ignored: 0, + total: 4, + type: "testsuite", + name: "Test Suite 0", + + testCase0: { + failed: 1, + passed: 1, + ignored: 0, + total: 2, + type: "testcase", + name: "testCase0", + + test0: { + result: "pass", + message: "Test passed." + type: "test", + name: "test0" + }, + test1: { + result: "fail", + message: "Assertion failed.", + type: "test", + name: "test1" + } + }, + testCase1: { + failed: 1, + passed: 1, + ignored: 0, + total: 2, + type: "testcase", + name: "testCase1", + + test0: { + result: "pass", + message: "Test passed.", + type: "test", + name: "test0" + }, + test1: { + result: "fail", + message: "Assertion failed.", + type: "test", + name: "test1" + } + } +} +``` + + +

This example shows the results for a test suite with two test cases, but there may be test suites contained within test suites. In that case, + the hierarchy is built out accordingly, for example:

+``` +{ + failed: 3, + passed: 3, + ignored: 0, + total: 6, + type: "testsuite", + name: "Test Suite 0", + + testCase0: { + failed: 1, + passed: 1, + ignored: 0, + total: 2, + type: "testcase", + name: "testCase0", + + test0: { + result: "pass", + message: "Test passed.", + type: "test", + name: "test0" + }, + test1: { + result: "fail", + message: "Assertion failed.", + type: "test", + name: "test1" + } + }, + + testCase1: { + failed: 1, + passed: 1, + ignored: 0, + total: 2, + type: "testcase", + name: "testCase1", + + test0: { + result: "pass", + message: "Test passed.", + type: "test", + name: "test0" + }, + test1: { + result: "fail", + message: "Assertion failed.", + type: "test", + name: "test1" + } + }, + + testSuite0:{ + failed: 1, + passed: 1, + ignored: 0, + total: 2, + type: "testsuite", + name: "testSuite0", + + testCase2: { + failed: 1, + passed: 1, + ignored: 0, + total: 2, + type: "testcase", + name: "testCase2", + + test0: { + result: "pass", + message: "Test passed.", + type: "test", + name: "test0" + }, + + test1: { + result: "fail", + message: "Assertion failed.", + type: "test", + name: "test1" + } + } + } +} +``` + +

In this code, the test suite contained another test suite named "testSuite0", which is included in the results along + with its test cases. At each level, the results are aggregated so that you can tell how many tests passed or failed within each + test case or test suite.

+ +

TestRunner-Level Events

+

There are two events that occur at the Y.Test.Runner level:

+ +

The data object for these events contain a type property, indicating the type of event that occurred. COMPLETE_EVENT + also includes a results property that is formatted the same as the data returned from TEST_SUITE_COMPLETE_EVENT and + contains rollup information for all test cases and tests suites that were added to the TestRunner.

+ +

Subscribing to Events

+

You can subscribe to particular events by calling the subscribe() method. Your event handler code + should expect a single object to be passed in as an argument. This object provides information about the event that just occured. Minimally, + the object has a type property that tells you which type of event occurred. Example:

+``` +function handleTestFail(data){ + alert("Test named '" + data.testName + "' failed with message: '" + data.error.message + "'."); +} + +var TestRunner = Y.Test.Runner; +TestRunner.subscribe(TestRunner.TEST_FAIL_EVENT, handleTestFail); +TestRunner.run(); +``` + + +

In this code, the handleTestFail() function is assigned as an event handler for TEST_FAIL_EVENT. You can also + use a single event handler to subscribe to any number of events, using the event data object's type property to determine + what to do:

+``` +function handleTestResult(data){ + var TestRunner = Y.Test.Runner; + + switch(data.type) { + case TestRunner.TEST_FAIL_EVENT: + alert("Test named '" + data.testName + "' failed with message: '" + data.error.message + "'."); + break; + case TestRunner.TEST_PASS_EVENT: + alert("Test named '" + data.testName + "' passed."); + break; + case TestRunner.TEST_IGNORE_EVENT: + alert("Test named '" + data.testName + "' was ignored."); + break; + } + +} + +TestRunner.subscribe(TestRunner.TEST_FAIL_EVENT, handleTestResult); +TestRunner.subscribe(TestRunner.TEST_IGNORE_EVENT, handleTestResult); +TestRunner.subscribe(TestRunner.TEST_PASS_EVENT, handleTestResult); +TestRunner.run(); +``` + + +

Viewing Results

+

There are two ways to view test results. The first is to output test results to the Console + component. To do so, you need only create a new Console instance; the result results will be posted + to the logger automatically:

+``` +YUI({ logInclude: { TestRunner: true } }).use("test", function(Y){ + + //tests go here + + //initialize the console + var yconsole = new Y.Console({ + newestOnTop: false + }); + yconsole.render('#log'); + + //run the tests + Y.Test.Runner.run(); +}); +``` + +

If you are using a browser that supports the console + object (Firefox with Firebug installed, Safari 3+, Internet Explorer 8+, Chrome), then you can + direct the test results onto the console. To do so, make sure that you've specified your YUI + instance to use the console when logging:

+``` +YUI({ useBrowserConsole: true }).use("test", function(Y){ + + //tests go here + + Y.Test.Runner.run(); + +}); +``` +

You can also extract the test result data using the Y.Test.Runner.getResults() method. By default, this method + returns an object representing the results of the tests that were just run (the method returns null if called + while tests are still running). You can optionally specify a format in which the results should be returned. There are four + possible formats:

+ +

You can pass any of these into Y.Test.Runner.getResults() to get a string with the test result information properly + formatted. For example:

+``` +YUI({ useBrowserConsole: true }).use("test", function(Y){ + + //tests go here + + //get object of results + var resultsObject = Y.Test.Runner.getResults(); + + //get XML results + var resultsXML = Y.Test.Runner.getResults(Y.Test.Format.XML); + +}); +``` +

The XML format outputs results in the following + format:

+``` + + + + + + + + + + + + +``` +

The JSON format requires the JSON utility to be loaded on the page and outputs results in a format that follows the object/array hierarchy of the results object, such as:

+``` +{ + "passed": 5, + "failed": 0, + "ignored": 0, + "total": 0, + "type": "report", + "name": "YUI Test Results", + + "yuisuite":{ + "passed": 5, + "failed": 0, + "ignored": 0, + "total": 0, + "type": "testsuite", + "name": "yuisuite", + + "Y.Anim":{ + "passed": 5, + "failed": 0, + "ignored": 0, + "total": 0, + "type":"testcase", + "name":"Y.Anim", + + "test_getEl":{ + "result":"pass", + "message":"Test passed.", + "type":"test", + "name":"test_getEl" + }, + "test_isAnimated":{ + "result":"pass", + "message":"Test passed.", + "type":"test", + "name":"test_isAnimated" + }, + "test_stop":{ + "result":"pass", + "message":"Test passed.", + "type":"test", + "name":"test_stop" + }, + "test_onStart":{ + "result":"pass", + "message":"Test passed.", + "type":"test", + "name":"test_onStart" + }, + "test_endValue":{ + "result":"pass", + "message":"Test passed.", + "type":"test", + "name":"test_endValue" + } + } + } +} +``` + +

The JUnit XML format outputs results in the following format:

+ +``` + + + + + + + + + + +``` + +

Note that there isn't a direct mapping between YUI Test test suites and JUnit test suites, so some +of the hierarchical information is lost.

+ +

The TAP format outputs results in the following format:

+ +```nohighlight +1..5 +#Begin report YUI Test Results (0 failed of 5) +#Begin testcase Y.Anim (0 failed of 5) +ok 1 - testGetServiceFromUntrustedModule +ok 2 - testGetServiceFromTrustedModule +ok 3 - testGetServiceFromService +ok 4 - testGetServiceMultipleTimesFromService +ok 5 - testGetServiceMultipleTimesFromUntrustedModule +#End testcase Y.Anim +#End report YUI Test Results +``` + +

The XML, JSON, and JUnit XML formats produce a string with no extra white space (white space and indentation shown here is for readability + purposes only).

+ +

Test Reporting

+

When all tests have been completed and the results object has been returned, you can post those results to a server + using a Y.Test.Reporter object. A Y.Test.Reporter object creates a form that is POSTed + to a specific URL with the following fields:

+ +

You can create a new Y.Test.Reporter object by passing in the URL to report to. The results object can + then be passed into the report() method to submit the results:

+``` +var reporter = new Y.Test.Reporter("http://www.yourserver.com/path/to/target"); +reporter.report(results); +``` +

The form submission happens behind-the-scenes and will not cause your page to navigate away. This operation is + one direction; the reporter does not get any content back from the server.

+

There are four predefined serialization formats for results objects:

+ + +

The format in which to submit the results can be specified in the Y.Test.Reporter constructor by passing in the appropriate + Y.Test.Format value (when no argument is specified, Y.Test.Format.XML is used:

+``` +var reporter = new Y.Test.Reporter("http://www.yourserver.com/path/to/target", Y.Test.Format.JSON); +``` + +

Custom Fields

+

You can optionally specify additional fields to be sent with the results report by using the addField() method. + This method accepts two arguments: a name and a value. Any field added using addField() is POSTed along with + the default fields back to the server:

+``` +reporter.addField("color", "blue"); +reporter.addField("message", "Hello world!"); +``` +

Note that if you specify a field name that is the same as a default field, the custom field is ignored in favor of + the default field.

diff --git a/src/test/docs/partials/test-advanced-test-options-source.mustache b/src/test/docs/partials/test-advanced-test-options-source.mustache new file mode 100644 index 00000000000..1c1ad590528 --- /dev/null +++ b/src/test/docs/partials/test-advanced-test-options-source.mustache @@ -0,0 +1,136 @@ +
+ + diff --git a/src/test/docs/partials/test-array-tests-source.mustache b/src/test/docs/partials/test-array-tests-source.mustache new file mode 100644 index 00000000000..26cd48de4a9 --- /dev/null +++ b/src/test/docs/partials/test-array-tests-source.mustache @@ -0,0 +1,180 @@ +
+ + diff --git a/src/test/docs/partials/test-async-event-tests-source.mustache b/src/test/docs/partials/test-async-event-tests-source.mustache new file mode 100644 index 00000000000..27bd9c4b46c --- /dev/null +++ b/src/test/docs/partials/test-async-event-tests-source.mustache @@ -0,0 +1,66 @@ +
+
+ + diff --git a/src/test/docs/partials/test-async-test-source.mustache b/src/test/docs/partials/test-async-test-source.mustache new file mode 100644 index 00000000000..3b1bcaaa303 --- /dev/null +++ b/src/test/docs/partials/test-async-test-source.mustache @@ -0,0 +1,71 @@ +
+ + diff --git a/src/test/docs/partials/test-simple-example-source.mustache b/src/test/docs/partials/test-simple-example-source.mustache new file mode 100644 index 00000000000..bb37cc54159 --- /dev/null +++ b/src/test/docs/partials/test-simple-example-source.mustache @@ -0,0 +1,140 @@ +
+ + diff --git a/src/test/docs/test-advanced-test-options.mustache b/src/test/docs/test-advanced-test-options.mustache new file mode 100644 index 00000000000..71a9fadeb16 --- /dev/null +++ b/src/test/docs/test-advanced-test-options.mustache @@ -0,0 +1,302 @@ +
+

This example shows how to use advanced test options, which allow you to specify additional information about how a test should be run. + Each TestCase can specify up to three different options for tests, + including tests that should be ignored, tests that should throw an error, and tests that should fail.

+
+ +
+ + + {{>test-advanced-test-options-source}} +
+ +

Advanced Test Options

+ +

This example begins by creating a namespace and a Y.Test.Case object:

+``` +Y.namespace("example.test"); +Y.example.test.AdvancedOptionsTestCase = new Y.TestCase({ + name: "Advanced Options Tests" +}); +``` + +

This Y.Test.Case serves as the basis for this example.

+ +

Using _should

+ +

Immediately after the name of the Y.Test.Case is defined, there is a _should property. + This property specifies information about how tests should behave and is defined as an object literal with one + or more of the following properties: fail, error, and ignore.Each of these three + is also defined as an object literal whose property names map directly to the names of test methods in the Y.Test.Case. + This example uses all three properties:

+``` +Y.example.test.AdvancedOptionsTestCase = new Y.TestCase({ + + //the name of the test case - if not provided, one is automatically generated + name: "Advanced Options Tests", + + /* + * Specifies tests that "should" be doing something other than the expected. + */ + _should: { + + /* + * Tests listed in here should fail, meaning that if they fail, the test + * has passed. This is used mostly for YuiTest to test itself, but may + * be helpful in other cases. + */ + fail: { + + //the test named "testFail" should fail + testFail: true + + }, + + /* + * Tests listed here should throw an error of some sort. If they throw an + * error, then they are considered to have passed. + */ + error: { + + /* + * You can specify "true" for each test, in which case any error will + * cause the test to pass. + */ + testGenericError: true, + + /* + * You can specify an error message, in which case the test passes only + * if the error thrown matches the given message. + */ + testStringError: "I'm a specific error message.", + testStringError2: "I'm a specific error message.", + + /* + * You can also specify an error object, in which case the test passes only + * if the error thrown is on the same type and has the same message. + */ + testObjectError: new TypeError("Number expected."), + testObjectError2: new TypeError("Number expected."), + testObjectError3: new TypeError("Number expected.") + + }, + + /* + * Tests listed here should be ignored when the test case is run. For these tests, + * setUp() and tearDown() are not called. + */ + ignore : { + + testIgnore: true + + } + }, + + ... +}); +``` + +

This Y.Test.Case specifies one test that should fail, six that should throw an error, and one that should be ignored.

+

In the fail section, the test method testFail() is specified as one that should fail. By adding the + property testFail and settings its value to true, the Y.Test.Runner knows that this test is expected to fail. + If the test were to be run without failing, it would be considered a failure of the test. This feature is useful when testing + YUI Test itself or addon components to YUI Test.

+

Moving on to the error section, there are six tests specified that should throw an error. There are three different ways + to indicate that a test is expected to throw an error. The first is simply to add a property with the same name as the test method + and set its value equal to true (similar to specifying tests that should fail). In this example, the testGenericError() + method is specified this way. When specified like this, the test passes regardless of the type of error that occurs. This can be + dangerous since unexpected errors will also cause the test to pass. To be more specific, set the property value for the test method + to an error message string. When a string is used instead of the Boolean true, the test passes only when an error is thrown and that + error message matches the string. In this example, testStringError() and testStringError2() expect an error + to be thrown with an error message of "I'm a specific error message." If any other error occurs inside of the these methods, + the test will fail because the error message doesn't match. The last way to specify an error should occur is to create an actual error + object, which is the case with testObjectError(), testObjectError2(), and testObjectError3(). + When specified in this way, a test will pass only when an error is thrown whose constructor and error message match that of the + error object.

+

The last section is ignore, which determines tests that should be ignored. In this example, the method testIgnore() + is set to be ignored when the Y.Test.Case is executed. Test in the ignore section are specified the same way + as those in the fail section, by adding the name as a property and setting its value to true.

+ +

Creating the test methods

+ +

The next part of the example contains the actual test methods. Since each test method is specified as having a certain behavior in + _should, they each do something to show their particular functionality.

+

The first method is testFail(), which does nothing but purposely fail. Since this method is specified as one that should + fail, it means that this test will pass:

+``` +Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ + + //the name of the test case - if not provided, one is automatically generated + name: "Advanced Options Tests", + + ... + + testFail : function() { + + //force a failure - but since this test "should" fail, it will pass + Y.Assert.fail("Something bad happened."); + + }, + + ... +}); +``` +

This method uses Assert.fail() to force the test to fail. This type of method is helpful if you are creating your own + type of assert methods that should fail when certain data is passed in.

+

Next, the test methods that should error are defined. The testGenericError() method is specified as needing to throw + an error to pass. In the error section, testGenericError is set to true, meaning that any error causes + this method to pass. To illustrate this, the method simply throws an error:

+``` +Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ + + //the name of the test case - if not provided, one is automatically generated + name: "Advanced Options Tests", + + ... + + testGenericError : function() { + throw new Error("Generic error"); + }, + + ... +}); +``` +

The fact that this method throws an error is enough to cause it to pass (the type of error and error message don't matter). The next + two methods, testStringError() and testStringError2() are specified as throwing an error with a specific + message ("I'm a specific error message."):

+``` +Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ + + //the name of the test case - if not provided, one is automatically generated + name: "Advanced Options Tests", + + ... + + testStringError : function() { + + //throw a specific error message - this will pass because it "should" happen + throw new Error("I'm a specific error message."); + }, + + testStringError2 : function() { + + //throw a specific error message - this will fail because the message isn't expected + throw new Error("I'm a specific error message, but a wrong one."); + }, + + ... +}); +``` +

The testStringError() method will pass when executed because the error message matches up exactly with the one + specified in the error section. The testStringError2() method, however, will fail because its + error message is different from the one specified.

+

To be more specific, testObjectError(), testObjectError2(), and testObjectError3(), + specified an error type (TypeError) and an error messsage ("Number expected."):

+``` +Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ + + //the name of the test case - if not provided, one is automatically generated + name: "Advanced Options Tests", + + ... + + testObjectError : function() { + + //throw a specific error and message - this will pass because it "should" happen + throw new TypeError("Number expected."); + }, + + testObjectError2 : function() { + + //throw a specific error and message - this will fail because the type doesn't match + throw new Error("Number expected."); + }, + + testObjectError3 : function() { + + //throw a specific error and message - this will fail because the message doesn't match + throw new TypeError("String expected."); + }, + + ... +}); +``` +

Of the these three methods, only testObjectError() will pass because it's the only one that throws a TypeError + object with the message, "Number expected." The testObjectError2() method will fail because the type of error + being thrown (Error) is different from the expected type (TypeError), as specified in the error + section. The last method, testObjectError3(), also fails. Though it throws the right type of error, the error message + doesn't match the one that was specified.

+

The last method in the Y.Test.Case is testIgnore(), which is specified to be ignored. To be certain, this + method pops up a message:

+``` +Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ + + //the name of the test case - if not provided, one is automatically generated + name: "Advanced Options Tests", + + ... + + testIgnore : function () { + alert("You'll never see this."); + } +}); +``` +

If this test weren't ignored, then the alert should be displayed. Since it is ignored, though, you will never see the alert. Additionally, + there is a special message displayed in the Y.Console when a test is ignored.

+ +

Running the tests

+ +

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

+ +``` +//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.AdvancedOptionsTestCase); + +//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.

+ +

Complete Example Source

+ +``` +{{>test-advanced-test-options-source}} +``` diff --git a/src/test/docs/test-array-tests.mustache b/src/test/docs/test-array-tests.mustache new file mode 100644 index 00000000000..33675f86509 --- /dev/null +++ b/src/test/docs/test-array-tests.mustache @@ -0,0 +1,331 @@ +
+

This example shows how to use the ArrayAssert object, which + contains assertions designed to be used specifically with JavaScript Arrays and array-like objects.

+
+ +
+ + + {{>test-array-tests-source}} +
+ +

Array Assertions

+ +

This example uses the Y.ArrayAssert object to test methods on JavaScript's + built-in Array object. The intent of this example is to introduce Y.ArrayAssert and its methods + as an alternative to the generic methods available on Y.Assert.

+

The example begins by creating an example namespace and Y.Test.Case:

+``` +Y.namespace("example.test"); +Y.example.test.ArrayTestCase = new Y.Test.Case({ + + name: "Array Tests", + + //------------------------------------------------------------------------- + // Setup and teardown + //------------------------------------------------------------------------- + + /* + * The setUp() method is used to setup data that necessary for a test to + * run. This method is called immediately before each test method is run, + * so it is run as many times as there are test methods. + */ + setUp : function () { + this.data = new Array (0,1,2,3,4,5); + }, + + /* + * The tearDown() method is used to clean up the initialization that was done + * in the setUp() method. Ideally, it should free up all memory allocated in + * setUp(), anticipating any possible changes to the data. This method is called + * immediately after each test method is run. + */ + tearDown : function () { + delete this.data; + }, + + ... +}); +``` +

This TestCase has a setUp() method that creates an array for all the tests to use, as well as + a tearDown() method that deletes the array after each test has been executed. This array is used throughout + the tests as a base for array manipulations.

+ +

Testing push()

+

The first test is testPush(), which tests the functionality of the Array object's push() method + (other methods hidden for simpicity):

+ +``` +Y.example.test.ArrayTestCase = new Y.Test.Case({ + + ... + + testPush : function() { + + //shortcut variables + var ArrayAssert = Y.ArrayAssert; + + //do whatever data manipulation is necessary + this.data.push(6); + + //array-specific assertions + ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); + ArrayAssert.contains(6, this.data, "Array should contain 6."); + ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6."); + + //check that all the values are there + ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal."); + + }, + + ... +}); +``` +

The test begins by setting up a shortcut variables for Y.ArrayAssert, then pushes the value 6 onto + the data array (which was created by setUp()). Next, Y.ArrayAssert.isNotEmpty() determines if the + array has at least one item; this should definitely pass because the push() operation only adds values to the array. To determine + that the new value, 6, is in the array, Y.ArrayAssert.contains() is used. The first argument is the value to look for and the second + is the array to look in. To find out if the new value ended up where it should have (the last position, index 6), Y.ArrayAssert.indexOf() + is used, passing in the value to search for as the first argument, the array to search in as the second, and the index at which the value should + occur as the final argument. Since 6 was pushed onto the end of an array that already had 6 items, it should end up at index 6 (the length of the + array minus one). As a final test, Y.ArrayAssert.itemsAreEqual() is used to determine that all of the items in the array are in the + correct place. The first argument of this method is an array that has all of the values that should be in the array you're testing. This assertion + passes only when the values in both arrays match up (the values are equal and the positions are the same).

+ +

Testing pop()

+

The next test is testPop(), which tests the functionality of the Array object's pop() method:

+ +``` +Y.example.test.ArrayTestCase = new Y.Test.Case({ + + ... + + testPop : function() { + + //shortcut variables + var Assert = Y.Assert; + var ArrayAssert = Y.ArrayAssert; + + //do whatever data manipulation is necessary + var value = this.data.pop(); + + //array shouldn't be empty + ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); + + //basic equality assertion - expected value, actual value, optional error message + Assert.areEqual(5, this.data.length, "Array should have 5 items."); + Assert.areEqual(5, value, "Value should be 5."); + + ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal."); + + }, + + ... +}); +``` +

This test also starts out by creating some shortcut variables, for Y.Assert and Y.ArrayAssert. Next, the pop() + method is called, storing the returned item in value. Since pop() should only remove a single item, Y.ArrayAssert.isNotEmpty() + is called to ensure that only one item has been removed. After that, Y.Assert.areEqual() is called twice: once to check the + length of the array and once to confirm the value of the item that was removed from the array (which should be 5). The last assertion uses + Y.ArrayAssert.itemsAreSame(), which is similar to Y.ArrayAssert.itemsAreEqual() in that it compares values between two + arrays. The difference is that Y.ArrayAssert.itemsAreSame() uses strict equality (===) to compare values, ensuring that + no behind-the-scenes type conversions will occur (this makes Y.ArrayAssert.itemsAreSame() more useful for working with arrays of + objects).

+ +

Testing reverse()

+

The next test is testReverse(), which tests the functionality of the Array object's reverse() method:

+ +``` +Y.example.test.ArrayTestCase = new Y.Test.Case({ + + ... + + testReverse : function() { + + //shortcut variables + var ArrayAssert = Y.ArrayAssert; + + //do whatever data manipulation is necessary + this.data = this.data.reverse(); + + ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal."); + + }, + + ... +}); +``` +

The testRemove() method is very simple, calling reverse() on the array and then testing the result. Since + every item in the array has changed, the changes can be tested by calling Y.ArrayAssert.itemsAreEqual() once (instead of + calling Y.ArrayAssert.indexOf() multiple times). The first argument is an array with all the values in the reverse order + of the array that was created in setUp(). When compared with the second argument, the newly reversed array, the values in + each position should be equal.

+ +

Testing shift()

+

The next test is testShift(), which tests the functionality of the Array object's shift() method:

+ +``` +Y.example.test.ArrayTestCase = new Y.Test.Case({ + + ... + + testShift : function() { + + //shortcut variables + var Assert = Y.Assert; + var ArrayAssert = Y.ArrayAssert; + + //do whatever data manipulation is necessary + var value = this.data.shift(); + + //array shouldn't be empty + ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); + + //basic equality assertion - expected value, actual value, optional error message + Assert.areEqual(5, this.data.length, "Array should have 6 items."); + Assert.areEqual(0, value, "Value should be 0."); + + ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal."); + + }, + + ... +}); +``` +

The shift() method removes the first item in the array and returns it (similar to pop(), which removes the item + from the end). In the testShift() method, shift() is called and the item is stored in value. To ensure + that the rest of the array is still there, Y.ArrayAssert.isNotEmpty() is called. After that, Array.areEqual() is + called twice, once to test the length of the array and once to test the value that was returned from shift() (which should be + 0). As a last test, the entire array is tested using Y.ArrayAssert.itemsAreEqual() to ensure that all of the items have shifted + into the appropriate positions in the array.

+ +

Testing splice()

+

The next test is testSplice(), which tests the functionality of the Array object's splice() method:

+ +``` +Y.example.test.ArrayTestCase = new Y.Test.Case({ + + ... + + testSplice : function() { + + //shortcut variables + var Assert = Y.Assert; + var ArrayAssert = Y.ArrayAssert; + + //do whatever data manipulation is necessary + var removed = this.data.splice(1, 2, 99, 100); + + //basic equality assertion - expected value, actual value, optional error message + Assert.areEqual(6, this.data.length, "Array should have 6 items."); + + //the new items should be there + ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99."); + ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100."); + + ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal."); + ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2."); + + }, + + ... +}); +``` +

The splice() method is one of the most powerful Array manipulations. It can both remove and add any number of items + from an array at the same time. This test begins by splicing some values into the array. When calling splice(), the first argument + is 1, indicating that values should be inserted at index 1 of the array; the second argument is 2, indicating that two values should be + removed from the array (the value in index 1 and the value in index 2); the third and fourth arguments are values that should be inserted + into the array at the position given by the first argument. Essentially, values 1 and 2 should end up being replaced by values 99 and 100 in + the array.

+

The first test is to determine that the length of the array is still 6 (since the previous step removed two items and then inserted two, the + length should still be 6). After that, Y.Assert.indexOf() is called to determine that the values of 99 and 100 are in positions + 1 and 2, respectively. To ensure the integrity of the entire array, Y.ArrayAssert.itemsAreEqual() is called on the array, comparing + it to an array with the same values. The very last step is to test the value returned from splice(), which is an array containing + the removed values, 1 and 2. Y.ArrayAssert.itemsAreEqual() is appropriate for this task as well.

+ +

Testing unshift()

+

The next test is testUnshift(), which tests the functionality of the Array object's unshift() method:

+ +``` +Y.example.test.ArrayTestCase = new Y.Test.Case({ + + ... + + testUnshift : function() { + + //shortcut variables + var Assert = Y.Assert; + var ArrayAssert = Y.ArrayAssert; + + //do whatever data manipulation is necessary + this.data.unshift(-1); + + //basic equality assertion - expected value, actual value, optional error message + Assert.areEqual(7, this.data.length, "Array should have 7 items."); + + //the new item should be there + ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1."); + + ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal."); + + }, + + ... +}); +``` +

Working similar to push(), unshift() adds a value to the array, but the item is added to the front (index 0) instead of + the back. This test begins by adding the value -1 to the array. The first assertion determines if the length of the array has been incremented + to 7 to account for the new value. After that, Y.ArrayAssert.indexOf() is used to determine if the value has been placed in the + correct location. The final assertions tests that the entire array is expected by using Y.ArrayAssert.itemsAreEqual().

+ +

Running the tests

+ +

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

+ +``` +//create the console +var r = new Y.Console({ + verbose : true, + newestOnTop : false +}); + +r.render('#testLogger'); + +Y.Test.Runner.add(Y.example.test.ArrayTestCase); + +//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 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.

+ +

Complete Example Source

+ +``` +{{>test-array-tests-source}} +``` diff --git a/src/test/docs/test-async-event-tests.mustache b/src/test/docs/test-async-event-tests.mustache new file mode 100644 index 00000000000..97c8b4bdf10 --- /dev/null +++ b/src/test/docs/test-async-event-tests.mustache @@ -0,0 +1,128 @@ +
+

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 to test the + Y.Anim object. The test waits until the animation is complete + before checking the settings of the animated element.

+
+ +
+ + + {{>test-async-event-tests-source}} +
+ +

Asynchronous Events Test Example

+ +

This example begins by creating a namespace:

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

+``` +Y.example.test.AsyncTestCase = new Y.Test.Case({ + + //name of the test case - if not provided, one is auto-generated + name : "Animation Tests", + + //--------------------------------------------------------------------- + // Test methods - names must begin with "test" + //--------------------------------------------------------------------- + + testAnimation : function (){ + + var myAnim = new Y.Anim({ + node: '#testDiv', + to: { + width: 400 + }, + duration: 3, + easing: Y.Easing.easeOut + }); + + //assign oncomplete handler + myAnim.on("end", function(){ + + //tell the TestRunner to resume + this.resume(function(){ + + Y.Assert.areEqual(document.getElementById("testDiv").offsetWidth, 400, "Width of the DIV should be 400."); + + }); + + }, this, true); + + //start the animation + myAnim.run(); + + //wait until something happens + this.wait(); + + } + +}); +``` +

The only test in the Y.Test.Caseis called testAnimation. It begins by creating a new +Anim object that will animate the width of a div to 400 pixels over three seconds. An +event handler is assigned to the Anim object's end event, within which the +resume() method is called. A function is passed into the resume() method to indicate +the code to run when the test resumes, which is a test to make sure the width is 400 pixels. After that, the +run() method is called to begin the animation and the wait() method is called to +tell the Y.Test.Runner to wait until it is told to resume testing again. When the animation completes, +the end event is fired and the test resumes, assuring that the width is correct.

+

Running the tests

+ +

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

+ +``` +//create the console +var r = new Y.Console({ + verbose : true, + newestOnTop : false +}); + +r.render('#testLogger'); + +//create the logger +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.Caseobject by calling + add() (any number of Y.Test.Caseand TestSuite 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.

+ +

Complete Example Source

+ +``` +{{>test-async-event-tests-source}} +``` diff --git a/src/test/docs/test-async-test.mustache b/src/test/docs/test-async-test.mustache new file mode 100644 index 00000000000..b2333d60f46 --- /dev/null +++ b/src/test/docs/test-async-test.mustache @@ -0,0 +1,141 @@ +
+

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.

+
+ +
+ + + {{>test-async-test-source}} +
+ +

Asynchronous Test Example

+ +

This example begins by creating a namespace:

+``` +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:

+``` +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:

+ +``` +//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.

+ +

Complete Example Source

+ +``` +{{>test-async-test-source}} +``` diff --git a/src/test/docs/test-simple-example.mustache b/src/test/docs/test-simple-example.mustache new file mode 100644 index 00000000000..3e75414c456 --- /dev/null +++ b/src/test/docs/test-simple-example.mustache @@ -0,0 +1,241 @@ +
+

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.

+
+ +
+ + + {{>test-simple-example-source}} +
+ +

Simple Test Example

+ +

This example begins by creating a namespace:

+``` +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:

+``` +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:

+ ``` +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:

+``` +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:

+ +``` +//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.

+ +

Complete Example Source

+ +``` +{{>test-simple-example-source}} +```