python mock使用

应用场景:项目初期,接口文档已给出,复杂接口功能未实现。但其他接口链路需要调用,可以先mock一个返回值。

 

from unittest import mock

url = "https://docs.open.alipay.com/api_1/alipay.trade.pay"
# 账号密码都正确,正常
data = {'user': 'python01', 'pay_pwd': 123456,'money':88.88}

res_data = {
    "alipay_trade_pay_response": {
        "code": "10000",
        "msg": "Success"
    },
    "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}
# 创建一个mock对象
request = mock.Mock(return_value=res_data)

# 调用mock对象
response = request(url=url,data=data)
print(response)
from unittest.mock import Mock

gen_sign = Mock(return_value="yJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJf")

class TestLogin(unittest.TestCase):

    def test_login(self):
        data = {
            "user": "xx",
            "pwd": "ldjfss",
            #调用的gen_sign moke方法参数可以随便传,无影响
            "sign": gen_sign("xx",name="mmm")
        }
        url = "http://127.0.0.1:8000/login"
        reponse = request(url=url,json=data,method="post")        

 

你可能感兴趣的:(python mock使用)