V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lpts007
V2EX  ›  问与答

存在调用其他 api 的 api 怎么测试?

  •  
  •   lpts007 · 2018-01-23 11:48:51 +08:00 · 1102 次点击
    这是一个创建于 2500 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大部分 api 都是 rpc 调微服务,就直接设置预期 return,但是还存在发起其他 http 请求,或是 import 一个函数(进行数据库操作,而且 session 也定义好了)然后执行。 举个例子:

    from xxx import operate_db
    from yyy import http_call
    class API(app):
        ...
        @api(name, "get_count")
        def get_count(self, param):
            self.rpc.some_service.some_func(param)
            operate_db(param)  #no session taken, because it's defined in xxx.py
            http_call("www.host.com/some_other_api", param)   #request other api
            ....
    

    现在架构师不让用真实数据库测试,但是 operate_db http_call 我拦不住啊 肯定依赖真实数据库啊。所以想问下有没有办法写个装饰器 判断函数内部是否有某些代码,有的话直接不执行 设置结果代替

    My problem is to test api function where other api being called or some db operatation func imported(means its dbsession is defined in other context) and executed. Except micro service easy to assume its return, this 2 situation above I just cannot handle. So I want to find way to judge whether given code shows in func to be tested or not, if yes, assume its return.

    If my comprehesion to unittest of api(called via http) is wrong, what should I test these api.

    ps: I was asked to not use real db, even not a real test db. so come with this problem

    1 条回复    2020-05-20 15:23:16 +08:00
    lpts007
        1
    lpts007  
    OP
       2020-05-20 15:23:16 +08:00
    把 http_call mock 一下就行了啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1006 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 21:22 · PVG 05:22 · LAX 13:22 · JFK 16:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.