Qunit Simple Tutorial

Source: Internet
Author: User
Tags define

What is Qunit?

Qunit is a very simple and powerful unit testing framework, simple enough to know a little bit of JavaScript, less than 5 minutes, the following is the use of this framework:

First of all, since it is a framework, there must be a common feature of the framework (frame css->qunit.css, frame JS function library->qunit.js, frame HTML Display page-> Framework.html, we put them where we should be, as shown in the following illustration:

Since it's a JavaScript test framework, there are definitely two parts, one is the JavaScript function being tested, the other is the JavaScript function of the test statement, we build 2 folders (Needtestjs and Tcjs to place) respectively.

Part 1: The tested JavaScript function:

We've written two functions here, and as Qunit suggests, we define a test module (modularity) to test whether a parameter is a number, and one to test whether the incoming number is even:

Define the Test modules module 
("test Example"); 
Defines a simple function that determines whether the parameter is a numeric 
function  Isnumber (para) { 
    if (typeof para== "number") {return 
        true; 
    } else{return 
        false; 
    } 
     
This function is used to determine if the number passed in is not even 
function IsEven (val) {return 
    val% 2 = 0; 
}

Part 2: Test Cases:

Test cases are also JavaScript functions, we have to construct these test cases with test assertions, there are a number of test assertions, the most commonly used are: OK (), Equals (), same (), and specifically where they see my Code comment section:

The first parameter of the start unit test 
     
//test is the name of the test set that is displayed on the page, and you can specify any meaningful name 
test (' isnumber () ', function () { 
    //Enumerate all possible situations, The logical//ok that each condition should conform to 
         
    is the most common function to judge in Qunit, but only true and false 
    //correct, then the green note, the error will explode red 
    OK (isnumber (2), "2 is a number"); 
    Ok (!isnumber ("2"), "String 2 is not a number"); 
    Ok (Isnumber (Nan), "Nan is a number"); 
         
     
         
} 
; 
     
     
     
Test ("IsEven ()", function () { 
         
     
    //equals (Actual,expected,[message]) is used for equivalent judgment function 
    equals (True, IsEven (2), ' 2 is even "); 
    Equals (False,iseven (3), ' 3 is not an even number '); 
} 
) 
     
     
; Test ("Identity assertion", function () { 
         
    //same () is an identity assertion that can be used to determine whether 2 objects are equal (can be different) 
    same ({}, {}, ' passes, objects have the same Content '); 
    Same ({a:1}, {a:50}, ' passes '); 
    Same ([], [], ' passes, arrays have the same content '); 
    Same ([1], [1], ' passes '); 
} 
);

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

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.