第二章.测试和debug(2.Testing and debugging)[完成]

翻译 Secrets of the JavaScript Ninja (JavaScript忍者禁术)

第二章 测试和debug(Testing and debugging)

本章重点:
1.测试工具

2.测试技术

3.构建一个测试框架

4.如何测试异步代码

目录链接:http://yannhe.com/secrets-of-the-javascript-ninja-javascript

本文链接:http://yannhe.com/2-testing-and-debugging

由于我们在之后的示例中用到的测试工具很少,几乎就是在用assert函数,所以本章不做详细的翻译了。

首先将assert函数给出,之后的章节用的的assert函数都是在调用此段代码,用的时候请引用下面的assert.js:

(function()  
{  
    document.write("<style>#results li.pass { color: green;}#results li.fail { color: red;}</style>");  
    document.write("<ul id='results'></ul>");  
    var results;  
    this.assert = function assert(value, desc)  
    {  
        results = results || document.getElementById("results");  
        var li = document.createElement('li');  
        li.className = value ? "pass" : "fail";  
        li.appendChild(document.createTextNode(desc));  
        results.appendChild(li);  
        if (!value)  
        {  
            li.parentNode.parentNode.className = "fail";  
        }  
        return li;  
    };  
    this.test = function test(name, fn)  
    {  
        results = document.getElementById("results");  
        results = assert(true, name).appendChild(document.createElement("ul"));  
        fn();  
    }  
})()  

2.1 debugging code #

一般菜鸟在debug的时候都是用alert(),现在我们看看在不同的浏览器中自带的高级点的工具:

.Firebug:请看http://getfirebug.org

.IE Developer Tools: 在IE8和9和10中可用

.Opera Dragonfly: Opera 9.5或更新可用

.WebKit Developer Tools: Safari 3 ,4, Chrome(推荐这个,我就是用这个)

2.1.1 Logging #

一般用console.log()

2.1.2 断点(Breakpoints) #

2.3 测试框架 #

2.3.1 QUnit(http://docs.jquery.com/Qunit) #

2.3.2 YUITest(http://developer.yahoo.com/yui/3/test/) #

2.3.3 JSUnit(http://www.jsunit.net) #

(转载本文章请注明作者和出处 Yann (http://yannhe.com),请勿用于任何商业用途)

 
56
Kudos
 
56
Kudos

Now read this

人生算法的python代码实现

#人生算法.py #宇宙基础能力,即,无常 import random #参数context来自宇宙context,即宇宙的所有数据,维持着宇宙每时每刻的状态(state), #总的来说,数据(context)是状态(state)的宿主,算法(human_life)是可以转换状态(state)的一种能力,而这个世界上只有一个状态state,即,the one def human_life(context): #植物神经系统,爬行脑算法,后台运行(异步模式) async... Continue →