README 6.0 KB

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