今天写了点 Javascript 的小东西,想做个测试,google了一下,看到 jquery 团队使用的测试工具 qunit (http://docs.jquery.com/QUnit) 。有一篇中文文章 http://www.cnblogs.com/nuaalfm/archive/2010/02/26/1674235.html 介绍了这个工具,我尝试了一下,文中的示例已经略有陈旧,跟最新的 qunit 有点不兼容,我稍微改动了一下。
代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="http://localhost/static/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://localhost/static/qunit.js" ></script>
<script>
$(document).ready(function() {
test("一个基本测试例子", function() {
ok( true, "测试布尔类型" );
var value = "hello";
QUnit.equal("hello", value, "我们期望得到hello" );
});
test("a basic test example", function() {
ok(true, "this test is fine");
var value = "hello";
QUnit.equal("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);
QUnit.equal(true, false, "failing test");
QUnit.equal(true, true, "passing test");
});
});
</script>
</head>
<body>
<h1 id="qunit-header">Levenshtein Ditstance QUnit</h1>
此页面需要 qunit 支持
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
因为我用 MAC ,自带了一个web server,所以将依赖的 qunit.css 和 qunit.js 放在了http://localhost/static,windows用户可以使用 iis 。另外代码中使用了 JQuery 官网的 jquery-last.js ,所以需要联网。具体的介绍可以看原作者的文章,很容易上手