zoukankan      html  css  js  c++  java
  • 我写了个项目,帮你学习HTTP接口测试!

    端午节我写了一个项目,帮助你学习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提问。

     来源  :  虫师   https://www.cnblogs.com/fnng/
  • 相关阅读:
    Vim Reference
    Java 8 Consumer、Supplier、Predicate、Function
    Java 8 Stream 用法
    Java 基础 Builder模式
    Spring/Spring-Boot 学习 使用自定义的ArgumentResolver
    架构之分布式图片存储系统架构
    微服务和SOA服务
    Centos 上 Tengine安装
    .NET平台上插拔姿势的AOP
    P1424 刷题记录
  • 原文地址:https://www.cnblogs.com/rxxbb123/p/14231902.html
Copyright © 2011-2022 走看看