『居善地』接口測試 — 21.Mock功能介紹(二)

6、Moco框架的使用

當需要調用接口來編寫測試用例的時候,此時該接口並沒有被實現,這個時候我們就可以用Mock框架來模擬一個接口出來。

使用Mock模擬接口以下功能:

  • 攔截服務:httphttps

  • 請求方式:GET,POST。

  • 模擬請求地址:URL。

  • 模擬參數:包括headercookie的數據。

  • 模擬響應結果。

  • 支持重定向。

(1)Moco框架第一個練習

編寫一個Json文件,接口所有的信息都配置在該json文件中。

[
  {
    "description": "第一個Moco框架例子。",  # 描述:增加接口的可讀性
    "request": {
      "uri": "/api/moco/demo",
    },
    "response": {
      "text": "hello Moco !"
    }
  }
]    

把Moco框架的jar包和上面編輯好的Json文件放在同一個文件夾中。

在cmd命令行或者PyCharm的命令行終端執行啓動命令。

  • 進入json文件的所在目錄。
  • 執行命令:java -jar ./moco-runner-0.12.0-standalone.jar http -p 12306 -c test.json

Moco服務啓動後,我們可以使用Requests庫請求接口,也可以用瀏覽器接口。

# 1.導入requests庫
import requests

# 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/demo"

# 3.發送請求
response = requests.get(url=url)
print(response.text)

瀏覽器訪問接口:

(2)Get方法的Mock實現

我們主要是看Json文件怎麼寫,其他步驟和上面練習一樣。

1)、沒有參數的get請求

[
  {
    "description": "模擬一個沒有參數的get請求。",
    "request": {
      "uri": "/api/moco/get/demo",
      "method": "get"  # 這裏添加了要給method屬性
    },
    "response": {
      "text": "hello get request !"
    }
  }
]

2)、有參數的get請求

[
  {
    "description": "模擬一個沒有參數的get請求。",
    "request": {
      "uri": "/api/moco/get/demo",
      "method": "get"
    },
    "response": {
      "text": "hello get request !"
    }
  },
  {
    "description": "模擬一個帶參數的get請求。",
    "request": {
      "uri": "/api/moco/get/param/demo",
      "method": "get",
      "queries": {      # get請求參數的選項,queries固定屬性。
        "name": "xiaoming",
        "age": "18"
      }
    },
    "response": {
      "text": "hello xiaoming !"
    }
  }
]

說明:請求地址爲:http://127.0.0.1:12306/api/moco/get/param/demo?name=xiaoming&age=18

(3)Post方法的Mock實現

1)、沒有參數的post請求

[
  {
    "description": "模擬一個不帶數據的post請求。",
    "request": {
      "uri": "/api/moco/post/demo",
      "method": "post"    
    },
    "response": {
      "text": "hello post request !"
    }
  }
]

提示:POST請求就不能用瀏覽器進行查看了。只能用Request庫或者JMeter,Postman等進行查看。(能進行接口調用的工具都可以)

# 1.導入requests庫
import requests

# 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/post/demo"

# 3.發送請求
response = requests.post(url=url)
print(response.text)

2)、有參數的post請求

[
  {
    "description": "模擬一個帶數據post請求。",
    "request": {
      "uri": "/api/moco/post/param/demo",
      "method": "post",
      "forms": {      # post請求帶參數,參數要添加到forms屬性中。
        "name": "xiaoming",
        "age": "18"
      }
    },
    "response": {
      "text": "hello post xiaoming !"
    }
  }
]

調用接口查看結果。

# 1.導入requests庫
import requests

# 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/post/param/demo"

data = {
    "name": "xiaoming",
    "age": "18"
}

# 3.發送請求
response = requests.post(url=url, data=data)
print(response.text)

(4)請求中加入Cookies

使用的是request中的cookies屬性。

1)、get請求

[
  {
    "description": "模擬一個帶cookie的get請求。",
    "request": {
      "uri": "/api/moco/get/cookies/demo",
      "method": "get",
      "cookies": {          # 這裏添加cookies參數
        "login": "true"
      }
    },
    "response": {
      "text": "hello get cookies !"
    }
  }
]

調用接口查看結果。

# 1.導入requests庫
import requests

# 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/get/cookies/demo"

cookies = {
    "login": "true"
}

# 3.發送請求
response = requests.get(url=url, cookies=cookies)
print(response.text)

2)、post請求

[
  {
    "description": "模擬一個帶cookie的post請求。",
    "request": {
      "uri": "/api/moco/post/cookies/demo",
      "method": "post",
      "cookies": {
        "login": "true"
      },
      "json": {     # post請求的參數也可以用json格式的數據進行傳輸
        "name": "xiaoming",
        "age": "18"
      }
    },
    "response": {
      "status": 201,
      "json": {
        "text": "hello post cookies !"
      }
    }
  }
]

調用接口查看結果。

# 1.導入requests庫
import requests

# 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/post/cookies/demo"

cookies = {
    "login": "true"
}

json = {
    "name": "xiaoming",
    "age": "18"
}

# 3.發送請求
response = requests.post(url=url, json=json ,cookies=cookies)
print(response.text)

(5)請求中加入Header

使用的是request中的headers屬性。

Header是添加請求頭信息,關於請求頭信息get請求和post請求都是一樣的。

[
  {
    "description": "模擬一個帶Header的post請求。",
    "request": {
      "uri": "/api/moco/post/headers/demo",
      "method": "post",
      "headers": {      # 添加請求頭信息
        "content-type": "application/json"
      },
      "json": {
        "name": "xiaoming",
        "age": "18"
      }
    },
    "response": {
      "status": 201,
      "json": {
        "text": "hello get Headers !"
      }
    }
  }
]

調用接口查看結果。

# 1.導入requests庫
import requests

# 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/post/headers/demo"

headers = {
    "content-type": "application/json"
}

json = {
    "name": "xiaoming",
    "age": "18"
}

# 3.發送請求
response = requests.post(url=url, json=json, headers=headers)
print(response.text)

(6)Moco模擬重定向

重定向使用的是和request同級的redirectTo屬性。

[
  {
    "description": "重定向到百度",
    "request": {
      "uri": "/api/moco/redirect/demo",
      "method": "get"
    },
    "redirectTo": "http://www.baidu.com"
  },
  {
    "description": "重定向到自己的接口",
    "request": {
      "uri": "/api/moco/redirect/new/demo",
      "method": "get"
    },
    "redirectTo": "http://www.baidu.com",
    "response": {
      "text": "hello redirectTo !"
    }
  }
]

使用瀏覽器進行測試就可以。

還有更多的使用方式請查看文檔:https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md

(7)綜合練習

[
  {
    "description": "department by dep_id",
    "request": {
      "uri": "/api/departments/",
      "method": "get",
      "queries": {
        "$dep_id_list": "T001"
      }
    },
    "response": {
      "json": {
        "count": 1,
        "next": null,
        "previous": null,
        "results": [
          {
            "dep_id": "T001",
            "dep_name": "php學院",
            "master_name": "老李",
            "slogan": "啦啦啦啦"
          }
        ]
      }
    }
  },
  {
    "description": "update department by dep_id",
    "request": {
      "uri": "/api/departments/T03/",
      "method": "put",
      "json": {
        "data": [
          {
            "dep_id": "T03",
            "dep_name": "C++",
            "master_name": "C++-Master",
            "slogan": "Here is Slogan"
          }
        ]
      }
    },
    "response": {
      "status": 201,
      "json": {
        "dep_id": "T03",
        "dep_name": "C++",
        "master_name": "C++-Master",
        "slogan": "Here is Slogan"
      }
    }
  }
]

(8)總結:

Json文件的配置屬性說明:

像我們上面練習過的Json文件配置,所有的數據值是固定的,

如:descriptionrequestresponseredirectTo等這些都是固定的,不能修改,修改可能連Moco服務都啓動不來。

還有request的屬性值,如:urimethodcookiesheaders,也是必須這樣寫的。

還有GET請求傳遞參數用queries屬性,POST請求傳遞參數用formsjson屬性都可以。(PUT,DELETE請求同Post請求。)

Moco框架原理:

就是把所有接口的數據,包括髮送請求的所有數據和返回結果的所有數據,以Json數據格式進行編寫。

把這些數據放入Moco框架提供的HTTP或者HTTPS的服務上,就實現了接口數據的模擬。

在使用的時候,我們只要按照json文件中接口配置的信息進行請求即可,如果調用接口傳遞的數據和Json文件中接口編寫要接收的數據不一致,則無法請求成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章