来自 cnblogs.com/fnng/p/9919803.html
比较 robot framework\unitetest\ pytest \httprunner \gauge

image.png
pytest
- 优点
1.兼容unitest且更简洁
pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效
Unittest+Request+HTMLRunner
-
优点
1.足够灵活强大: 分层测试、数据驱动、测试报告,集成CI...
缺点:
1.有一定的学习成本
数据文件:
{
"test_case1": {
"key": "value1",
"status_code": 200
},
"test_case2": {
"key": "value2",
"status_code": 200
},
"test_case3": {
"key": "value3",
"status_code": 200
},
"test_case4": {
"key": "value4",
"status_code": 200
}}
测试用例:
import requests
import unittest
from ddt import ddt, file_data
@ddtclass InterfaceTest(unittest.TestCase):
def setUp(self):
self.url = "http://httpbin.org/post"
def tearDown(self):
print(self.result)
@file_data("./data/test_data_dict.json")
def test_post_request(self, key, status_code):
r = requests.post(self.url, data={"key": key})
self.result = r.json()
self.assertEqual(r.status_code, status_code)
总结:推荐使用,代码相对简单,功能足够灵活。
robot framework
-
优点
1.关键字驱动,自定义用户关键字。
- 支持测试日志和报告生成。
- 支持系统关键字开发,可扩展性好。
- 支持数据库操作。
缺点:
1.接口测试用例写起来不简洁。
2.需要掌握特定语法。
*** Settings ***
Library RequestsLibrary
Library Collections
*** Test Cases ***
test_get_event_list # 查询发布会(GET请求)
${payload}= Create Dictionary eid=1
Create Session event http://127.0.0.1:8000/api
${r}= Get Request event /get_event_list/ params=${payload}
Should Be Equal As Strings ${r.status_code} 200
log ${r.json()}
${dict} Set variable ${r.json()}
#断言结果
${msg} Get From Dictionary ${dict} message
Should Be Equal ${msg} success
${sta} Get From Dictionary ${dict} status
${status} Evaluate int(200)
Should Be Equal ${sta} ${status}
HttpRunner
-
优点
1.基于YAML/JSON格式,专注于接口本身的编写。
- 接口编写简单
- 生成测试报告。
- 接口录制功能
缺点:
1.没有编辑器插件对语法校验,容易出错
2.官方文档没有详细的说明扩展不方便
[
{
"config": {
"name": "testcase description",
"variables": [],
"request": {
"base_url": "http://127.0.0.1:5000",
"headers": {
"User-Agent": "python-requests/2.18.4"
}
}
}
},
{
"test": {
"name": "test case name",
"request": {
"url": "/api/get-token",
"headers": {
"device_sn": "FwgRiO7CNA50DSU",
"user_agent": "iOS/10.3",
"os_platform": "ios",
"app_version": "2.8.6",
"Content-Type": "application/json"
},
"method": "POST",
"date": {"sign": "958a05393efef0ac7c0fb80a7eac45e24fd40c27"}
},
"validate": [
{"eq": ["status_code", 200]},
{"eq": ["headers.Content-Type", "application/json"]},
{"eq": ["content.success", true]},
{"eq": ["content.token", "baNLX1zhFYP11Seb"]}
]
}
}]
总结:可以考虑,至于接口数据的初始化可能需要单独处理