* 输入参数覆盖
* 正常逻辑覆盖
* 异常逻辑覆盖
* 保证代码规范
* 查漏补缺
* 提高代码质量
* 把控程序的功能点
* 学习别人代码的优点
代码质量如何度量?
如果没有测试你如何保证你的代码质量?
单元测试是否也能让产品经理看得懂?
单元测试是否也能成功一个产品需求的Case?
你有足够信心在没有单元测试的情况下发布你的重构代码吗?
如何检测你重构的代码符合需要?
全是绿灯!
单元测试全部跑通!
去QA
NAME = taobaoindex
SRC = $(shell find lib -type f -name "*.js")
TESTS = test/*.js
TESTTIMEOUT = 15000
REPORTER = tap
VERSION = $(shell date +%Y%m%d%H%M%S)
SVNURL = $(shell svn info | grep URL | awk '{print $$2}')
test: compile
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
--reporter $(REPORTER) --timeout $(TESTTIMEOUT) $(TESTS)
test-cov:
@JSCOV=1 $(MAKE) test REPORTER=html-cov > coverage.html
test-cov-json:
@JSCOV=1 $(MAKE) test REPORTER=json-cov
......
var searchApp = require('../controllers/search');
var app = require('../app').create();
var should = require('should');
before(function(done) {
app.listen(0, done);
});
describe('/cpv_items', function() {
var urls = [
'/cpv_items/50010850/20511,28386;20664,28105;2917619,8496965?query=%E8%BF%9E%E8%A1%A3%E8%A3%99&cid=50010850&hot=63426&title=%E8%95%BE%E4%B8%9D%20%E8%95%BE%E4%B8%9D%E8%8A%B1%E8%BE%B9%20%E9%9F%A9%E7%89%88&datatype=cpv',
'/cpv_items/50019272/20000,47342;3775809,20144;7221242,4526599?query=%E7%99%BB%E5%B1%B1%E9%9E%8B&cid=50019272&hot=13451&title=%E7%94%B7%E5%A3%AB%20Camel%2F%E9%AA%86%E9%A9%BC%20%E8%80%90%E7%A3%A8&datatype=cpv',
];
urls.forEach(function(url) {
it(url.substring(0, 30) + '... should return json format', function(done) {
app.request().get(url).end(function(res) {
res.should.have.status(200);
res.should.have.header('content-type', 'application/json; charset=utf-8');
var items = JSON.parse(res.body);
items.should.be.an.instanceof(Array);
items.length.should.above(0);
for (var j = 0, jl = items.length; j < jl; j++) {
checkImageItem(items[j]);
}
done();
});
})
});
});
......
function mockDataRenderError() {
var _request = urllib.request;
before(function() {
urllib.request = function() {
var cb = arguments[arguments.length - 1];
process.nextTick(function() {
cb(null, new Buffer('{"v": "3.0","c": 200,"m": "数据渲染错误","t": 0,"n": 0,"fn": 0}'));
});
};
});
after(function() {
urllib.request = _request;
});
return function(err, data, done) {
should.exist(err);
err.should.be.an.instanceof(Error);
err.message.should.equal('数据渲染错误');
err.data.should.equal('{"v": "3.0","c": 200,"m": "数据渲染错误","t": 0,"n": 0,"fn": 0}');
should.not.exist(data);
done();
};
}
......
看见单元测试全部跑过是件很爽的事情
/
#