为何使用Mountebank
在实施测试的过程中,碰到以下场景,你脑海里第一时间是不是想到找开发、找开发、找开发。
- 下游应用测试环境不稳定,导致自动化测试脚本经常执行失败。
- 调用方请求进行测试,但是本应用的接口尚未开发完成。
- 某些业务场景使用正常的测试方法很难模拟,比如接口调用超时等。
- 系统尚未开发完成,但急需给客户进行展示。
- TDD模式实施过程中经常碰到协同模块尚未开发完成。
作为一名测试人员,如果我们具备了以上问题的解决能力,测试效率肯定大大的提升。
moutebank作为一个开源的、跨平台、支持多协议的服务模拟工具,有效地帮助测试人员解决以上问题。
我们也可以看看官网推荐使用mountebank的理由。
Trivial to get started
mountebank is easy to install, without any platform dependencies. mountebank aims for fun and comprehensive documentation with lots of examples, and a nice UI that lets you explore the API interactively.
A platform, not just a tool
mountebank aims to be fully cross-platform, with native language bindings. Servers are extensible through scripting when the out of the box functionality isn't enough.
Powerful
mountebank is the only open source service virtualization tool that is non-modal and multi-protocol. Commercial solutions exist, but their licensed platforms make it hard to move the tests closer to development and may even require a specialized IDE. mountebank provides service virtualization free of charge without any platform constraints
Mountebank工作原理
moutebank的工作原理。
Mountebank安装
- 安装Nodejs
- 安装Mountebank
- npm install -g mountebank
- 启动Mountebank
- mb命令启动mountebank。
Mountebank使用示例
-
启动mountebank
- mb --configfile d:\mountebank_ejs\main.ejs --allowInjection
- 启动mountebank后,再进入http://localhost:2525/imposters,发现mock脚本已注入到imposters。
-
接着使用jmeter编写一个简单的jmx脚本进行测试验证。
-
运行脚本
针对运行结果,咱们结合以下mock脚本createAccount.ejs源码进行分析,发现ejs脚本分了两大块,predicates和responses,对应就是前置条件和响应信息。predicates里我们使用了关键字deepEquals,表示深度匹配。再回看jmx脚本,咱们上送的path和body满足predicates条件,所以运行脚本后返回了对应的responses。
"deepEquals": {
"path": "/v1/createAccount",
"body": "\"accountNo\":\"123\""
}
至此,咱们完成了一个简单mountebank示例,mountebank提供了更高级的用法,比如代理proxy、javascript注入等。总之,mountebank足以实现你所有的mock场景。文章最后附上mock脚本源码,有兴趣的童鞋可以尝试,相信你会爱上mountebank。
各脚本源码如下:
main.ejs源码
{
"imposters": [
<% include proxy.ejs %>,
<% include iiacct.ejs %>
]
}
iiacct.ejs源码
{
"port": 8187,
"protocol": "http",
"stubs": [
<% include recharge.ejs %>,
<% include withdraw.ejs %>,
<% include createAccount.ejs %>
]
}
createAccount.ejs源码
{
"predicates": [
{
"contains": {
"path": "/v1/createAccount",
"body": "\"merchantId\":\"123\""
}
}
],
"responses": [
{
"is": {
"statusCode": 500,
"headers": {
"Server": "Apache-Coyote/1.1",
"Content-Type": "text/json;charset=UTF-8",
"Content-Length": 298,
"Date": "Tue, 05 Sep 2017 06:49:14 GMT",
"Connection": "close"
},
"body": "{\"data\":{\"errCode\":\"iia-trade-00010\",\"errMsg\":\"商户不存在\"},\"message\":\"业务处理失败\",\"status\":\"GW-10510\",\"sign\":\"6tbbBajxsMTsql1Gl/VSsI7BHilAvCtA9J0FGiN7+p3Nde7vwZVd9taneNIp4M1zsRhqXXHMFTp67ZFTUItcI8PB4UFnltXomCCW1Jya7dI+hpQilUs2rLQ1WcumGN3GqjWaE472FQbOX2muzcUjJbsMosTo+P0SPawhO5m83Uw=\"}",
"_mode": "text",
"_proxyResponseTime": 135
}
}
]
},
{
"predicates": [
{
"deepEquals": {
"path": "/v1/createAccount",
"body": "\"accountNo\":\"123\""
}
}
],
"responses": [
{
"is": {
"statusCode": 500,
"headers": {
"Server": "Apache-Coyote/1.1",
"Content-Type": "text/json;charset=UTF-8",
"Content-Length": 299,
"Date": "Wed, 06 Sep 2017 07:53:46 GMT",
"Connection": "close"
},
"body": "{\"data\":{\"errCode\":\"iia-acct-00003\",\"errMsg\":\"账户不存在123899\"},\"message\":\"业务处理失败\",\"status\":\"GW-10510\",\"sign\":\"v31ud5d5le/XspEbZevxgu3y5oBfW8lAlyWbeL3O4UnZlIY6Fw8kPreoti4de/CbEI0TpoGCkMAz5NWEAXcX4sny2DM8MK8ZxFAZ2x17H4obaxHKcu09n4a2deEHyaie4k021/8q1t5fucO7ZoEI9QZvyGj/JhC7AzEq1RagFOk=\"}",
"_mode": "text",
"_proxyResponseTime": 661
}
}
]
}