SoftwareTesting:Tools:jsUnit: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
* example jsunit-based tests ([http://people.mozilla.com/~davel/jsunit/ davel's examples])
** urls in test suite pages are resolved relative to the url of the test runner page, not the url of the test suite page
** can we use chrome urls (with appropriate manifest files) to access the jsunit core js files in a path-independent manner?  this means we just have to worry about the test runner page and the test suite/case pages.
** this is only a problem for packaging jsunit-based tests in our tree so that they point to the same core jsunit files
=Description=
=Description=
[http://www.jsunit.net/ jsUnit] is a fairly mature harness for in-browser testing.  We can easily use it to run tests that can run as content
[http://www.jsunit.net/ jsUnit] is a fairly mature harness for in-browser testing.  We can easily use it to run tests that can run as content
Line 14: Line 10:
# use the file picker to select the test you just wrote
# use the file picker to select the test you just wrote
# run the test
# run the test
==Example==
==Simple Example==
dom_3348.html: tagName of HTML element should give tag in upper case (bug 3348)
dom_3348.html: tagName of HTML element should give tag in upper case (bug 3348)
<pre>
<pre>
Line 42: Line 38:
===Output===
===Output===
the test passes
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 <tt>setUpPage</tt> function, and setting <tt>setUpPageStatus</tt> to 'complete' when it is ok to run the test assertions
<pre>
<!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">
    &lt;/pre>
</dd>
<dt>Expected result:</dt>
<dd>
    <pre>
Previous:    width: 20em;
New:        width: auto;
    &lt;/pre>
</dd>
</dl>
</pre>
==Other Examples==
* example jsunit-based tests ([http://people.mozilla.com/~davel/jsunit/ davel's examples])
=Comments=
=Comments=
* jsUnit also supports test suites.
* jsUnit also supports test suites.
** paths for files in addTestPage() are evaluated relative to the testRunner page, not the test suite page
** paths for files in addTestPage() are evaluated relative to the testRunner page, not the test suite page
=To do list=
=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 [http://lxr.mozilla.org/mozilla/source/toolkit/mozapps/update/src/testnsUpdateService.js here] for a quick attempt to do so
* convert, use as base, or merge with xpcshell-simple harness?
[[Category:SoftwareTesting]]
[[Category:SoftwareTesting]]

Latest revision as of 15:47, 17 September 2006

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?