/ Published in: JavaScript
written test in yui for my jquery code. :)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>JS Bin</title> <!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <style> article, aside, figure, footer, header, hgroup, menu, nav, section { display: block; } .yui3-skin-sam .yui3-console-entry-pass .yui3-console-entry-cat { background-color: green; color: #fff; } .yui3-skin-sam .yui3-console-entry-fail .yui3-console-entry-cat { background-color: red; color: #fff; } .yui3-skin-sam .yui3-console-entry-ignore .yui3-console-entry-cat { background-color: #666; } </style> </head> <body class="yui3-skin-sam yui-skin-sam"> <p id="hello">Hello World</p> <div id="testLogger"></div> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script> <script src="yuitest-min.js"></script> <script> jQuery(function($){ $("#hello").hover( function(){ $(this).css("color","red"); }, function(){ $(this).css("color","blue"); }); }); YUI().use("console", function (Y) { var dataTestCase = new YUITest.TestCase({ //name of the test case - if not provided, one is auto-generated name : "Data Tests", //--------------------------------------------------------------------- // setUp and tearDown methods - optional //--------------------------------------------------------------------- /* * Cleans up everything that was created by setUp(). */ tearDown : function () { $("#hello").css("color","black"); }, //--------------------------------------------------------------------- // Test methods - names must begin with "test" //--------------------------------------------------------------------- testMouseOver : function () { var Assert = YUITest.Assert; $("#hello").trigger("mouseenter"); Assert.areEqual("red", $("#hello").css("color"), "Allah g kia karoon"); }, testMouseOut : function () { var Assert = YUITest.Assert; $("#hello").trigger("mouseleave"); Assert.areEqual("blue", $("#hello").css("color")); }, //--------------------------------------------------------------------- // Test methods - also, may just have a space in the name //--------------------------------------------------------------------- "This test should fail": function(){ var Assert = YUITest.Assert; Assert.fail("This test was supposed to fail."); } }); var suite = new YUITest.TestSuite("Example Suite"); suite.add(dataTestCase); //create the console var r = new Y.Console({ newestOnTop : false, style: 'block' // to anchor in the example content }); r.render('#testLogger'); var TestRunner = YUITest.TestRunner; TestRunner.add(suite); //function to handle events generated by the testrunner function logEvent(event){ //data variables var message = "", messageType = ""; switch(event.type){ case TestRunner.BEGIN_EVENT: message = "Testing began at " + (new Date()).toString() + "."; messageType = "info"; break; case TestRunner.COMPLETE_EVENT: message = Y.substitute("Testing completed at " + (new Date()).toString() + ".\n" + "Passed:{passed} Failed:{failed} " + "Total:{total} ({ignored} ignored)", event.results); messageType = "info"; break; case TestRunner.TEST_FAIL_EVENT: message = event.testName + ": failed.\n" + event.error.getMessage(); messageType = "fail"; break; case TestRunner.TEST_IGNORE_EVENT: message = event.testName + ": ignored."; messageType = "ignore"; break; case TestRunner.TEST_PASS_EVENT: message = event.testName + ": passed."; messageType = "pass"; break; case TestRunner.TEST_SUITE_BEGIN_EVENT: message = "Test suite \"" + event.testSuite.name + "\" started."; messageType = "info"; break; case TestRunner.TEST_SUITE_COMPLETE_EVENT: message = Y.substitute("Test suite \"" + event.testSuite.name + "\" completed" + ".\n" + "Passed:{passed} Failed:{failed} " + "Total:{total} ({ignored} ignored)", event.results); messageType = "info"; break; case TestRunner.TEST_CASE_BEGIN_EVENT: message = "Test case \"" + event.testCase.name + "\" started."; messageType = "info"; break; case TestRunner.TEST_CASE_COMPLETE_EVENT: message = Y.substitute("Test case \"" + event.testCase.name + "\" completed.\n" + "Passed:{passed} Failed:{failed} " + "Total:{total} ({ignored} ignored)", event.results); messageType = "info"; break; default: message = "Unexpected event " + event.type; message = "info"; } //only log if required Y.log(message, messageType, "TestRunner"); } //listen for events to publish to the logger TestRunner.attach(TestRunner.BEGIN_EVENT, logEvent); TestRunner.attach(TestRunner.COMPLETE_EVENT, logEvent); TestRunner.attach(TestRunner.TEST_CASE_BEGIN_EVENT, logEvent); TestRunner.attach(TestRunner.TEST_CASE_COMPLETE_EVENT, logEvent); TestRunner.attach(TestRunner.TEST_SUITE_BEGIN_EVENT, logEvent); TestRunner.attach(TestRunner.TEST_SUITE_COMPLETE_EVENT, logEvent); TestRunner.attach(TestRunner.TEST_PASS_EVENT, logEvent); TestRunner.attach(TestRunner.TEST_FAIL_EVENT, logEvent); TestRunner.attach(TestRunner.TEST_IGNORE_EVENT, logEvent); //run the tests TestRunner.run(); }); </script> </body> </html>