主流接口测试框架那么多怎么选择?优缺点对比它来了

需求

1、接口编写方便。
2、方便调试接口。
3、支持数据初始化。
4、生成测试报告。
5、支持参数化。

1、robot framework

优点

  • 关键字驱动,自定义用户关键字。

  • 支持测试日志和报告生成。

  • 支持系统关键字开发,可扩展性好。

  • 支持数据库操作。

缺点

缺点

  • 接口测试用例写起来不简洁。

  • 需要掌握特定语法。

    1. *** Settings ***

    2. Library RequestsLibrary

    3. Library Collections

    4. *** Test Cases ***

    5. test_get_event_list # 查询发布会(GET请求)

    6. ${payload}= Create Dictionary eid=1

    7. Create Session event http://127.0.0.1:8000/api

    8. ${r}= Get Request event /get_event_list/ params=${payload}

    9. Should Be Equal As Strings ${r.status_code} 200

    10. log ${r.json()}

    11. ${dict} Set variable ${r.json()}

    12. #断言结果

    13. ${msg} Get From Dictionary ${dict} message

    14. Should Be Equal ${msg} success

    15. ${sta} Get From Dictionary ${dict} status

    16. ${status} Evaluate int(200)

    17. Should Be Equal ${sta} ${status}

    结果:不考虑,没人愿意这么写接口用例。

    2、JMeter

    优点

  • 支持参数化

  • 不需要写代码

  • 创建接口用例效率不高。

  • 不能生成查看每一个接口执行情况的测试报告。

总结:不考虑,接口编写不方便,最主要是不能生成测试报告,如果做接口性能的话可以考虑。

3、HttpRunner

优点:

  • 基于YAML/JSON格式,专注于接口本身的编写。

  • 接口编写简单

  • 生成测试报告

  • 接口录制功能。

缺点:

  • 没有编辑器插件对语法校验,容易出错。

  • 官方文档没有详细的说明。

  • 扩展不方便。

    1. [

    2. {

    3. "config": {

    4. "name": "testcase description",

    5. "variables": [],

    6. "request": {

    7. "base_url": "http://127.0.0.1:5000",

    8. "headers": {

    9. "User-Agent": "python-requests/2.18.4"

    10. }

    11. }

    12. }

    13. },

    14. {

    15. "test": {

    16. "name": "test case name",

    17. "request": {

    18. "url": "/api/get-token",

    19. "headers": {

    20. "device_sn": "FwgRiO7CNA50DSU",

    21. "user_agent": "iOS/10.3",

    22. "os_platform": "ios",

    23. "app_version": "2.8.6",

    24. "Content-Type": "application/json"

    25. },

    26. "method": "POST",

    27. "date": {"sign": "958a05393efef0ac7c0fb80a7eac45e24fd40c27"}

    28. },

    29. "validate": [

    30. {"eq": ["status_code", 200]},

    31. {"eq": ["headers.Content-Type", "application/json"]},

    32. {"eq": ["content.success", true]},

    33. {"eq": ["content.token", "baNLX1zhFYP11Seb"]}

    34. ]

    35. }

    36. }]

    总结:可以考虑,至于接口数据的初始化可能需要单独处理。

4、gauge

BDD行为驱动测试框架。

优点:

  • 行为文件与脚本文件分离,本质上实现了数据驱动。

  • 功能强大灵活,本质上还用Python写接口用例。

  • 自动生成测试报告。

  • VS Code有支持插件

缺点:

  • 门槛略高,需要了解BDD的用法。

  • 需要会markdworn语法

行为描述文件:

  1. ## test post request

  2. * post "http://httpbin.org/post" interface

  3. |key | status_code|

  4. |------|-----------|

  5. |value1|200 |

  6. |value2|200 |

  7. |value3|200 |

测试脚本:

  1. ……

  2. @step("post <url> interface <table>")

  3. def test_get_request(url, table):

  4. values = []

  5. status_codes = []

  6. for word in table.get_column_values_with_name("key"):

  7. values.append(word)

  8. for word in table.get_column_values_with_name("status_code"):

  9. status_codes.append(word)

  10. for i in range(len(values)):

  11. r = requests.post(url, data={"key": values[i]})

  12. result = r.json()

  13. assert r.status_code == int(status_codes[i])

总结:推荐使用,BDD有一定门槛,看测试人员的学些能力和接受速度。

 
5、Unittest+Request+HTMLRunner

利用现有的框架和库自己定制。

优点:

  • 足够灵活强大: 分层测试、数据驱动、测试报告,集成CI...

缺点:

  • 有一定的学习成本

数据文件:

  1. {

  2. "test_case1": {

  3. "key": "value1",

  4. "status_code": 200

  5. },

  6. "test_case2": {

  7. "key": "value2",

  8. "status_code": 200

  9. },

  10. "test_case3": {

  11. "key": "value3",

  12. "status_code": 200

  13. },

  14. "test_case4": {

  15. "key": "value4",

  16. "status_code": 200

  17. }}

测试用例:

  1. import requests

  2. import unittest

  3. from ddt import ddt, file_data

  4. @ddtclass InterfaceTest(unittest.TestCase):

  5. def setUp(self):

  6. self.url = "http://httpbin.org/post"

  7. def tearDown(self):

  8. print(self.result)

  9. @file_data("./data/test_data_dict.json")

  10. def test_post_request(self, key, status_code):

  11. r = requests.post(self.url, data={"key": key})

  12. self.result = r.json()

  13. self.assertEqual(r.status_code, status_code)

总结:推荐使用,代码相对简单,功能足够灵活。

  1. 我花了两天时间整理这些框架,其实重点就是了解HttpRunner 和 gauge 。

  2. yg

  3. HttpRunner 没有编辑器插件,本身就是一个YAML/JSON配置文件,所以配置写错了,但只要是合法的YAML/JSON格式,也看不出来,只有运行的过后才知道。就像你用记事本写代码一样,只有运行了才知道代码有没有写错。

  4. 另外,扩展起来也不是特别方便,单独用python实现一些函数:在json文件中

  5. ```{"device_sn": "${gen_random_string(15)}"}```

  6. 以这样的方式引用```gen_random_string()``` 函数。

  7. gauge我已经分享过两篇基础文章了,虽然用BDD拿来做接口理念不搭,但并不是不可以,唯一的缺点是用BDD来描述接口行为不合适,其他的都没毛病,可以参数化,断言写起来也简单,测试报告也漂亮,本质上还是用Python实现一些功能,所以非常灵活。

  8. unittest + requests + HTMLTestRunner是我最熟悉的方案,几乎没什么短板。以前通过这种方案写过很多测试用例,这次把ddt加上似乎更完美了。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值