forked from madrobby/scriptaculous
-
Notifications
You must be signed in to change notification settings - Fork 0
Test.Unit.Runner
joe-loco edited this page Sep 12, 2010
·
5 revisions
Unit Testing > Test.Unit.Runner
Test.Unit.Runner
is a utility class for writing unit test cases with an easy to use syntax.
new Test.Unit.Runner(functions [, options] );
The functions
and options
parameters are both {key: value}
collections.
Using Test.Unit.Runner
requires you to use a specific XHTML page template, that looks as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Page title</title>
<script src="path/to/prototype.js" type="text/javascript"></script>
<script src="path/to/util.js" type="text/javascript"></script>
<script src="path/to/unittest.js" type="text/javascript"></script>
<!-- other JavaScript includes -->
<link rel="stylesheet" href="path/to/test.css" type="text/css" />
</head>
<body>
<!-- Log output -->
<div id="testlog"> </div>
<!-- here go any elements you do the testing on -->
<!-- Tests -->
<script type="text/javascript" language="javascript">
// <![CDATA[
new Test.Unit.Runner({
// optional setup function, run before each individual test case
setup: function() { with(this) {
// code
}},
// optional teardown function, run after each individual test case
teardown: function() { with(this) {
// code
}},
// test cases follow, each method which starts
// with "test" is considered a test case
testATest: function() { with(this) {
// code
}},
testAnotherTest: function() { with(this) {
// code
}}
}, { options });
// ]]>
</script>
</html>
Notice how the with(this) { ... }
syntax allows for easily calling on methods of the Test.Unit.Testcase class, which includes the assertions, as defined in Test.Unit.Assertions.
Probably the best examples of Test.Unit.Runner are the actual tests used by script.aculo.us. Don’t be afraid to dive in to the code! See the test folder in the repository root.