【mock service系列】如何用mountebank做mock service

Mountebank--江湖騙子,他有藥,你要嗎?

最開始看到這個詞感受怪怪的,詞典解釋是江湖騙子、賣假藥的意思,感受很奇妙,爲啥子取這個名字。
官網圖片以下,確實有一瓶藥。
圖片描述git

What

Mountebank究竟是什麼?
從我使用的理解,就是給開發者提供假的api,替換掉被真實依賴的api,這在測試場景中常常用到。github

How

如何使用,能夠單獨使用,按照官網教程就好,也能夠結合docker使用。
後續我會把單獨使用的demo以及結合docker使用的demo 上傳至github 供你們參考,歡迎提意見。
詳細的步驟請參考git裏面的commit信息。
第一個get api 效果如圖
圖片描述
圖片描述docker

純數據代碼只有下面一部分shell

{
  "port": 4545,
  "protocol": "http",
  "stubs": [{
      "responses": [{
        "is": {
          "statusCode": 200,
          "headers": {
            "Content-Type": "application/json"
          },
          "body": ["Australia", "Brazil", "Canada", "Chile", "China", "Ecuador", "Germany", "India", "Italy", "Singapore", "South Africa", "Spain", "Turkey", "UK", "US Central", "US East", "US West"]
        }
      }],
      "predicates": [{
        "equals": {
          "path": "/country",
          "method": "GET"
        }
      }]
    }, {
      "responses": [{
        "is": {
          "statusCode": 400,
          "body": {
            "code": "bad-request",
            "message": "Bad Request"
          }
        }
      }]
    }]
}

而須要把數據run起來 腳本部分只要shell就好json

#!/bin/sh
set -e
RUN_RESULT=$(docker ps | grep hasanozgan/mountebank | wc -l)
MOUNTEBANK_URI=http://localhost:2525
BANK_IS_OPEN=1

if [ "$RUN_RESULT" -eq 0 ]; then
  docker run -p 2525:2525 -p 4545:4545 -d hasanozgan/mountebank
fi

curl $MOUNTEBANK_URI/imposters || BANK_IS_OPEN=0
if [ $BANK_IS_OPEN -eq 1 ]; then
  break
fi

curl -X DELETE $MOUNTEBANK_URI/imposters/4545
curl -X POST -H 'Content-Type: application/json' -d @stubs.json $MOUNTEBANK_URI/imposters

Why

爲何選用mountebank,
一個緣由是由於以前沒用過,只是聽到國內一些講測試的文章有說過,看完並無get太多的點,還不如動手玩一下;
另外更重要的緣由是恰好一個codebase有現成的,有項目契機作事情很lucky。api

拋開這些自身興趣和巧合,我推薦開發者選用mountebank的緣由大概會有如下幾個app

  1. 輕量級 無平臺依賴curl

  2. 多語言 多協議支持ide

  3. 免費post

  4. 有UI交互有趣

更多的自詡部分參見Why 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

When&缺點

其實目前我也剛使用不久,也不知道啥太大的缺點,而關於何時使用呢?當有須要mock假的依賴的api的時候,就可使用,感受和沒說同樣。只是目前國內使用的很少,想一想先積累點使用經驗也是好的,說不定又激發新的靈感。

相關文章
相關標籤/搜索