4 JSUnit is intended to aid in testing the structure of the HTML
5 document generated by ASP.Net, as well as to test behavior of scripted
6 elements on webpages (like the client side scripting aspects of
9 It's syntax is meant to resemble that of NUnit as much as possible, so
10 moving from one to the other shouldn't require too much effort to
11 understand. There is some boilerplate, but the syntax for writing
12 tests is largely the same. Here is an example test script:
14 <script Language="JavaScript">
16 Example_Test1 : function () {
17 Assert.IsTrue (true, "true test");
20 Example_Test2 : function () {
21 Assert.AreEqual ("hi there", "hi" + " there", "JS string concat");
26 Your tests must be written in the above manner, as properties of
27 variable called "TestFixture". JSUnit will call each of your tests
28 automatically. As you can see, as in NUnit "Assert." is used as a
29 prefix for all of your test assertions.
35 JSUnit's Assert object has many of the same methods as NUnit's, with a
36 few differences. One very important thing to note is when you see an
37 argument called "expr", it is evaluated as javascript, so you can
38 include code to query attributes, set attributes, do just about
39 anything you want. This gets evaluated as part of your assertion, and
40 any errors are treated as test failures.
42 Here is a complete list of Assert's methods:
45 if @expr evaluates to false, the test fails.
48 if @expr does not evaluate to false, the test fails.
51 if @expr is not null, the test fails.
54 if @expr is null, the test fails.
56 AreEqual(expected, expr, msg)
57 if @expected and @expr (after evaluation) do not equal, the
60 AreEqualCase(expected, expr, msg);
61 Same as AreEqual, only case insensitive.
63 NotEqual(expected, expr, msg);
64 if @expected and @expr (after evaluation) are equal, the test
67 IsFunction(expr, msg);
68 if @expr evaluates such that the javascript type is not
69 'function', the test fails.
71 AttributeHasValue(expected, attr, msg)
72 if the value of the attribute @attr of the bound element does
73 not equal @expected, the test fails. More on "the bound
80 JSUnit has a number of functions that are available from within your
81 test scripts to help make writing test assertions less painful.
83 JSUnit_BindElement(elementid);
84 sets the bound element for the currently running test to
85 document.getElementById(@elementid).
87 JSUnit_GetElement([elementid]);
88 if @elementid is present, looks up the element using
89 document.getElementById. otherwise it returns the bound
92 JSUnit_GetAttribute(attrname[, elementid]);
93 if @elementid is present, looks up the attribute named
94 @attrname on that element. otherwise does the lookup on the
97 JSUnit_Click(element);
98 Causes the browser to react as if the user had clicked on
99 element. @element can be any html element with a click()
100 method, an onClick handler, or an href attribute.
102 JSUnit_TestCausesPageLoad();
103 a special function to let JSUnit know that at some point
104 during the running of the current test, a page load will
105 happen, be it from a form submit, calling .load, setting
106 document.src, whatever. Special care is needed when writing
107 tests that cause page loads. See below.
111 Tests that cause page loads
112 ===========================
114 While testing asp.net validators, we needed to be able to cause page
115 loads to happen and write tests based on the resulting html. This is
116 usually done by creating an asp:button in the form on the test page
117 and calling "JSUnit_Click()" as part of the test.
119 Once this method has been called, no further assertions can be made
120 about the state of the page. the JSUnit_Click() call (or whatever
121 mechanism you use to load a new page) should be the last thing in that
122 test. The JSUnit machinery will wait for the page to fully load and
123 then continue on through the list of tests in the current TextFixture.
124 This means you need to write page load tests split into two parts,
132 TestBeforeLoad: function () {
133 var button = JSUnit_GetElement ("formsubmitbutton");
135 /* this function causes a page load. Let JSUnit know. */
136 JSUnit_TestCausesPageLoad ();
138 /* assertions about current state of page here */
140 /* a submit button click nothing else after this call */
141 JSUnit_Click (button);
144 TestAfterLoad: function () {
145 /* assertions about the state of the page after the load has
154 Telling jsunit about your tests
155 ===============================
157 The following is how you build up your test page:
161 <script language="JavaScript" src="../jsunit/jsunit.js"></script>
163 <script language="JavaScript">
165 var JSUnit_TestPages = [
166 { url:"http://www.mywebsite.com/", script:"test-script.html" },
167 { url:"combined-test.html" },
174 <frameset onload="JSUnit_OnLoad()" rows="*,1,1" border=0>
175 <frame id="test-results" src="../jsunit/jsunit-results.html"/>
176 <frame id="test-run" src="about:blank"/>
177 <frame id="test-scripts" src="about:blank"/>
181 Pretty much the only thing you should change is the list of test
182 pages, the variable JSUnit_TestPages. Note the two tests listed in
183 the example. One specifies both a test url and script file, and the
184 other just a url. The first instance is useful for testing remote
185 servers, or where you're testing html that you don't want to mark up
186 with tests. The second instance is useful when writing small unit
187 tests, where you want to keep the html and javascript together.
189 Including tests inside the html
190 ===============================
192 For tests such as the second one in the example above, you embed your
193 tests directly in the html along with the content you're testing, like
198 Hi, this is a combined JSUnit test.
202 <script Language="JavaScript">