YUI3 Test and JsTestDriver
As some of you might know I am a big fan of YUI3. Currently I am revamping the JavaScript tests at local.ch which uses YUI3 for all the new stuff we do. I found some code from lazr and modified it a little to match my taste better. So basically all you do is include that snipped:
YUI.add('jstestdriver', function(Y) {
R = Y.namespace('jstestdriver.Runner');
R.add = function add(suite) {
if ((typeof jstestdriver === "undefined")) {
// If we are not running under JsTestDriver, then
// register the suite with Y.Test.Runner and run it.
Y.Test.Runner.add(suite);
} else {
// If ``jstestdriver`` is defined, that means we are
// running under JsTestDriver, so instead register each
// test case from the suite as a separate TestCase() with
// JsTestDriver.
for each(var test_case in suite.items) {
TestCase(test_case.name, test_case);
}
}
};
R.run = function run(suite) {
if ((typeof jstestdriver === "undefined")) {
// If we are not running under JsTestDriver, then run all
// the registered test suites with Y.Test.Runner.
var yconsole = new Y.Console({
newestOnTop: false
});
yconsole.render('#log');
Y.Test.Runner.run();
}
}
},
'0.1', {
requires: ['test', 'console']
});
Now you can write your tests as usual you only need to to the assigning a bit different. Instead of the normal YUI3 Test Runner you use this:
Y.jstestdriver.Runner.add(suite); Y.jstestdriver.Runner.run();
What you have to watch out for: Load _ALL_ Yahoo code with your tests otherwise you will have very funky side effects of tests not running the first time but passing the second one, when yui3 loaded the missing files. I created one big file including everything that I use for that purpose.
Comments
Hi, I used your method. I keep the snip in a seperate Js file. And before load the tests, I load it. but when I run Js-tes-driver, the code above will never be accessed.
Try to load it after the tests :) Probably the better way is to do it as an onload event anyways. I saw that the guys from lazr do it even more advanced now so I will probably do an update to this post soon.
acme
@ 16.12.2009 16:37 CEST
Thanks for your quick response.:)
I tried that and failed again. I 'm wondering in the case you described what the js excute flow is. Actually, I'm new to YUI3. And YUI 3 seems to be very different from YUI2. I can make YUI2 work with JsTestDriver.
If I put the YUI.add('jstestdriver') after the definition of testcase(in the test case, I also add this piece of code Y.jstestdriver.Runner.add(suite);...) so seems I try to use Y.jstestdriver before I register the jstestdriver with yui. weird...
Thanks for your quick response.:)
I tried that and failed again. I 'm wondering in the case you described what the js excute flow is. Actually, I'm new to YUI3. And YUI 3 seems to be very different from YUI2. I can make YUI2 work with JsTestDriver.
If I put the YUI.add('jstestdriver') after the definition of testcase(in the test case, I also add this piece of code Y.jstestdriver.Runner.add(suite);...) so seems I try to use Y.jstestdriver before I register the jstestdriver with yui. weird...
acme
@ 16.12.2009 17:36 CEST
I made some debugging. Since in YUI 3 the callback function is called after the required component is loaded. When I run the test case, it seems after YUI finish the loading and begin to execute the callback function, at this moment, JSTestDriver have been finished runing and exit. So when we try to call functions of JSTestDriver, it's no longer available...
I made some debugging. Since in YUI 3 the callback function is called after the required component is loaded. When I run the test case, it seems after YUI finish the loading and begin to execute the callback function, at this moment, JSTestDriver have been finished runing and exit. So when we try to call functions of JSTestDriver, it's no longer available...
acme
@ 21.12.2009 03:52 CEST
Hi, I've solved it by disable the dynamic loading and load all the required JS by specifying them in the configuration file.
Only one problem now, when a test fails using the YahooAssert framework, JsTestDriver will treat it as an error other than fail..
Hi, I've solved it by disable the dynamic loading and load all the required JS by specifying them in the configuration file.
Only one problem now, when a test fails using the YahooAssert framework, JsTestDriver will treat it as an error other than fail..
Yes unfortunately that's currently the case. I hope I can fix that somehow with patching YUI Test a little to report correctly to JsTestDriver :)
No new comments allowed (anymore) on this post.