I recently read the basic Ajax tutorial and learned how to use jsunit to test and track JavaScript.
I. Use jsunit to test JavaScript,
1, ready for jsunit, can be from http://sourceforge.net/projects/jsunit/
Download the jsunit package and decompress it to the project file.
Assert ([comment],
Booleanvalue)
Asserttrue ([comment], booleanvalue)
Assertfalse ([comment], booleanvalue)
Assertequals ([comment], value1, value2)
Assertnotequals ([comment], value1,
Value2)
Assertnull ([comment], value)
Assertnotnull ([comment], value)
Assertundefined ([comment], value)
Assertnotundefined ([comment], value)
Assertnan ([comment], value)
Assertnotnan ([comment], value)
Fail (comment)
2. Prepare a simple test page:
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <title> A simple test page </title> <br/> <MCE: script Type = "text/JavaScript" src = "jsunit/APP/jsunitcore. JS "mce_src =" jsunit/APP/jsunitcore. JS "> </MCE: SCRIPT> <br/> <MCE: Script Type =" text/JavaScript "src =" javascripts/sample. JS "mce_src =" javascripts/sample. JS "> <! -- </P> <p> // --> </MCE: SCRIPT> <br/> <MCE: Script Type = "text/JavaScript"> <! -- <Br/> function setup () {<br/> document. getelementbyid ("value1 "). value = "2"; <br/> document. getelementbyid ("value2 "). value = "2"; <br/>}< br/> function testvalidargs () {<br/> assertequals ("2 + 2 shocould equal 4", 4, addnumbers (); <br/>}< br/> function addnumbers () {<br/> var val1 = document. getelementbyid ("value1 "). value; <br/> var val2 = document. getelementbyid ("value2 "). value; <br/> return addtwonumbers (val1, val2); <br/>}< br/> function teardown () {<br/> document. getelementbyid ("value1 "). value = ""; <br/> document. getelementbyid ("value2 "). value = ""; <br/>}< br/> function exposetestfunctionnames () {<br/> var tests = new array (1 ); <br/> tests [0] = "testvalidargs"; <br/> return tests; <br/>}</P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <input type = "text" size = "3" id = "value1"/> <br/> <input type = "text" size = "3" id = "value2"/> <br/> <input type = "button" value = "add" onclick = "addnumbers () "/> <br/> This is a simple test page for addtwonumbers (value1, value2 ). <br/> </body> <br/> </ptml>
3. Open the testrunner.html file and enter the test page for testing.
Jsunit supports many methods of setup ()
And
Teardown ()
And
Setuppage (), etc. After reading this, I have gained a better understanding of the automated testing.
Test results:
2. jsunit also supports JS tracking. Tracking methods include:
Warn (message,
[Value])
Inform (message, [value])
Debug (message, [value])
Script Language = "JavaScript"> <br/> function addtwonumbers (value1, value2) {<br/> warn ("this is a warning message "); <br/> warn ("this is a warning message with a value", value1); <br/> return value1 + value2; <br/>}< br/> function testvalidargs () {<br/> Inform ("This Is An inform message "); <br/> assertequals ("2 + 2 is 4", 4, addtwonumbers (2, 2); <br/>}< br/> function testwithnegativenumbers () {<br/> debug ("this is a debug message"); <br/> assertequals ("negative numbers:-2 +-2 is-4",-4, <br/> addtwonumbers (-2,-2); <br/>}< br/> </SCRIPT>