SoftwareTesting:Tools:jsUnit

From MozillaWiki
Jump to: navigation, search

Description

jsUnit is a fairly mature harness for in-browser testing. We can easily use it to run tests that can run as content

How to run the test

  1. download jsunit from jsunit.net
    • I've had success playing with 2.2a11
  2. unzip the file
    • I didn't do any of the installation stuff for jsUnit Server - I just used the unpacked files as-is
  3. add your test to the jsunit directory (the one containing testRunner.html)
  4. load testRunner.html in your browser
  5. use the file picker to select the test you just wrote
  6. run the test

Simple Example

dom_3348.html: tagName of HTML element should give tag in upper case (bug 3348)

<HTML>
<HEAD>
<script language="javascript" src="app/jsUnitCore.js"></script>

<script>
function test3348()
{
        var oForm = document.getElementById("form1");
        assertEquals("FORM", oForm.tagName);
}
</script>
</HEAD>
<BODY>


<form id="form1">
<input type="button" value="click here" onclick="buttonClick();">
</form>


</BODY>
</HTML>

Output

the test passes

Example using events

dom_338679.html - DOMAttrModified event should not report new value as prevValue for style changes

  • note the use of the setUpPage function, and setting setUpPageStatus to 'complete' when it is ok to run the test assertions
<!DOCTYPE html>
<title>Testcase bug 338679</title>
<script language="javascript" src="app/jsUnitCore.js"></script>
<script>
function setUpPage() {
    with (document.getElementById("out")) {
        addEventListener("DOMAttrModified", attr_modified, false)
        style.width = "auto"
    }
}
function attr_modified(ev) {
    this.textContent = "Previous:\t" + ev.prevValue + "\nNew:\t\t" + ev.newValue;
    setUpPageStatus = 'complete'
}

function testBug338679() {
assertEquals("Previous:\twidth: 20em;\nNew:\t\twidth: auto;",document.getElementById("out").textContent);
}
</script> 
<dl>
<dt>Actual result:</dt>
<dd>
    <pre id="out" style="width: 20em">
    </pre>
</dd>

<dt>Expected result:</dt>

<dd>
    <pre>
Previous:    width: 20em;
New:        width: auto;
    </pre>
</dd>
</dl>

Other Examples

Comments

  • jsUnit also supports test suites.
    • paths for files in addTestPage() are evaluated relative to the testRunner page, not the test suite page

To do list

  • resolve how this should be checked in / referenced from the tree
    • check in to tree?
    • make accessible via resource: or chrome: url?
    • package as an extension?
      • a wrapper extension that takes as config input the url or directory where the jsunit files live?
  • make it easier to detect pass/fail status from an automated invocation harness
    • add test runner that reports pass/fail via dump, log, or write to file
  • convert, or use as a base, to run tests with chrome privileges
    • need to break dependency on running in a browser window - see here for a quick attempt to do so
  • convert, use as base, or merge with xpcshell-simple harness?