Você está na página 1de 1

YUI Library: YUI Test Utilit y 2008-2-19 v2.

5
Simple Use Case: TestCase Object Key Events in YUITest YAHOO.tool.TestSuite
See online docs for full list of custom events, including TestSuite- and TestRunner-level events. Methods:
Create a TestCase object with desired tests, add your TestCase All YUITest events are subscribed to at the TestRunner level; e.g.:
add(obj testCase or testSuite) adds
to the TestRunner object, and run the test: YAHOO.tool.TestRunner.subscribe(YAHOO.tool.TestRunner.TEST_FAIL_EVENT, myFn);.
a testCase or testSuite object to a
Test-level Events testSuite
//set up a test case:
var oTestCase = new YAHOO.tool.TestCase({ Event: Fires:
name: "Simple Math",
YAHOO.tool.TestRunner
TEST_PASS_EVENT When an individual test passes.
testEquality: function () { Methods:
TEST_FAIL_EVENT When an individual test fails.
YAHOO.util.Assert.areEqual(4, (2+2), "2+2=4"); add(obj testCase or testSuite) adds
}; Argument Data Object for Test-level Events:
a testCase or testSuite object to the
}); type Type of event. list of items to run
clear() removes all test objects from the
//add the test case to the TestRunner: testCase The testCase object to which this test belongs. runner
YAHOO.tool.TestRunner.add(oTestCase); testName The string name of this test. run() runs all testCase and
testSuites currently queued in
YAHOO.tool.TestRunner.run(); //run the test TestCase-level Events the TestRunner
Event: Fires:
Key Members of the TestCase Object TEST_CASE_BEGIN_EVENT Before the TestCase is run. YAHOO.tool.TestCase
TEST_CASE_COMPLETE_EVENT After the TestCase is run. Methods:
name The name of the TestCase. This will be visible in the
logging of TestCase events. Argument Data Object for TestCase-level Events: wait([fn segment, int delay]) causes
the TestCase to delay the specified
type Type of event.
testname A function that tests functional code via one or more number of milliseconds before
testCase Current TestCase instance. segment is executed
assertions; name must begin with the string “test”. A
resume([fn segment]) resmes a
TestCase can have one or more test members. results passed Number of tests that passed. paused test; if segment is omitted, the
(TEST_CASE_END event failed Number of tests that failed. test automatically passes
setUp Method that prepares your test case to run by, for only)
example, creating needed data constructs or objects. testname result pass or fail.
YAHOO.util.UserAction
message String returned by the test.
tearDown Method that nulls out variables in use by the TestCase, Methods:
detaches event handlers, etc. Note: TestSuite- and TestRunner-level events are also available, containing summary data in
addition to specific TestCase results objects. See online docs for full details. click(obj target, obj options)
_should Special object that provides granular configuration of the simulates a click on the target
test case. It can have the following members: mousedown(obj target, obj
Assertions options) simulates a mousedown on
ignore A name:value pair consisting of a Assertions are accessed via YAHOO.util.Assert
the target
testname and Boolean indicating whether mouseup(obj target, obj options)
to ignore the test (e.g.: ignore: Equality assertions: isTypeOf assertion: simulates a mouseup on the target
areEqual(expected, actual) isTypeOf(sType, sTest, mouseover(obj target, obj options)
{testOne : true /*ignore simulates a mouseover on the target
testOne*/}. areNotEqual(expected, actual) sFailureMessage) mouseout(obj target, obj options)
equivalent to == test YAHOO.util.Assert.isTypeOf(“string simulates a mouseout on the target
error A name:value pair consisting of a Sameness assertions:
”, 5, “Expected string.”); mousemove(obj target, obj
testname and Boolean indicating whether //fails options) simulates a mousemove on
areSame(expected, actual) the target
the test is should fail: areNotSame(expected, actual) isInstanceOf assertion:
keydown(obj target, obj options)
equivalent to === test isInstanceOf(oConstructor, oTestObject, simulates a keydown on the target
error: {testTwo : true sFailureMessage) keyup(obj target, obj options)
/*testTwo should fail*/} Data-type assertions: YAHOO.util.Assert.isInstanceOf(Str simulates a keyup on the target
isArray(arg) ing, “Madrone”, “Expected keypress(obj target, obj options)
Or, a specific error string can substitute isBoolean(arg) string.”); //passes simulates a keypress on the target
for the Boolean: isFunction(arg) can be used to test non-native objects, too
isNumber(arg)
isObject(arg) object and function return true
Special Value Assertions: Dependencies
error: {testTwo : “Expected isFalse, isTrue, isNaN, isNotNaN,
string.” /*testTwo should isString(arg) isNull, isNotNull, isUndefined, The YUI Test Utility requires the
fail with this specific isNotUndefined Yahoo Global Object, Dom
error message*/}. YAHOO.util.Assert.isNull(7, Collection, Event Utility, and
“Expected null.”); //fails Logger Control. The Logger’s
All TestCase configurations should be passed into the TestCase constructor as an object literal;
CSS file and the YUI Test CSS
see Simple Use Case.
file are also required.

Você também pode gostar