使用 Node.js 做 Function Test实现方法
2013-10-25 00:00
801 查看
Info
上周 meeting 上同事说他们现在在用 java 写 function test,产生了很多冗余的代码,整个项目也变得比较臃肿。现在迫切需要个简单的模板项目能快速搭建function test。
后来我回去想了想,为什么我们非得用 java 来做 function test 呢?
Node.js 应该是个不错的选择,并且对 json 有着天然的支持,于是回去在 github 上随手一搜,还果真有相关的项目:testosterone,于是便有了这篇blog.
Server
要做demo,自然要有相应的server来支撑。
在这里我们选用Express作为server。
首先我们建立一个server的文件夹,新建package.json。
接下来run command
这样express就装上了。
我们实现几个简单的 get post 方法来做实验
testosterone
server 架设完毕,自然要开始做测试了。
这个 project 的接口的命名都挺优雅,直接上代码。
首先是测试基本的功能
然后针对上面模拟的user的get post 做简单的测试。
接下来,如果你想要针对每个test case 用 give when then 来描述的话,可以这样:
Conclusion
通过以上的代码,可以看出,同java 冗长的 http 头设置等,testosterone确实简单和优雅了不少。
上周 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
npm install
这样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确实简单和优雅了不少。
相关文章推荐
- ubuntu安装iNode连网认证软件遇到的问题
- Node.js log3:create ejs engine and jade engine
- soket.io.js + angular.js + express.js(node.js)
- Hadoop源码分析之_NameNode regular过程分析2
- nodejs中使用velocity
- leetcode:Remove Nth Node From End of List (移除从尾部数的第N个节点)
- soket.io.js + angular.js + express.js(node.js)
- hadoop2.2.0安装中遇到的错误:mapreduce.shuffle set in yarn.nodemanager.aux-services is invalid
- domain: node.js的异常捕获利器
- Node.js log2: ERR when execute command >npm install
- nodejs入门级基础(数据类型,最基本的语法详解)
- nodejs中的==、===、!=、!==的区别
- hadoop-2.2.0 NameNode启动源码注释
- leetcode - Swap Nodes in Pairs
- MonoDevelop几个常用的快捷键
- [LeetCode] Populating Next Right Pointers in Each Node
- datanode启动不了
- YARN/MRv2 Node Manager深入剖析—节点健康状况检测
- 【second】Populating Next Right Pointers in Each Node II
- Websphere8.5通过addNode命名往集群添加节点时出现ADMU0036E异常解决