内容简介:端午节我写了一个项目,帮助你学习HTTP接口测试。GitHub地址:
端午节我写了一个项目,帮助你学习HTTP接口测试。
GitHub地址:
https://github.com/defnngj/learning-API-test
整个项目基于Flask和 Requests实现。
Flask是 Python 主流的Web框架,以简单著称,它可非常方便的实现API,整个项目中的API都通过一个文件实现。
Requests是模拟HTTP的测试库,同样是Python语言的明星库,它可以以非常简单的方式模拟HTTP请求。
如何开始学习
克隆或下载项目,安装依赖。
$ pip install -r requirements.txt
启动Flask项目。
$ python api_server.py * Serving Flask app "api_server.py" (lazy loading) * Environment:production WARNING: Do not use thedevelopment server in a production environment. Use a production WSGI serverinstead. * Debug mode: on * Running onhttp://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting withstat * Debugger isactive! * Debugger PIN:208-740-173
接下来,你可根据项目文档中所提供的Requests例子,调用启动的服务所提供的接口。
最简单的接口调用。
import requests
r = requests.get("http://127.0.0.1:5000/")
result = r.json()
print(result)
接口的返回结果。
{"code": 10200, "message": "Welcome toAPI testing"}
如果,你想知道上面调用的接口是如何实现的,可以查看api_server.py文件。
# 最简单的json格式返回
@app.route('/')
def hello_world():
return jsonify({
"code": 10200,
"message": "Welcome to API testing"
})
Flask 实现接口是不是很简单?当然,还有更多复杂的接口实现,不过,这里的所有接口实现忽略了数据库的操作。
如果想做接口自动化测试,请参考tests/目录,里面提供了基于unittest 单元测试框架的用例。
如果本项目对你帮助,请帮忙加 star,有什么问题也可以通过issues提问。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Design Handbook
Baeck, Philippe de 编 / 2009-12 / $ 22.54
This non-technical book brings together contemporary web design's latest and most original creative examples in the areas of services, media, blogs, contacts, links and jobs. It also traces the latest......一起来看看 《Web Design Handbook》 这本书的介绍吧!