Info
上周 meeting 上同事说他们现在在用 java 写 function test,产生了很多冗余的代码,整个项目也变得比较臃肿 。现在迫切需要个简单的模板项目能快速搭建function test 。
后来我回去想了想,为什么我们非得用 java 来做 function test 呢?
Node.js 应该是个不错的选择,并且对 json 有着天然的支持,于是回去在 github 上随手一搜,还果真有相关的项目:testosterone,于是便有了这篇blog.
Server
要做demo,自然要有相应的server来支撑 。
在这里我们选用Express作为server 。
首先我们建立一个server的文件夹,新建package.json 。
复制代码 代码如下:
{
"name": "wine-cellar",
"description": "Wine Cellar Application",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x"
}
}
接下来run command
复制代码 代码如下:
这样express就装上了 。
我们实现几个简单的 get post 方法来做实验
复制代码 代码如下:
var express = require(express)
, app = express();
app.use(express.bodyParser());
app.get(/hello, function(req, res) {
res.send("hello world");
});
app.get(/, function (req, res) {
setTimeout(function () {
res.writeHead(200, {Content-Type: text/plain});
res.end();
}, 200);
});
app.get(/hi, function (req, res) {
if (req.param(hello) !== undefined) {
res.writeHead(200, {Content-Type: text/plain});
res.end(Hello!);
} else {
res.writeHead(500, {Content-Type: text/plain});
res.end(use post instead);
}
});
app.post(/hi, function (req, res) {
setTimeout(function () {
res.writeHead(200, {Content-Type: text/plain});
res.end(req.param(message) || message);
}, 100);
});
app.get(/user, function(req, res) {
res.send(
[
{name:jack},
{name:tom}
]
);
});
app.get(/user/:id, function(req, res) {
res.send({
id: 1,
name: "node js",
description: "I am node js"
});
});
app.post(/user/edit, function (req, res) {
setTimeout(function () {
res.send({
id:req.param(id),
status:1
});
}, 100);
});
app.listen(3000);
console.log(Listening on port 3000...);
testosterone
server 架设完毕,自然要开始做测试了 。
这个 project 的接口的命名都挺优雅,直接上代码 。
首先是测试基本的功能
复制代码 代码如下:
var testosterone = require(testosterone)({port: 3000})
, assert = testosterone.assert;
testosterone
.get(/hello,function(res){
assert.equal(res.statusCode, 200);
})
.get(/hi,function(res){
assert.equal(res.statusCode, 500);
})
.post(/hi, {data: {message: hola}}, {
status: 200
,body: hola
});
然后针对上面模拟的user的get post 做简单的测试 。
复制代码 代码如下:
var testosterone = require(testosterone)({port: 3000})
, assert = testosterone.assert;
testosterone
.get(/user, function (res) {
var expectRes = [
{name:jack},
{name:tom}
];
assert.equal(res.statusCode, 200);
assert.equal(JSON.stringify(JSON.parse(res.body)),JSON.stringify(expectRes));
})
.get(/user/1, function (res) {
var user = JSON.parse(res.body);
assert.equal(res.statusCode, 200);
assert.equal(user.name, "node js");
assert.equal(user.description,"I am node js");
})
接下来,如果你想要针对每个test case 用 give when then 来描述的话,可以这样:
复制代码 代码如下:
var testosterone = require(testosterone)({port: 3000, title: test user api})
, add = testosterone.add
, assert = testosterone.assert;
testosterone
.add(
GIVEN a user id to /user/{id} \n +
WHEN it have response user \n +
THEN it should return user json,
function (cb) {
testosterone.get(/user/1, cb(function (res) {
var expectRes = {
id: 1,
name: "node js",
description: "I am node js"
};
assert.equal(res.statusCode, 200);
assert.equal(JSON.stringify(JSON.parse(res.body)), JSON.stringify(expectRes));
}));
})
.add(
GIVEN a POST a user info to /user/edit \n +
WHEN find modify success \n +
THEN it should resturn status 1,
function (cb) {
testosterone.post(/user/edit, {data: {id: 1, name: "change name"}}, cb(function (res) {
var res = JSON.parse(res.body);
assert.equal(res.status, 1);
}));
}
)
.run(function () {
require(sys).print(done!);
});
Conclusion
通过以上的代码,可以看出,同java 冗长的 http 头设置等,testosterone确实简单和优雅了不少 。