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

2018-01-23 11:48:51 +08:00
 lpts007

大部分 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

1118 次点击
所在节点    问与答
1 条回复
lpts007
2020-05-20 15:23:16 +08:00
把 http_call mock 一下就行了啊

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/425182

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX