首页 web前端 js教程 使用 Node.js 做 Function Test实现方法_javascript技巧

使用 Node.js 做 Function Test实现方法_javascript技巧

May 16, 2016 pm 05:18 PM
node.js

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

复制代码 代码如下:

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确实简单和优雅了不少。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

一文聊聊Node中的内存控制 一文聊聊Node中的内存控制 Apr 26, 2023 pm 05:37 PM

一文聊聊Node中的内存控制

图文详解Node V8引擎的内存和GC 图文详解Node V8引擎的内存和GC Mar 29, 2023 pm 06:02 PM

图文详解Node V8引擎的内存和GC

Node.js 19正式发布,聊聊它的 6 大特性! Node.js 19正式发布,聊聊它的 6 大特性! Nov 16, 2022 pm 08:34 PM

Node.js 19正式发布,聊聊它的 6 大特性!

深入聊聊Node中的File模块 深入聊聊Node中的File模块 Apr 24, 2023 pm 05:49 PM

深入聊聊Node中的File模块

node无法用npm命令怎么办 node无法用npm命令怎么办 Feb 08, 2023 am 10:09 AM

node无法用npm命令怎么办

聊聊如何选择一个最好的Node.js Docker镜像? 聊聊如何选择一个最好的Node.js Docker镜像? Dec 13, 2022 pm 08:00 PM

聊聊如何选择一个最好的Node.js Docker镜像?

聊聊Node.js中的 GC (垃圾回收)机制 聊聊Node.js中的 GC (垃圾回收)机制 Nov 29, 2022 pm 08:44 PM

聊聊Node.js中的 GC (垃圾回收)机制

一起聊聊Node中的事件循环 一起聊聊Node中的事件循环 Apr 11, 2023 pm 07:08 PM

一起聊聊Node中的事件循环

See all articles