JavaScript Unit Test Framework Qunitjs details _javascript Tips

Source: Internet
Author: User

One, what is Qunit

Qunit (http://qunitjs.com/) is a very powerful JavaScript unit test framework that can help you debug your code. It was written by a member of the jquery team and is the official jquery test suite. But Qunit is generally enough to test any regular JavaScript code, and it might even test server-side JavaScript with some JavaScript engines such as Rhino or V8.
If you are unfamiliar with the concept of unit testing, please don't worry. It's not hard to understand:

Copy Code code as follows:

In computer programming, unit testing (also known as module testing) is a test work for correctness testing of program modules (the smallest unit of software design). The program unit is the smallest testable part of the application. In procedural programming, a unit is a single program, function, process, etc. for object-oriented programming, the smallest unit is the method, including the method in the base class (superclass), abstract class, or derived class (subclass). -Quote from Wikipedia.


Simply put, you write tests for each feature of your code, and if all of these tests are passed, then you can be sure that the code has no flaws (usually, or how thoroughly your test will be).


Second, why do you want to test your code

If you've never written any unit tests before, you might be able to put your code directly on the site, click a minute to see if there are any problems, and try to solve the problems you find, and there are a lot of problems with this approach.
First of all, this is very boring. Clicking is actually not an easy job because you have to make sure everything is clicked and it's possible you missed one or two.

Second, everything you do for the test is not reusable, which means it is difficult to return. What is regression? Imagine you've written some code and tested it, fixed all the bugs you found, and then released it. At this point, a user sends some feedback about the new defect and needs some new features. You go back into the code, fix these new bugs, and add new features. What's likely to happen next is that some of the old bugs reappear, which is called "Regression." Look, now you have to click again, and there's a chance you won't find the old ones, and even if you do, it will take a while to figure out that your problem is caused by regression. With unit tests, you write tests to find bugs, and once the code is modified, you filter through the test again. If regression occurs, some tests will fail, and you can easily recognize them and know which part of the code contains the error. Now that you know what you have just changed, you can easily solve it.

Another advantage of unit testing, especially for Web development: It makes Cross-browser compatibility testing easy. Just run your test case in a different browser, if there is a problem with a browser, you fix it and rerun the test cases to make sure that you don't get back in another browser, and once you've all passed the test, you can be sure that all the target browsers support it.

I would like to mention a John Resig project: Testswarm (http://testswarm.com/). It brings Javascript unit tests to a new level by distributing them, which is a Web site that contains a lot of test cases where anyone can run some test cases and return the results to the server. In this way, the code will be very quick to test in different browsers, or even different platforms to run.

Iii. How to write unit tests with Qunit

So, how do you correctly write unit tests with Qunit? First, you need to set up a test environment:

Copy Code code as follows:

<! DOCTYPE html>
<title>qunit Test suite</title>
<link rel= "stylesheet" href= "Http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type= "Text/css" Screen ">
<script type= "Text/javascript" src= "Http://github.com/jquery/qunit/raw/master/qunit/qunit.js" ></script >
<!--Your project file goes here-->
<script type= "Text/javascript" src= "Myproject.js" ></script>
<!--Your tests file goes here-->
<script type= "Text/javascript" src= "Mytests.js" ></script>
<body>
&LT;H1 id= "Qunit-header" >qunit Test suite&LT;H2 id= "Qunit-banner" ><div id= "Qunit-testrunner-toolbar" ></div>
&LT;H2 id= "Qunit-useragent" ><ol id= "Qunit-tests" ></ol>
</body>

As you can see, a managed version of the Qunit framework is used here.
The code that will be tested has been added to the myproject.js, and your test should be inserted into mytest.js. To run these tests, you simply open the HTML file in a browser. Now it's time to write some tests.
The cornerstone of unit testing is the assertion:

An assertion is a proposition that predicts the return result of your code. If the prediction is false and the assertion fails, you know that there is a problem.


To run assertions, you should put them into the test case:

Copy Code code as follows:

Let ' s test this function
Function IsEven (val) {
Return val% 2 = 0;
}

Test (' IsEven () ', function () {
Ok (IsEven (0), ' Zero is a even number ');
Ok (IsEven (2), ' So is two ');
Ok (IsEven ( -4), ' so is negative four ');
Ok (!iseven (1), ' One is not ' even number ');
Ok (!iseven ( -7), ' Neither is negative seven ');
})


Here we define a function: IsEven, which detects whether a number is odd, and we want to test the function to confirm that it does not return the wrong answer.
We first call test (), which builds a test case; The first parameter is a string that will be displayed in the result, and the second parameter is a callback function that includes our main fault.
We wrote 5 assertions, all of which are Boolean. A Boolean assertion that expects its first argument to be true. The second argument remains the message to be displayed in the result.
Here's what you want to get, as long as you run the test:



Four, in-depth study reference

The above is a simple introduction to qunit.js, and there are a lot of assertion methods, specific reference API documentation:
http://api.qunitjs.com/
Unit Testing is a great way to test your code before you post your code. If you haven't written any unit tests before, it's time to start!

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.