『居善地』接口测试 — 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文件中接口编写要接收的数据不一致,则无法请求成功。

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