mock,在接口未开发完毕时,使用mock模拟返回数据
from unittest import mock #python3 unittest自带mock
def test_02(self):
url = 'https://uskid.com/api/user/login?token=undefined&ms=undefined'
data = {"account":"17610266910","password":"5nagesha"}
mock_data = mock.Mock(return_value = data) #返回值指定为data的值
print(mock_data)
self.run.run_main = mock_data #直接把data的值赋给self.run.run_main
res = self.run.run_main(url=url,method='POST',data=data)
#res = eval(res)
print(res)
self.assertEqual(res['password'],'5nagesha','测试失败思密达')
from mock_test import mock_test
def test_02(self):
url = 'https://uskid.com/api/user/login?token=undefined&ms=undefined'
data = {"account":"17610266910","password":"5nagesha"}
#self.run.run_main = mock.Mock(return_value=data)---------------->1
res = mock_test(self.run.run_main,data,url,'post',data)
#res = self.run.run_main(url,'POST',data)
#res = eval(res)
print(res)
#self.assertEqual(res['password'],'5nagesha','测试失败思密达')
# -*- coding: utf-8 -*-
#import unittest
from unittest import mock
def mock_test(mock_method,request_data,url,method,response_data):
mock_method = mock.Mock(return_value = response_data)---------------->1
res = mock_method(url,method,request_data)
return res