• 接口自动化项目落地之HTTPBin网站


    原文:https://www.cnblogs.com/df888/p/16011061.html
     

    接口自动化项目落地系列

    找个开源网站或开源项目,用tep实现整套pytest接口自动化项目落地,归档到电子书,作为tep完整教程项目篇一部分。自从tep完整教程发布以后,tep被越来越多小伙伴了解。教程只是纯理论,是骡子是马,拉出来遛遛才知道。做接口自动化项目落地,一方面是为了让自己脑海中的构想实实在在的呈现出来,现实和理想存在多少差距,不断尝试去弥补和修缮;另一方面也是方便读者朋友们学习使用,借助实际项目来练习,才能在赛道中弯道超车。

    HTTPBin网站

    httpbin.org是一个简单的在线提供HTTP服务的网站:

    它能够用来对HTTP进行在线测试。

    测试报告

    HTTPBin网站的接口自动化项目包含11个用例集

    67条测试用例

    自动化执行正确率98.5%,其中有1条错误结果,是我故意为之的,因为想展示下断言失败的效果。

    环境配置

    包含http和https两套环境,因为HTTPBin支持HTTP&HTTPS:

    fixtures/fixture_env_vars.py

    1. #!/usr/bin/python
    2. # encoding=utf-8
    3. from tep.fixture import *
    4. @pytest.fixture(scope="session")
    5. def env_vars(config):
    6. class Clazz(TepVars):
    7. env = config["env"]
    8. """变量定义开始"""
    9. # 环境变量
    10. mapping = {
    11. "http": { # http环境
    12. "domain": "http://httpbin.org",
    13. },
    14. "https": { # https环境
    15. "domain": "https://httpbin.org",
    16. }
    17. # 继续添加
    18. }
    19. # 定义类属性,敲代码时会自动补全
    20. domain = mapping[env]["domain"]
    21. """变量定义结束"""
    22. return Clazz()

    配置默认为http环境:

    conf.yaml

    env: http
    

    用例集

    http-methods

    1. import allure
    2. from tep.client import request
    3. @allure.title("get请求")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "get",
    10. url=env_vars.domain + "/get",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'application/json',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'},
    15. params={}
    16. )
    17. # 提取
    18. # 断言
    19. assert response.status_code < 400

    auth

    1. import allure
    2. from tep.client import request
    3. @allure.title("Authorization以Bearer开头,认证成功")
    4. def test(env_vars):
    5. # 描述
    6. # http://httpbin.org/#/Auth/get_basic_auth__user___passwd_
    7. # 数据
    8. # 请求
    9. response = request(
    10. "get",
    11. url=env_vars.domain + "/bearer",
    12. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'application/json',
    13. 'Authorization': 'Bearer ZG9uZ2ZhbmdlcjoxMjM0NTY=', # 替换token
    14. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    15. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    16. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'},
    17. params={}
    18. )
    19. # 提取
    20. # 断言
    21. assert response.status_code < 400

    status-codes

    1. import allure
    2. from tep.client import request
    3. @allure.title("post返回状态码300")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "post",
    10. url=env_vars.domain + "/status/300",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'Content-Length': '0', 'accept': 'text/plain',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Origin': 'http://httpbin.org', 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    15. 'Cookie': 'stale_after=never; fake=fake_value'},
    16. json={}
    17. )
    18. # 提取
    19. # 断言
    20. assert response.status_code == 300

    request_inspection

    1. import allure
    2. from tep.client import request
    3. @allure.title("捕获请求信息--headers")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "get",
    10. url=env_vars.domain + "/headers",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'application/json',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    15. 'Cookie': 'stale_after=never; fake=fake_value'},
    16. params={}
    17. )
    18. # 提取
    19. # 断言
    20. assert response.status_code < 400
    21. assert response.json()["headers"]

    response_inspection

    1. import allure
    2. from tep.client import request
    3. @allure.title("捕获响应信息--缓存")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "get",
    10. url=env_vars.domain + "/cache",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'Cache-Control': 'max-age=0',
    12. 'accept': 'application/json', 'If-None-Match': '1', 'If-Modified-Since': '1',
    13. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    14. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    15. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    16. 'Cookie': 'stale_after=never; fake=fake_value'},
    17. params={}
    18. )
    19. # 提取
    20. # 断言
    21. assert response.status_code == 304

    response_formats

    1. import allure
    2. from tep.client import request
    3. @allure.title("txt文本text/plain")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "get",
    10. url=env_vars.domain + "/robots.txt",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'text/plain',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    15. 'Cookie': 'stale_after=never; fake=fake_value'},
    16. params={}
    17. )
    18. # 提取
    19. # 断言
    20. assert response.status_code < 400
    21. assert response.headers["content-type"] == "text/plain"

    dynamic_data

    1. import allure
    2. from tep.client import request
    3. @allure.title("base64解码")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "get",
    10. url=env_vars.domain + "/base64/SFRUUEJJTiBpcyBhd2Vzb21l",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'text/html',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    15. 'Cookie': 'stale_after=never; fake=fake_value'},
    16. params={}
    17. )
    18. # 提取
    19. # 断言
    20. assert response.status_code < 400
    21. assert "HTTPBIN is awesome" == response.text

    cookies

    1. import allure
    2. from tep.client import request
    3. @allure.title("cookies")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "get",
    10. url=env_vars.domain + "/cookies",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'application/json',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    15. 'Cookie': 'stale_after=never; fake=fake_value'},
    16. params={}
    17. )
    18. # 提取
    19. # 断言
    20. assert response.status_code < 400
    21. assert response.json()["cookies"]

    images

    1. import allure
    2. from tep.client import request
    3. @allure.title("图片")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "get",
    10. url=env_vars.domain + "/image",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'image/webp',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    15. 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'},
    16. params={}
    17. )
    18. # 提取
    19. # 断言
    20. assert response.status_code < 400

    redirects

    1. import allure
    2. from tep.client import request
    3. @allure.title("重定向")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "get",
    10. url=env_vars.domain + "/redirect/1",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'text/html',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    15. 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'},
    16. params={}
    17. )
    18. # 提取
    19. # 断言
    20. assert response.status_code == 404

    anything

    1. import allure
    2. from tep.client import request
    3. @allure.title("返回所有数据")
    4. def test(env_vars):
    5. # 描述
    6. # 数据
    7. # 请求
    8. response = request(
    9. "delete",
    10. url=env_vars.domain + "/anything",
    11. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'application/json',
    12. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    13. 'Origin': '', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate',
    14. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    15. 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'},
    16. json={}
    17. )
    18. # 提取
    19. # 断言
    20. assert response.status_code < 400
    21. # 描述
    22. # 数据
    23. # 请求
    24. response = request(
    25. "get",
    26. url=env_vars.domain + "/anything",
    27. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'application/json',
    28. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    29. 'Referer': '/', 'Accept-Encoding': 'gzip, deflate',
    30. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    31. 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'},
    32. params={}
    33. )
    34. # 提取
    35. # 断言
    36. assert response.status_code < 400
    37. # 描述
    38. # 数据
    39. # 请求
    40. response = request(
    41. "patch",
    42. url=env_vars.domain + "/anything",
    43. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'application/json',
    44. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    45. 'Origin': '', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate',
    46. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    47. 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'},
    48. json={}
    49. )
    50. # 提取
    51. # 断言
    52. assert response.status_code < 400
    53. # 描述
    54. # 数据
    55. # 请求
    56. response = request(
    57. "post",
    58. url=env_vars.domain + "/anything",
    59. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'Content-Length': '0',
    60. 'accept': 'application/json',
    61. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    62. 'Origin': '', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate',
    63. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    64. 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'},
    65. json={}
    66. )
    67. # 提取
    68. # 断言
    69. assert response.status_code < 400
    70. # 描述
    71. # 数据
    72. # 请求
    73. response = request(
    74. "put",
    75. url=env_vars.domain + "/anything",
    76. headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'Content-Length': '0',
    77. 'accept': 'application/json',
    78. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    79. 'Origin': '', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate',
    80. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
    81. 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'},
    82. json={}
    83. )
    84. # 提取
    85. # 断言
    86. assert response.status_code < 400

    只花了3小时完成

    通过mitmproxy来录制流量自动生成用例,效率得到了极大的提高,从原来的1天缩短到3小时就完成了整个项目落地。相比于手工编写用例,这次写HTTPBin的接口自动化,我使用了utils/mitm.py来录制流量,mitmproxy稍微不方便的是需要手动开启代理,不过适应了以后还是能接受。录制流量后就会生成自动化用例,但是还需要二次修改,才会变成最终的用例。主要修改的工作量是在添加断言,根据业务设置合理的断言。其次是替换url为env_vars.domain + "/api"拼接方式,直接批量Replace即可。然后就是修改文件名和@allure.title了,给用例加上标题。工欲善其事,必先利其器。

    tep共建

    欢迎添加微信:cekaigang,分享交流tep实践案例,可以提供开源项目我来写,也可以写好后发我一起看看,优秀的项目会添加到tep完整教程的项目篇,以便更多测试同行们借鉴,大佬们赶快来加入我们吧。

    参考资料:

    HTTPBin接口自动化项目源码 GitHub - dongfanger/httpbin: httpbin.org接口自动化项目

    postman Postman

  • 相关阅读:
    2022年全球市场超宽带电容器总体规模、主要生产商、主要地区、产品和应用细分研究报告
    数学建模学习(83):模拟退火算法,最详细版本
    ThreadLocal
    数据分析基础入门_环境安装
    QA新人任务成果演示
    美团财务科技Java后端一面:面向对象、类加载过程、全限定类名相同的类是否可以同时被加载
    交换机与路由技术-30-标准ACL
    前端面试宝典React篇14 如何避免重复渲染?
    操作视频的开始与暂停
    Android自定义属性的基本使用
  • 原文地址:https://blog.csdn.net/javastart/article/details/134516264