使用mock 模拟接口返回

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()

 

转载于:https://my.oschina.net/donngchao/blog/3083290

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值