Ajax Basics Tutorial (6)-6.2 analysis Jsunit

Source: Internet
Author: User
Tags assert zip xunit


6.2 Analysis Jsunit



At the beginning of 2001, Edward Hieatt started "porting" JUnit to test JavaScript in a browser. Since then, Jsunit has been downloading nearly 10 000 times and about 300 people have joined Jsunit's newsgroups. Jsunit supports general Xunit functionality, written entirely in JavaScript, and if you're accustomed to using JUnit or a similar xunit framework, you'll find jsunit pretty straightforward to use.



Jsunit also has a number of different places: there are setup () and teardown (), but now as a function rather than a method; the test function (not the test method) is divided into multiple test pages (not test cases) In addition, Jsunit provides its own html-based test-run tool. Table 6-1 compares the two frameworks.



Table 6-1 Comparison of JUnit and Jsunit





Junit JsUnit
Test class Extension TestCase The test page contains Jsunitcore.js
Test method Test function
Test class HTML-based test page
Testsuites An HTML-based test set
Multiple test run Tools Html/javascript-based test run tool
The SetUp () and teardown () methods The SetUp () and teardown () functions
Running in virtual machines Running in the browser
Writing in Java Writing in JavaScript





6.2.1 Start



For Jsunit, the start is simple, just download the jsunit zip file from the Jsunit Web site (www.edwardh.com/jsunit/). By unlocking this compressed file, you'll get a Jsunit folder where you can put the Web server here so that the entire team or the entire organization can use jsunit more easily. Most of the Jsunit's "cores" are in the Jsunit/app directory, where Jsunitcore.js, Jsunittracer.js, and jsunittestmanager.js are visible, as well as some other files. If you want to run specific jsunit tests, you can use testrunner.html to run any test pages found in the Jsunit/tests directory. If you are using IntelliJ, and want to use the Jsunit,jsunit/intellij directory specifically to contain all the appropriate files you need.



6.2.2 Writing Tests



Writing tests with Jsunit is similar to writing tests with JUnit. The test function cannot have any arguments and must have a prefix test, such as Testdatevalidation (). The test function is contained in a test page, which is similar to a test class in JUnit. The test page must contain the Jsunitcore.js file, and after the Jsunit zip file is unpacked, the file will be found in the Jsunit/app directory. The inclusion of this JavaScript file is actually adding an external JavaScript file to the page; just use scripting elements <script language= "JavaScript" src= "Jsunitcore.js" > </script> to refer to this file, remember that if your current directory is not a Jsunit/app directory, you will also need to provide relevant path information for the Jsunitcore.js file. Of course, you can include any number of other functions or JavaScript in a test page, and in fact, it's a good practice to put multiple JavaScript functions in separate files. The test function can also be placed in a separate JavaScript file, but if you do so, you need to use the Exposetestfunctionnames () method so that Jsunit can find the test function. In fact, if you need to set up a test for different page content, you can put the test function in a separate file, which avoids the pain of the copy-paste problem.



Generally, Jsunit automatically discovers test functions, just as JUnit finds all test methods. However, some operating systems/browsers cannot collaborate. If you find that you cannot find the test function as you wish, using the Exposetestfunctionnames () method can solve the problem.



Assertion method



Now you have a certain understanding of the test function and the test page, the following need to write some practical tests! As with JUnit, you can use the Assert method. The assertion method is the basic module of any unit test, and they are simply Boolean expressions that indicate whether a given statement is true or false. When an assertion fails, an error occurs, which is known as the Red Bar. Unlike JUnit, Jsunit does not offer a rich assertion method, but it is enough to test JavaScript code. Note that, in addition to the annotations of the Fail () method, comments for the other assertion methods are optional (this is similar to JUnit, and even "incorrectly" puts the optional arguments at the front, not the last).



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)





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.