What is unit testing?
Unit Testing, also known as module testing, is a test of the correctness of program modules (the smallest unit of software design. Unit Testing is mainly used to test the internal logic of a program, also known as individual testing, structure testing, or logic-driven testing. Generally, the programmer who writes the code is responsible for this.
Generally, every time a program is modified, a program runs at least one RMB per modification, before and after the authentication program's process, it is likely that you have to perform the billing statement multiple times to verify that the program has passed the authentication specification (EN: Specification) the required job objectives are not smelly; although unit testing is not necessary, it is not bad. This is related to the policy decision of case management.
-- Wikipedia (Chinese and English)
Advantages of Unit Testing
1. It is a verification behavior.
Every function in the program is tested to verify its correctness. It provides a delay for future development. Even after development, we can easily add features or change the program structure without worrying about the damage to important things in this process. It also guarantees code refactoring. In this way, we can improve the program more freely.
2. It is a design behavior.
Writing Unit Tests will let us observe and think from callers. In particular, writing test-first forces us to design programs that are easy to call and testable, which forces us to lift coupling in software.
3. It is a document writing activity.
Unit testing is an invaluable document that best shows how functions or classes are used. This document can be compiled, run, and kept up-to-date, always synchronized with the code.
4. It is regression.
Automated unit tests avoid code regression. After compilation, you can run tests anytime, anywhere.
Refer:
Http://miiceic.org.cn/phrase/200602281036115.html
Http://tech.ddvip.com/2009-06/1245992965124860.html
Http://www.blogjava.net/square/articles/158103.html
Unit test framework in javscript
- Jsunit
The systematic solution is based on xnuit specifications. If you use JUnit, nunit, and other frameworks, this should be easy to use and include the server side (Java ). Http://www.jsunit.net/
Rating: comprehensive and professional, suitable for large-scale enterprise-level development.
- Test. Simple & test. More
This is the father of jquery, John resig.
Test Framework http://openjsan.org/doc/t/th/theory/Test/Simple/0.21/lib/Test/Simple.html recommended in Javascript
.
Rating: it is very easy to use and concise. It is suitable for small and medium-sized projects to quickly introduce unit tests.
- Fireunit
John resig made another job. In his blog, John
Resig-fireunit: javascript unit testing extension, which is released with Jan
Extended fireunit Based on firebug developed by odvarko.In short, fireunit adds a tag panel to firebug and provides some simple JavaScript APIs to record and view tests. For more information, see http://shawphy.com/2008/12/fireunit.html.
Comment: there are traces of test. Simple, John
Resig is a guy who is very good at learning and innovating. Fireunit has outstanding performance in ease of use and is very suitable for front-end engineers who use firebug-based debugging environments.
- Qunit
Qunit is a javascript unit test tool developed by the jquery team. You can download it from http://docs.jquery.com/qunit.Rating: easy to use and beautiful interface.
Refer:
Http://www.cnblogs.com/kaima/archive/2009/04/09/javascritp_unittest.html
Next we will focus on introducing qunit
Qunit Introduction
Javascript
We still need good readability, so refactoring is also essential. We know that refactoring without unit tests is unreliable. A good unit test coverage will make it easier and cheaper to refactor, therefore, for excellent JavaScript programmers, the unit test framework, qunit
Is a powerful and easy-to-use JavaScript testing framework. It is used for testing jquery and its plug-ins, and can also test common JavaScript code.
Use qunit
First, we need to find the qunit.js and qunit.css files in http://docs.jquery.com/qunit. the qunit framework is as follows:
Code <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> Note: The element ID in the body must be named in the following format, otherwise it cannot be displayed normally. We can put the content to be tested in $ (document). Ready.Let's look at a simple example.
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"
"Http://www.w3.org/TR/html4/loose.dtd%22>
<HTML>
<Head>
<SCRIPT src = "http://code.jquery.com/jquery-latest.js%22> </SCRIPT>
<LINK rel = "stylesheet" href = "qunit.css" type = "text/CSS" Media = "screen"/>
<SCRIPT type = "text/JavaScript" src = "qunit. js"> </SCRIPT>
<SCRIPT>
$ (Document). Ready (function (){
Test ("A basic test example", function (){
OK (true, "test boolean type ");
VaR value = "hello ";
Equals ("hello", value, "we expect to get hello ");
});
});
</SCRIPT>
</Head>
<Body>
<H1 id = "qunit-header"> qunit example <H2 id = "qunit-Banner"> </H2>
<H2 id = "qunit-useragent"> </H2>
<Ol id = "qunit-tests"> </OL>
</Body>
</Html>
Test (name, expected, test):Join the test to run, which can contain several asserted functions, such as OK and equals.
OK (State, [Message])It is a common judgment function in qunit. It can be used to determine the boolean type. The non-zero integer type is true, and the non-"" string type is true.
Equals (actual, expected, [Message])It is a commonly used criterion function in qunit.
Let's take a look at a slightly more complex example:
Code
Result:
Module (name, lifecycle): Groups Test Modules. [lifecycle] is used to initialize and clean up tests.
See the following example:
Code module ("module2", {setup: function () {OK (true, "Once extra assert per test"); this. testdata = "foobar" ;}, teardown: function () {OK (true, "and one extra assert after each test ");}}); test ("test with setup and teardown", function () {keep CT (3); same (this. testdata, "foobar ");});Asynchronous test:
Codeasynctest ("Asynchronous test", function () {expect (2); $. getjson ("/home/josndemo", function (result) {equals (result. x, "Sss"); equals (result. y, "Sss"); Start ();});});"/Home/josndemo" is the address that provides JSON data. Note that you must call the START () function after writing the asserted function.
Qunit also provides more advanced applications during testing, such as the desire to do some work at the beginning of a test. See http://docs.jquery.com/QUnit#Integration_into_Browser_Automation_Tools
Many of jquery's core suites use qunit for testing. There are many examples in http://docs.jquery.com/qunit?reference_test_suitesfor your reference.
Save this file as HTML and run it locally
Original article: http://hi.baidu.com/gguozhenqian/blog/item/5c555e0bceb8f91494ca6b01.html