什麼是單元測試?
單元測試又稱為模組測試,是針對程式模組(軟體設計的最小單位)來進行正確性檢驗的測試工作。單元測試主要是用來檢驗程式的內部邏輯,也稱為個體測試、結構測試或邏輯驅動測試。通常由撰寫程式碼的程式設計師負責進行。
通常來說,程式設計師每修改一次程式就會進行最少一次單元測試,在編寫程式的過程中前後很可能要進行多次單元測試,以證實程式達到軟件規格書(en:Specification)要求的工作目標,沒有臭蟲;雖然單元測試不是什麼必須的,但也不壞,這牽涉到專案管理的政策決定。
—— 維基百科 (中文,英文)
單元測試的優點
1、它是一種驗證行為。
程式中的每一項功能都是測試來驗證它的正確性。它為以後的開發提供支緩。就算是開發後期,我們也可以輕鬆的增加功能或更改程式結構,而不用擔心這個過程中會破壞重要的東西。而且它為代碼的重構提供了保障。這樣,我們就可以更自由的對程式進行改進。
2、它是一種設計行為。
編寫單元測試將使我們從調用者觀察、思考。特別是先寫測試(test-first),迫使我們把程式設計成易於調用和可測試的,即迫使我們解除軟體中的耦合。
3、它是一種編寫文檔的行為。
單元測試是一種無價的文檔,它是展示函數或類如何使用的最佳文檔。這份文檔是可編譯、可啟動並執行,並且它保持最新,永遠與代碼同步。
4、它具有迴歸性。
自動化的單元測試避免了代碼出現迴歸,編寫完成之後,可以隨時隨地的快速運行測試。
參考:
http://miiceic.org.cn/phrase/200602281036115.html
http://tech.ddvip.com/2009-06/1245992965124860.html
http://www.blogjava.net/square/articles/158103.html
javscript中單元測試架構
參考:
http://www.cnblogs.com/kaima/archive/2009/04/09/javascritp_unittest.html
下面我們重點來介紹一下QUnit
QUnit 介紹
JavaScript
依然需要很好的可讀性,所以重構也必不可少,我們知道沒有單元測試的重構是不靠譜的,有好的單元測試覆蓋會讓我們重構時更容易成本也更低,所以對於優秀的javascript編程人員來說非常需要單元測試架構,QUnit
是一款強大而且容易使用的JavaScript 測試架構,它被用於jQuery 與其外掛程式的測試,同時它也同樣可以測試普通的JavaScript 代碼。
使用QUnit
首先我們先要http://docs.jquery.com/QUnit中找到qunit.js 和 qunit.css兩個檔案,Qunit的架構如下:
code<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><script src="http://code.jquery.com/jquery-latest.js"></script><link rel="stylesheet" href="qunit.css" type="text/css" media="screen" /><script type="text/javascript" src="qunit.js" ></script><script>$(document).ready(function() {});</script></head><body><h1 id="qunit-header">QUnit example</h1><h2 id="qunit-banner"></h2><h2 id="qunit-userAgent"></h2><ol id="qunit-tests"></ol></body></html>註:body中的元素id命名必須依照如下形式,否則無法正常顯示,我們可以將要測試的內容放在$(document).ready()中。
我們先來看一個簡單的例子
<!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("一個基本測試例子", function() {
ok( true, "測試布爾類型" );
var value = "hello";
equals( "hello", value, "我們期望得到hello" );
});
});
</script>
</head>
<body>
<h1 id="qunit-header">QUnit example</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
test( name, expected, test ):加入測試去運行,其中可以包含若干斷言函數,比方說ok,equals等。
ok( state, [message] ) 是QUnit中常用的一個判斷函數,可以判斷布爾類型,整型非零為true,字串非“”為true。
equals( actual, expected, [message] ) 是QUnit中常用的一個判等函數
我們再來看一個稍微複雜一點的例子:
code<head> <script src="http://code.jquery.com/jquery-latest.js"></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, "this test is fine"); var value = "hello"; equals("hello", value, "We expect value to be hello"); }); module("Module A"); test("first test within module", function() { ok(true, "all pass"); }); test("second test within module", function() { ok(true, "all pass"); }); module("Module B"); test("some other test", function() { expect(2); equals(true, false, "failing test"); equals(true, true, "passing test"); }); }); </script> </head><body> <h1 id="qunit-header">QUnit example</h1> <h2 id="qunit-banner"></h2> <h2 id="qunit-userAgent"></h2> <ol id="qunit-tests"></ol></body></html>
得到的結果:
module( name, lifecycle ):是用於對測試模組進行分組,[lifecycle] 用於初始化測試和清理測試。
參考如下執行個體:
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() {expect(3);same(this.testData, "foobar");});非同步測試:
codeasyncTest("非同步測試", function() {expect(2);$.getJSON("/Home/JosnDemo", function(result) {equals(result.x, "sss");equals(result.y, "sss");start();});});"/Home/JosnDemo"是提供json資料的地址,這裡需要注意的是必須在寫完斷言函數後調用start()函數。
Qunit還提供了在測試時的一些更為進階的應用,比如希望在某個測試開始時做些工作等,可以參見http://docs.jquery.com/QUnit#Integration_into_Browser_Automation_Tools
JQuery的很多核心套件是使用Qunit來進行測試的,http://docs.jquery.com/QUnit#Reference_Test_Suites中有很多的例子可以供大家參考。
將此檔案存為html直接在本地運行
原文:http://hi.baidu.com/gguozhenqian/blog/item/5c555e0bceb8f91494ca6b01.html