測試工具Wiremock介紹


WireMock是一個開源的測試工具,支持HTTP響應存根、請求驗證、代理/攔截、記錄和回放。最直接的用法: 

  • 為Web/移動應用構建Mock Service
  • 快速創建Web API原型
  • 模擬Web Service中錯誤返回
  • 錄制HTTP請求和回放

 一般開發項目都會把前端組和Service組分開,當進度不一致時,可以根據接口構建Mock Service對和模擬不同輸入/數據/場景,這樣不至於影響兩組的開發進度。構建Mock Service方法很多,node.js大概五句代碼,另一測試工具soapUI也可做到,同時還可以對Service進行功能/性能測試,功能齊全。Wiremock好在輕便,一個jar包基本夠用了,當然,也可以把它引用寫進測試代碼里。

官網地址:http://wiremock.org/

Jar下載:http://repo1.maven.org/maven2/com/github/tomakehurst/wiremock/1.57/wiremock-1.57-standalone.jar

Mock Service

下載后,在命令行中運行:(需要java JDK環境)

java -jar wiremock-1.57-standalone.jar –port 9999 --verbose

 (不指定端口默認為8080; 開啟日志:--verbose。更多參數,請至:http://wiremock.org/running-standalone.html。 把命令放在bat文件中比較方便)

啟動后在同目錄下生成兩個空的文件夾:__files和mappings。__files是放上傳/下載/錄制文件用的,mappings放你想要的Service返回數據和Url mapping.

在mappings文件夾下隨便創建一個*.json文件:

(注意,添加修改mapping文件后,都需要重啟服務才能生效)

{
    "request": {
        "method": "GET",
        "url": "/api/mytest"
    },
    "response": {
        "status": 200,
        "body": "More content\n"
    }
}

 

在Fiddler/瀏覽器/curl命令調用: http://localhost:9999/api/mytest

 

這是一個HTTP GET的例子,還可以在response中自定義返回的頭headers:

"response": {

            "status": 200,
            "body": "Hello world!",
            "headers": { "Content-Type": "text/plain" }
}

 HTTP方法支持GET, POST, PUT, DELETE, HEAD, TRACE, OPTIONS等,自定義頭、數據模板(bodyPatterns,如下例,如不符合,拋出404錯誤),URL Template,Query參數匹配,顯示指定文件內容等。

如以下例子:

 POST: http://localhost:9999/api/products

{
    "request": {
        "method": "POST",
        "url": "/api/products",
          "bodyPatterns": [
                 {"equalToJson" : "{ \"name\": \"new product\", \"creator\": \"tester\", \"createTime\": \"2015-09-07\" }", "jsonCompareMode": "LENIENT"}
         ]
    },
    "response": {
        "status": 201,
        "body": "Add successfully.",
         "headers":{
                   "x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
         }
    }
}

PUT: http://localhost:9999/api/products/1

{
    "request": {
        "method": "PUT",
        "url": "/api/products/1",
          "bodyPatterns": [
            {"equalToJson" : "{ \"id\": 1, \"name\": \"new product\", \"creator\": \"tester\", \"createTime\": \"2015-09-07\" }", "jsonCompareMode": "LENIENT"}
         ]
    },
    "response": {
        "status": 200,
        "body": "Update successfully.",
         "headers":{
                  "x-token":" xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
         }
    }
}

DELETE: http://localhost:9999/api/products/1

{
    "request": {
        "method": "DELETE",
        "url": "/api/products/1" 
    },
    "response": {
        "status": 204,      
         "headers":{
                   "x-token":" xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
         }
    }
}

 URL Matching: http://localhost:9999/api/products/1(2/3...)

{
    "request": {
        "method": "GET",
        "urlPattern": "/api/products/[0-9]+"
    },
    "response": {
        "status": 200
    }
}

 Query參數匹配:http://localhost:9999/api/products?search=china

{
    "request": {
        "method": "GET",
        "urlPath": "/api/products",
        "queryParameters": {
            "search": {
                "contains": "chin"
            }
        }
    },
    "response": {
            "status": 200,
             "headers":{ "Content-Type": "application/json"},
              "body": "{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China\" },{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China(RPC)\" }"
    }
}

 返回文件內容:http://localhost:9999/file/1

{
    "request": {
        "method": "GET",
        "url": "/file/1"
    },
    "response": {
        "status": 200,
        "bodyFileName": "test.xml"(或:”xmlfiles/test.xml”)
    }
}

(在__files文件夾下建好所調文件,路徑為相對__files文件夾。)

 模擬錯誤

{
  "request" : {
    "url" : "/unknown.html",
    "method" : "GET"
  },
  "response" : {
    "status" : 404,   
    "headers" : {     
      "Content-Type" : "text/html; charset=utf-8"    
    }
  }
}

 

{
    "request": {
        "method": "GET",
        "url": "/fault"
    },
    "response": {
        "fault": "MALFORMED_RESPONSE_CHUNK"
    }
}

 

錄制HTTP請求及回放 

Wiremock的錄制過程是啟動一個代理服務,截取HTTP請求和響應,在mappings文件夾中創建一json文件記錄下請求地址和響應概要,在__files文件夾下創建一文件包含響應內容;當重啟Standalone進程時,那些記錄下的請求響應就會作為Mock Service生成。 

啟動錄制服務: 

java -jar wiremock-1.57-standalone.jar --proxy-all="http://localhost:7777" --record-mappings –verbose

默認的代理服務端口是8080,即之后發向http://localhost:7777的請求,可以用http://localhst:8080/來代理。

代理前:

代理后:

錄制記錄:

 生成mapping文件和響應內容文件:

 

{
  "request" : {
    "url" : "/test.aspx",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "body-test.aspx-WK0fD.json",
    "headers" : {
      "Cache-Control" : "private",
      "Content-Type" : "text/html; charset=utf-8",
      "Server" : "Microsoft-IIS/7.5",
      "X-AspNet-Version" : "2.0.50727",
      "X-Powered-By" : "ASP.NET",
      "Date" : "Tue, 08 Sep 2015 03:14:36 GMT",
      "Content-Length" : "61"
    }
  }
}

 

錄制完重啟服務,驗證剛才錄制的請求是否生效:

 

更多的功能,如集成到測試代碼、場景模擬(比較接近於真實的情景),請參閱官網:http://wiremock.org/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM