正文
使用 Node.js 做 Function Test
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
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 | { |
接下来run command
1 | npm install |
这样express就装上了。
我们实现几个简单的 get post 方法来做实验
1 | var express = require('express') |
testosterone
server 架设完毕,自然要开始做测试了。
这个 project 的接口的命名都挺优雅,直接上代码。
首先是测试基本的功能
1 | var testosterone = require('testosterone')({port: 3000}) |
然后针对上面模拟的user的get post 做简单的测试。
1 | var testosterone = require('testosterone')({port: 3000}) |
接下来,如果你想要针对每个test case 用 give when then 来描述的话,可以这样:
1 | var testosterone = require('testosterone')({port: 3000, title: 'test user api'}) |
Conclusion
通过以上的代码,可以看出,同java 冗长的 http 头设置等,testosterone确实简单和优雅了不少。
testosterone 是个蛮不错的项目,作者写的文档简单易懂,在 test 文件夹里面也有很多测试代码。
本篇 blog 的 sample code 的放在 https://github.com/nateriver520/function-test-demo