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. FFMPEG图片转视频

    1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpeg -i input_file -acod ...

  2. *1022. D进制的A+B【考前最后一道题】

      /* *Main.c *1022. D进制的A+B Ver.1 *Created on : 2014.9.5 *****测试通过****** */ #include <stdio.h> ...

  3. linux case 语句

    #!/bin/bash #$ 表示脚本名 #$n 表示第n个参数(n>) in ") echo '--=> A' ;; ") echo '--=> B' ;; * ...

  4. MFC Attach()函数和Detach()函数

    一.Windows对象和MFC对象的区别?MFC对象实际上并没有把整个Windows对象都包装在其中.对于窗口:MFC对象它只是有一个窗口句柄而已,这个窗口句柄如果指向一个实际存在的窗口对象(窗口对象 ...

  5. UML_组件图

    简介 众所周知,组件图是用来描述系统中的各组件之间的关系.首先我们必须知道组件的定义是什么,然后组件之间有哪些关系.理清楚这些,我们在以后的设计中才能 派上用场.UML语言对组件的定义已发生了巨大变化 ...

  6. 查看实时公网ip

    icanhazip.com 使您在任何地方知道你的公网IP地址 icanhazip.com 使你在任何地方知道你的公网IP地址 icanhazip.com 使你在任何地方知道你的公网IP地址 ican ...

  7. 代码管理git总结

    1. http://blog.csdn.net/teresa502/article/details/7388834 pwd 当前工作目录 cd(不加参数) 进root cd(folder) 进入文件夹 ...

  8. 实际用户ID,有效用户ID和设置用户ID

    摘自http://blog.csdn.net/guosha/article/details/2679334 实际用户ID,有效用户ID和设置用户ID 看UNIX相关的书时经常能遇到这几个概念,但一直没 ...

  9. EMCA创建em资料库时报错

    说明:监听程序配置及启动正常.实例运行正常,远程或是本地sqlplus连接实例也正常.主机名显示正常,与主机名相关三个配置文件也没问题. emca报错: [oracle@rusky-oracle11g ...

  10. MatLab实现FFT与功率谱

    FFT和功率谱估计 用Fourier变换求取信号的功率谱---周期图法 clf; Fs=1000; N=256;Nfft=256;%数据的长度和FFT所用的数据长度 n=0:N-1;t=n/Fs;%采 ...