Info

上周 meeting 上同事说他们现在在用 java 写 function test,产生了很多冗余的代码,整个项目也变得比较臃肿。现在迫切需要个简单的模板项目能快速搭建function test。

后来我回去想了想,为什么我们非得用 java 来做 function test 呢?

Node.js 应该是个不错的选择,并且对 json 有着天然的支持,于是回去在 github 上随手一搜,还果真有相关的项目:testosterone,于是便有了这篇blog.

Server

要做demo,自然要有相应的server来支撑。

在这里我们选用Express作为server。

首先我们建立一个server的文件夹,新建package.json。

1
2
3
4
5
6
7
8
9
{
"name": "wine-cellar",
"description": "Wine Cellar Application",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x"
}
}

接下来run command

1
npm install

这样express就装上了。

我们实现几个简单的 get post 方法来做实验

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 的接口的命名都挺优雅,直接上代码。

首先是测试基本的功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 做简单的测试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 来描述的话,可以这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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确实简单和优雅了不少。

testosterone 是个蛮不错的项目,作者写的文档简单易懂,在 test 文件夹里面也有很多测试代码。

本篇 blog 的 sample code 的放在 https://github.com/nateriver520/function-test-demo

使用 Node.js 做 Function Test的更多相关文章

  1. JS一般般的网页重构可以使用Node.js做些什么(转)

    一.非计算机背景前端如何快速了解Node.js? 做前端的应该都听过Node.js,偏开发背景的童鞋应该都玩过. 对于一些没有计算机背景的,工作内容以静态页面呈现为主的前端,可能并未把玩过Node.j ...

  2. Node.js学习 - Function

    Node.js函数和JavaScript类似 function say(word) { console.log(word); } function execute(someFunction, valu ...

  3. 初涉node.js做微信测试公众号一路填坑顺便发现个有趣的其他漏洞

    [微信测试公众号] 半年前耍着玩搭起来的“微信简历”,是LAMP版的,很皮毛. 微信的官方文档在这 http://mp.weixin.qq.com/wiki/index.php 1.获取access ...

  4. 用node.js做cluster,监听异常的邮件提醒服务

    __ __ __ _ __ ____ ____ ____/ /__ _____/ /_ _______/ /____ _____ ___ ____ ___ ____ _(_) / / __ \/ __ ...

  5. 使用node.js做一个自用的天气插件

    var request = require('request') var url = 'http://www.baidu.com/home/xman/data/superload' var cooki ...

  6. node.js打印function

    var Person = function(name) { this.name = name; this.gender = ['man', 'woman']; } console.log(Person ...

  7. Node.js做的代理转发服务器

    可以代理苹果ID服务器 const http = require('http'); const https = require('https'); const client = require('ht ...

  8. Node.js配合node-http-proxy解决本地开发ajax跨域问题

    情景: 前后端分离,本地前端开发调用接口会有跨域问题,一般有以下3种解决方法: 1. 后端接口打包到本地运行(缺点:每次后端更新都要去测试服下一个更新包,还要在本地搭建java运行环境,麻烦) 2. ...

  9. Node.js抓取网页

    前几天四六级成绩出来(然而我没考),用Node.js做了一个模拟表单提交并抓取数据的Web 总结一下用到的知识,简单的网页抓取大概就是这个流程了 发送Get或Post请求 表单提交,首先弄到原网页提交 ...

随机推荐

  1. 国内好用的公用DNS 服务器。

    阿里 AliDNS 223.5.5.5 223.6.6.6 CNNIC SDNS 1.2.4.8 210.2.4.8 Google DNS 8.8.8.8 8.8.4.4 OpenDNS 208.67 ...

  2. 1206: B.求和

    题目描述 点击这里 对于正整数n,k,我们定义这样一个函数f,它满足如下规律 现在给出n和k,你的任务就是要计算f(n,k)的值. 输入 首先是一个整数T,表示有T组数据 接下来每组数据是n和k(1& ...

  3. 13个JavaScript图表(JS图表)图形绘制插件

    转自:http://blog.jobbole.com/13671/ 1. Flash 过去是最佳解决方案,但很多人多在从那迁移: 2. 现代浏览器及其更强大的计算能力,使其在转化绘制实时数据方面的能力 ...

  4. linux文件属性

    在Linux中,文件的属性是一个很重要的概念,用户或者用户组对一个文件所拥有的权限,都可以 从文件的属性得知.我们可以通过ls -al命令,列出某个文件夹下面的所有文件(包括以.开头的隐藏 文件).下 ...

  5. CURL常用命令---样例

    原文地址: http://www.thegeekstuff.com/2012/04/curl-examples/ 下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://w ...

  6. Makefile里调用Shell注意点

    http://www.linuxidc.com/Linux/2012-04/59093.htm 大家经常编写和使用Makefile, Makefile里面也经常用到shell, 但对其中一些需要注意的 ...

  7. 窥探 kernel --- 进程调度的目标,nice值,静态优先级,动态优先级,实时优先级

    http://blog.chinaunix.net/uid-24227137-id-3595610.html 窥探 kernel --- 进程调度的目标,nice值,静态优先级,动态优先级,实时优先级 ...

  8. 通过P-SMR看State Machine Replication

    在一个复制系统中,为了保持一致性,各个replicated server是串行运行.这样性能上就会比仅仅有一台server的系统慢,由于仅仅有一台server能够进行并行处理.假设在复制系统中各个se ...

  9. 用户名_密码获取Access_Token

    http://www.ivanjevremovic.in.rs/live/domination/red/index-async-slider.html http://designova.net/rev ...

  10. SOA架构有基本的要求

    SOA在相对较粗的粒度上对应用服务或业务模块进行封装与重用: 服务间保持松散耦合,基于开放的标准, 服务的接口描述与具体实现无关: 灵活的架构 -服务的实现细节,服务的位置乃至服务请求的底层协议都应该 ...