接口测试基本要素

接口文档

参考
https://work.weixin.qq.com/api/doc/90000/90135/90263

http://47.100.175.62:3000/api

文档中主要包含的元素

  • 请求方式
    • GET 一般作为查询
    • Post 一般作为添加数据的请求
    • Put 一般更新数据
    • Delete 一般是删除数据
  • 请求的路径 请求地址
  • 请求的数据
    • 请求参数
  • 服务器返回的结果

发送请求

get请求

请求方式: GET
请求路径: http://47.100.175.62:3000/api/v1/topics
接收 get 参数

  • page Number 页数
  • tab String 主题分类。目前有 ask share job good
  • limit Number 每一页的主题数量 最大值为50,超过50按50计算
  • mdrender String 当为 false 时,不渲染。默认为 true,渲染出现的所有 markdown 格式文本。

在Postman工具发送请求

image.png

使用Requets 库模拟发送请求

参考Requests库官网
https://docs.python-requests.org/en/latest/

code

根据上面的接口文档使用 requests库发送请求

  1. # 导入模块
  2. import requests
  3. # 发送get请求
  4. # 请求地址
  5. url = "http://47.100.175.62:3000/api/v1/topics"
  6. # 请求参数
  7. query_data = {
  8. "page":1,
  9. "tab":"ask",
  10. "limit":10,
  11. "mdrender":"false"
  12. }
  13. # 发送get 请求 params 是内置参数名,表示get请求的参数
  14. r = requests.get(url,params=query_data)
  15. # 查看服务器返回的状态码
  16. print("状态码",r.status_code)
  17. # 查看服务器返回的结果 r.json() 返回的是字典类型的数据
  18. print('结果',type(r.json()), r.json())
  19. # r.text 返回的为字符串
  20. print( type(r.text),r.text)

这里需要注意:

  • r.json() 返回的数据类型为字典
  • r.text 返回的数据类型为字符串

发送Post请求

请求方式: post
请求路径: http://47.100.175.62:3000/api/v1/topics
接收 post 参数

  • accesstoken String 用户的 accessToken 可以使用 a0084525-6022-4719-b817-7ec1efe8a52e 这个token值
  • title String 标题
  • tab String 目前有 ask share job dev。开发新客户端的同学,请务必将你们的测试帖发在 dev 专区,以免污染日常的版面,否则会进行封号一周处理。
  • content String 主体内容

    Postman 手工测试

    image.png

code

  1. # 导入模块
  2. import requests
  3. # 请求的路径
  4. url = "http://47.100.175.62:3000/api/v1/topics"
  5. # 请求body
  6. body_data = {
  7. "accesstoken":"a0084525-6022-4719-b817-7ec1efe8a52e",
  8. "title":"hello 大家好!!",
  9. "tab":"ask",
  10. "content":"大家好!!!"
  11. }
  12. # 发送post请求 将请求体传给json,requests发送请求的时候会自动使用json格式来发送
  13. r = requests.post(url,json=body_data)
  14. print(r.status_code)
  15. print(r.json())