Use qunit to perform unit tests on JavaScript scripts

Source: Internet
Author: User

Javascript debugging is a headache for every Web Developer. Although Firefox plug-in firebug solves many problems, when JavaScript files become larger and larger, managing JavaScript becomes increasingly tricky.

We all know that there is a very useful tool for unit testing of c #-nunit. Is there a similar tool for JavaScript?CodePerform unit tests? The answer is yes. It is qunit.

 

The following describes the procedure of qunit using a simple example:

 

Step 2. Add a header reference to the HTML file

 

< Link REL = "Stylesheet" Href = "CSS/qunit.css" Type = "Text/CSS" Media = "Screen"   />
< Script Type = "Text/JavaScript" SRC = "JS/jquery-1.6.2.js" > </ Script >
< Script Type = "Text/JavaScript" SRC = "JS/qunit. js" > </ Script >

 

 

 

Step 2. Compile HTML code

1 < H1 ID = "Qunit-header" > First Qunit example </ H1 >

2 < H2 ID = "Qunit-Banner" > </ H2 >
3 < Div ID = "Qunit-testrunner-toolbar" > </ Div >
4 < H2 ID = "Qunit-useragent" > </ H2 >
5 < Ol ID = "Qunit-tests" > </ Ol >
6 < Div ID = "Qunit-fixture" > Test markup, will be hidden </ Div >

 

Step 2. Compile JavaScript code

1 <SCRIPT>
2 $ (Document). Ready ( Function (){
3
4 Test ("A basic test example ", Function (){
5 OK ( True , "This test is fine ");
6 VaR Value = "hello ";
7 Equal (value, "hello", "We recommend CT value to be hello ");
8 });
9
10 Module ("module ");
11
12 Test ("first test within module ", Function (){
13 OK ( True , "All pass ");
14 });
15
16 Test ("second test within module ", Function (){
17 OK ( True , "All pass ");
18 });
19
20 Module ("Module B ");
21
22 Test ("Some other test ", Function (){
23 Secondary CT (2 );
24 Equal ( True , False , "Failing test ");
25 Equal ( True , True , "Passing test ");
26 });
27
28 });

29</SCRIPT>


Step 2. Click to run the HTML file to view the test result.

 

 

Obviously, there is a problem with the first method of the 4th modules. The expected return value is false, but true is returned.

 

Finally, the unit test for JavaScript code is very similar to the unit test for C # code. Using the jquery plug-in qunit for unit test saves a lot of time for script debugging.

See the official website for reference: http://docs.jquery.com/QUnit

In addition, I used a large number of qunit testing methods for the development of the website, greatly improving the development efficiency.

 

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.