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. 【Python备忘】python判断文件和文件夹是否存在

    python判断文件和文件夹是否存在 import os os.path.isfile('test.txt') #如果不存在就返回False os.path.exists(directory) #如果 ...

  2. ACboy needs your help again!--hdu1702

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. Learn LIBSVM---a practical Guide to SVM classification

    想学习一下SVM,所以找到了LIBSVM--A Library for Support Vector Machines,首先阅读了一下网站提供的A practical guide to SVM cla ...

  4. C# 迪杰斯特拉(Dijkstra)算法

    Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 其基本思想是,设置顶点集合S并不断地作 ...

  5. Java常见序列化与反序列方法总结

    很多商业项目用到数据库.内存映射文件和普通文件来完成项目中的序列化处理的需求,但是这些方法很少会依靠于Java序列化.本文也不是用来解释序列化的,而是一起来看看面试中有关序列化的问题,这些问题你很有可 ...

  6. HTML5 Audio时代的MIDI音乐文件播放

    大家都知道,HTML5 Audio标签能够支持wav, webm, mp3, ogg, acc等格式,但是有个很重要的音乐文件格式midi(扩展名mid)却在各大浏览器中都没有内置的支持,因为mid文 ...

  7. Win7 64 bit 激活工具

    下载地址:http://www.3987.com/xiazai/1/18/35487.html 一键运行,重启就激活了,简洁方便!

  8. 使用virtualenv隔离python环境

    使用virtualenv隔离python环境 | 爱积累爱分享 使用virtualenv隔离python环境 iitshare 分类:Python | 标签:pythonenv, virtualenv ...

  9. #include <queue>

    双端队列deque比向量vector更有优势 双端队列(deque) 连续存储的指向不同元素的指针所组成的数组<deque> 队列(queue) 先进先出的值的排列 <queue&g ...

  10. poj 3253 Fence Repair(模拟huffman树 + 优先队列)

    题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...