python mock的用法如下:
#coding:utf-8
from mock import mock
def mock_test(mock_method,request_data,url,method,response_data):
mock_method = mock.Mock(return_value=response_data)
res = mock_method(url,method,request_data)
return res
可以据此进行接口的模拟返回测试:
#coding:utf-8
import unittest
import json
import HtmlTestRunner
from mock import mock
from mock_demo import mock_test
from requestsdemo import Runmain
class TestMethod(unittest.TestCase):
@classmethod
def setUpClass(cls):
print "before class execute, this method should be run"
@classmethod
def tearDownClass(clss):
print "after class execute, this method should be run"
def setUp(self):
self.run = Runmain()
self.userid = self.test_01()
print 'test-->setup'
def tearDown(self):
print 'test-->teardown'
def test_02(self):
# print global_value
url = 'http://127.0.0.1:8000/login/'
data = {'username':'test111','password':'1111111222'}
res = mock_test(self.run.run_main,data,url,"POST",data)
print res
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestMethod('test_02'))
unittest.main()