Changeset 7847

Show
Ignore:
Timestamp:
03/29/07 23:13:52 (22 months ago)
Author:
alex
Message:

adds support for:

  • registering URLs
  • loading of tests in loaded URLs
  • automatic timeouts for loaded URLs
  • automatic test creation from code strings
  • improvements in use with dojo
  • group-level setUp() and tearDown()

Also includes myriad bug fixes. Refs #2550. Still need to document many of these changes in the porting guide.

Location:
dojo/trunk/tests
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • dojo/trunk/tests/runner.html

    r7647 r7847  
    66        <head> 
    77                <title>The Dojo Unit Test Harness, $Rev$</title> 
     8                <script type="text/javascript" src="../dojo.js"   
     9                        djConfig="isDebug: true"></script> 
     10                <script type="text/javascript"> 
     11                        try{ 
     12                                dojo.require("tests.runner"); 
     13                        }catch(e){ 
     14                                document.write("<scr"+"ipt type='text/javascript' src='runner.js'></scr"+"ipt>"); 
     15                                document.write("<scr"+"ipt type='text/javascript' src='_browserRunner.js'></scr"+"ipt>"); 
     16                        } 
     17                </script> 
    818                <style type="text/css"> 
    919                        body { 
     
    148158                        } 
    149159                </style> 
    150                 <script type="text/javascript" src="../dojo.js"></script> 
    151                 <script type="text/javascript" src="runner.js"></script> 
    152                 <script type="text/javascript"> 
    153                         if(!this["dojo"]){ 
    154                                 document.write("<scr"+"ipt type='text/javascript' src='_browserRunner.js'></scr"+"ipt>"); 
    155                                 document.write("<scr"+"ipt type='text/javascript' src='_base.js'></scr"+"ipt>"); 
    156                         } 
    157                 </script> 
    158160        </head> 
    159161        <body> 
  • dojo/trunk/tests/runner.js

    r7704 r7847  
    33 
    44// package system gunk.  
    5 if(this["dojo"]){ 
    6         // Ensure we can run w/o Dojo in a Rhino or SM environment 
     5try{ 
    76        dojo.provide("tests.runner"); 
    8 }else if(!this["tests"]){ 
    9         tests = {}; 
     7}catch(e){ 
     8        if(!this["tests"]){ 
     9                tests = {}; 
     10        } 
    1011} 
    1112 
     
    206207                if(this.fired != -1){ 
    207208                        if(!this.silentlyCancelled){ 
    208                                 tests.raise("already called!"); 
     209                                throw new Error("already called!"); 
    209210                        } 
    210211                        this.silentlyCancelled = false; 
     
    268269                        var pair = chain.shift(); 
    269270                        var f = pair[fired]; 
    270                         if (f == null) { 
     271                        if(f == null){ 
    271272                                continue; 
    272273                        } 
     
    274275                                res = f(res); 
    275276                                fired = ((res instanceof Error) ? 1 : 0); 
    276                                 if(res instanceof tests.Deferred) { 
     277                                if(res instanceof tests.Deferred){ 
    277278                                        cb = function(res){ 
    278279                                                self._continue(res); 
     
    355356} 
    356357 
    357 tests.registerTest = function(/*String*/ group, /*Function or Object*/ test){ 
     358tests.registerGroup = function( /*String*/ group,  
     359                                                                /*Array||Function||Object*/ tests,  
     360                                                                /*Function*/ setUp,  
     361                                                                /*Function*/ tearDown){ 
     362        // summary: 
     363        //              registers an entire group of tests at once and provides a setUp and 
     364        //              tearDown facility for groups. If you call this method with only 
     365        //              setUp and tearDown parameters, they will replace previously 
     366        //              installed setUp or tearDown functions for the group with the new 
     367        //              methods. 
     368        // group: 
     369        //              string name of the group 
     370        // tests: 
     371        //              either a function or an object or an array of functions/objects. If 
     372        //              an object, it must contain at *least* a "runTest" method, and may 
     373        //              also contain "setUp" and "tearDown" methods. These will be invoked 
     374        //              on either side of the "runTest" method (respectively) when the test 
     375        //              is run. If an array, it must contain objects matching the above 
     376        //              description or test functions. 
     377        // setUp: a function for initializing the test group 
     378        // tearDown: a function for initializing the test group 
     379        if(tests){ 
     380                this.register(group, tests); 
     381        } 
     382        if(setUp){ 
     383                this._groups[group].setUp = setUp; 
     384        } 
     385        if(tearDown){ 
     386                this._groups[group].tearDown = tearDown; 
     387        } 
     388} 
     389 
     390tests._getTestObj = function(group, test){ 
     391        var tObj = test; 
     392        if(typeof test == "string"){ 
     393                if(test.substr(0, 4)=="url:"){ 
     394                        return this.registerUrl(group, test); 
     395                }else{ 
     396                        tObj = { 
     397                                name: test.replace("/\s/g", "_") 
     398                        }; 
     399                        tObj.runTest = new Function(test); 
     400                } 
     401        }else if(typeof test == "function"){ 
     402                // if we didn't get a fixture, wrap the function 
     403                tObj = { "runTest": test }; 
     404                if(test["name"]){ 
     405                        tObj.name = test.name; 
     406                }else{ 
     407                        try{ 
     408                                var fStr = "function "; 
     409                                var ts = tObj.runTest+""; 
     410                                if(0 <= ts.indexOf(fStr)){ 
     411                                        tObj.name = ts.split(fStr)[1].split("(", 1)[0]; 
     412                                } 
     413                                // tests.debug(tObj.runTest.toSource()); 
     414                        }catch(e){ 
     415                        } 
     416                } 
     417                // FIXME: try harder to get the test name here 
     418        } 
     419        return tObj; 
     420} 
     421 
     422tests.registerTest = function(/*String*/ group, /*Function||Object*/ test){ 
    358423        // summary: 
    359424        //              add the provided test function or fixture object to the specified 
     
    371436                this._groups[group].inFlight = 0; 
    372437        } 
    373         var tObj = test; 
    374         if(typeof test == "function"){ 
    375                 // if we didn't get a fixture, wrap the function 
    376                 tObj = { "runTest": test }; 
    377                 if(test["name"]){ 
    378                         tObj.name = test.name; 
    379                 }else{ 
    380                         try{ 
    381                                 var fStr = "function "; 
    382                                 var ts = tObj.runTest+""; 
    383                                 if(0 <= ts.indexOf(fStr)){ 
    384                                         tObj.name = ts.split(fStr)[1].split("(", 1)[0]; 
    385                                 } 
    386                                 // tests.debug(tObj.runTest.toSource()); 
    387                         }catch(e){ 
    388                         } 
    389                 } 
    390                 // FIXME: try harder to get the test name here 
    391         } 
     438        var tObj = this._getTestObj(group, test); 
     439        if(!tObj){ return; } 
    392440        this._groups[group].push(tObj); 
    393441        this._testCount++; 
    394442        this._testRegistered(group, tObj); 
     443        return tObj; 
    395444} 
    396445 
     
    405454} 
    406455 
    407 tests.registerTestUrl = function(/*String*/ group, /*String*/ url){ 
     456// FIXME: move implementation to _browserRunner? 
     457tests.registerUrl = function(   /*String*/ group,  
     458                                                                /*String*/ url,  
     459                                                                /*Integer*/ timeout){ 
    408460        this.debug("ERROR:"); 
    409         this.debug("\tNO registerTestUrl() METHOD AVAILABLE."); 
     461        this.debug("\tNO registerUrl() METHOD AVAILABLE."); 
    410462        // this._urls.push(url); 
     463} 
     464 
     465tests.registerString = function(group, str){ 
    411466} 
    412467 
     
    419474        if(     (arguments.length == 1)&& 
    420475                (typeof groupOrNs == "string") ){ 
    421                 this.registerTestUrl(groupOrNs); 
     476                if(groupOrNs.substr(0, 4)=="url:"){ 
     477                        this.registerUrl(groupOrNs); 
     478                }else{ 
     479                        this.registerTest("ungrouped", groupOrNs); 
     480                } 
    422481        } 
    423482        if(arguments.length == 1){ 
     
    426485        } 
    427486        if(typeof testOrNull == "string"){ 
    428                 this.registerTestNs(groupOrNs, testOrNull); 
     487                if(testOrNull.substr(0, 4)=="url:"){ 
     488                        this.registerUrl(testOrNull); 
     489                }else{ 
     490                        this.registerTest(groupOrNs, testOrNull); 
     491                } 
     492                // this.registerTestNs(groupOrNs, testOrNull); 
    429493                return; 
    430494        } 
     
    673737} 
    674738 
    675 if(this["dojo"]){ 
     739try{ 
    676740        dojo.platformRequire({ 
    677741                browser: ["tests._browserRunner"], 
     
    679743                spidermonkey: ["tests._rhinoRunner"] 
    680744        }); 
    681         dojo.require("tests._base"); 
    682         dojo.addOnLoad(function(){ 
    683                 setTimeout(function(){ 
    684                         tests.run(); 
    685                 }, 100); 
    686         }); 
     745        var _shouldRequire = (dojo.isBrowser) ? (window == window.parent) : true; 
     746        if(_shouldRequire){ 
     747                dojo.require("tests._base"); 
     748                dojo.addOnLoad(function(){ 
     749                        setTimeout(function(){ 
     750                                tests.run(); 
     751                        }, 100); 
     752                }); 
     753        } 
    687754        // set us up for a run 
    688 }else if(this["load"]){ 
    689         load("_rhinoRunner.js"); 
    690         load("_base.js"); 
    691  
    692         print("\n"+tests._line); 
    693         print("The Dojo Unit Test Harness, $Rev$"); 
    694         print("Copyright (c) 2007, The Dojo Foundation, All Rights Reserved"); 
    695         print(tests._line, "\n"); 
    696  
    697         tests.run(); 
    698 } 
     755}catch(e){ 
     756        if(this["load"]){ 
     757                load("_rhinoRunner.js"); 
     758                load("_base.js"); 
     759 
     760                print("\n"+tests._line); 
     761                print("The Dojo Unit Test Harness, $Rev$"); 
     762                print("Copyright (c) 2007, The Dojo Foundation, All Rights Reserved"); 
     763                print(tests._line, "\n"); 
     764 
     765                tests.run(); 
     766        } 
     767} 
  • dojo/trunk/tests/_browserRunner.js

    r7704 r7847  
    187187 
    188188                tests._groupStarted = function(group){ 
     189                        // console.debug("_groupStarted", group); 
    189190                        getGroupNode(group).className = "inProgress"; 
    190191                } 
    191192 
    192193                tests._groupFinished = function(group, success){ 
     194                        // console.debug("_groupFinished", group); 
    193195                        getGroupNode(group).className = (success) ? "success" : "failure"; 
    194196                } 
    195197 
    196198                tests._testStarted = function(group, fixture){ 
     199                        // console.debug("_testStarted", group, fixture.name); 
    197200                        getFixtureNode(group, fixture).className = "inProgress"; 
    198201                        fixture.startTime = new Date(); 
     
    200203 
    201204                tests._testFinished = function(group, fixture, success){ 
     205                        var fn = getFixtureNode(group, fixture); 
     206                        fn.getElementsByTagName("td")[2].innerHTML = ((new Date())-fixture.startTime)+"ms"; 
     207                        fn.className = (success) ? "success" : "failure"; 
    202208                        this.debug(((success) ? "PASSED" : "FAILED"), "test:", fixture.name); 
    203                         var fn = getFixtureNode(group, fixture); 
    204                         fn.className = (success) ? "success" : "failure"; 
    205                         fn.getElementsByTagName("td")[2].innerHTML = ((new Date())-fixture.startTime)+"ms"; 
     209                } 
     210 
     211                // FIXME: move implementation to _browserRunner? 
     212                tests.registerUrl = function(   /*String*/ group,  
     213                                                                                /*String*/ url,  
     214                                                                                /*Integer*/ timeout){ 
     215                        var tg = new String(group); 
     216                        this.register(group, { 
     217                                name: url, 
     218                                setUp: function(){ 
     219                                        tests.currentGroupName = tg; 
     220                                        tests.currentGroup = this; 
     221                                        tests.currentUrl = url; 
     222                                        this.d = new tests.Deferred(); 
     223                                        tests.currentTestDeferred = this.d; 
     224                                        showTestPage(); 
     225                                        byId("testBody").src = url; 
     226                                }, 
     227                                timeout: timeout||10000, // 10s 
     228                                // timeout: timeout||1000, // 10s 
     229                                runTest: function(){ 
     230                                        // FIXME: implement calling into the url's groups here!! 
     231                                        return this.d; 
     232                                }, 
     233                                tearDown: function(){ 
     234                                        tests.currentGroupName = null; 
     235                                        tests.currentGroup = null; 
     236                                        tests.currentTestDeferred = null; 
     237                                        tests.currentUrl = null; 
     238                                        // this.d.errback(false); 
     239                                        // byId("testBody").src = "about:blank"; 
     240                                        showLogPage(); 
     241                                } 
     242                        }); 
    206243                } 
    207244 
     
    258295                _addOnEvt("load", setListHeight); 
    259296                _addOnEvt("load", function(){ 
     297                        if(loaded){ return; } 
    260298                        loaded = true; 
    261299                        groupTemplate = byId("groupTemplate"); 
     
    274312                _addOnEvt("load",  
    275313                        function(){ 
     314                                if(loaded){ return; } 
    276315                                if(!byId("play")){  
    277316                                        // make sure we've got an ammenable DOM structure 
     
    307346        }else{ 
    308347                // we're in an iframe environment. Time to mix it up a bit. 
    309                 tests = window.parent.tests; 
     348 
     349                _tests = window.parent.tests; 
     350                var _thisGroup = _tests.currentGroupName; 
     351                var _thisUrl = _tests.currentUrl; 
     352                if(_thisGroup){ 
     353                        tests._testRegistered = function(group, tObj){ 
     354                                _tests._updateTestList(_thisGroup, tObj); 
     355                        } 
     356                        tests._onEnd = function(){ 
     357                                _tests.currentTestDeferred.callback(true); 
     358                        } 
     359                        var otr = tests._getTestObj; 
     360                        tests._getTestObj = function(){ 
     361                                var tObj = otr.apply(tests, arguments); 
     362                                tObj.name = _thisUrl+"::"+arguments[0]+"::"+tObj.name; 
     363                                return tObj; 
     364                        } 
     365                        tests.debug = tests.hitch(_tests, "debug"); 
     366                        tests.registerUrl = tests.hitch(_tests, "registerUrl"); 
     367                        tests._testStarted = function(group, fixture){ 
     368                                _tests._testStarted(_thisGroup, fixture); 
     369                        } 
     370                        tests._testFinished = function(g, f, s){ 
     371                                _tests._testFinished(_thisGroup, f, s); 
     372                        } 
     373                } 
    310374        } 
    311375