Autorun for JsUnit tests
Develop | Posted February 16, 2009

While writing some Javascript unit tests using JsUnit today, I finally got sick of having to enter my test page's URL in the JsUnit test runner page. More friction means fewer tests. I went looking in the JsUnit documentation and found that they have some handy query strings, but did not seem to have a simple function for doing this. No problem, I hacked one together and it works great:


 function autoRunJsUnit( runner )
{
// Do not redirect if running inside testRunner.html

if ( window.location.href.indexOf('cacheBuster') == -1 )
window.location = runner + "?autoRun=true&testPage=" +
window.location.href;
}

Just invoke this function on page load with the path to the testRunner.html and your tests will run automatically.

A quick explanation. When testRunner.html loads the test file, it adds a cacheBuster query parameter to the URL to make sure it gets the latest version of the file (looks like the value is just the unix time, but no matter). Checking for that parameter breaks the infinite redirect loop.

Note to JSUnit developers: If JsUnit is being actively developed (last release an alpha in 2006), please incorporate something like this into jsUnitCore.js. Feel free to steal my code if need be.

Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox