端午节我写了一个项目,帮助你学习HTTP接口测试。python
GitHub地址:git
https://github.com/defnngj/learning-API-testgithub
整个项目基于Flask和 Requests实现。shell
Flask是Python主流的Web框架,以简单著称,它可很是方便的实现API,整个项目中的API都经过一个文件实现。
Requests是模拟HTTP的测试库,一样是Python语言的明星库,它能够以很是简单的方式模拟HTTP请求。数据库
克隆或下载项目,安装依赖。json
$ pip install -r requirements.txt
启动Flask项目。api
$ 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例子,调用启动的服务所提供的接口。app
最简单的接口调用。框架
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提问。