We all know that java has a testing framework called Junit, which can be used to perform unit tests with simple code. Recently, jquery has developed a javascript testing framework called Qunit. The following is an introduction to QUnit on the official jquery website. I hope it will help you.
Introduction
It is very basic to use automated software testing tools in software development. Unit testing is the basic content of Automatic Testing: for unit testing of your function, it is best to write it only once, and everyone who uses your function can run your test, however, there is no need to communicate with each other or do additional work.
Another advantage of testing is that testing can drive the development of software, that is, "test-driven development". First, write a simple test, and then write the implementation code for the test, then add the test content. to implement it, the code written by repeating this process step by step is often better than the code written directly.
Js unit testing is no different from other languages. A testing framework is required to write a real test.
Automated Unit Testing
Problem
Because you have benefited from TDD, you may want to automatically test your application and write your own test program, but it involves a lot of work (including compatibility with different browsers)
Solution
In some js unit testing frameworks, you have decided to choose QUnit. Qunit is a unit testing framework implemented by jquery and is widely used.
Displays test results.
<Script src =/resources/qunit. js> </script> <script> test (a basic test example, function () {var value = hello; equal (value, hello, we recommend CT value to be hello) ;}); </script>
Open in the browser and the result is as follows:
The most important tag in html is
The test result of Qunit in the element is added to this label. The Test method comes from qunit. the js file contains two parameters. The first one is a string, indicating the name of the test, which is displayed in the test result to distinguish the test result, and the last one is a function, function contains the code to be executed for the test, including one or more assertion. Here the equal method is used to display the test result.
If you want to see the effect in your browser, you can download this demo.