大部分 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
lpts007 OP 把 http_call mock 一下就行了啊
|