注意事项:moke作为第三方模块在Python2中需要安装,安装方式如下;在Python3中moke模块是在unittest中的,调用的方法是from unittest import moke
1、mock模块:模拟接口返回请求,使得可以和代码的开发进行并行开发
2、安装mock:
在终端进行安装:pip install mock
在编译器上进行安装:file->Settings->Project:文件夹名称->Project lnterpreter
添加成功:在终端上输入python->import mock
3、mock的使用
导入mock
1 from mock import mock
使用mock
import requests import unittest import json from unittest import mock class Csjg(unittest.TestCase): def test_01(self): url = ‘http://39.105.34.27/projects/index.php/index/user/login.html‘ data = { ‘mobile‘: ‘cctv321‘, ‘password‘: ‘as110001‘, ‘type‘: 1, } headers = { ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36‘ } response = requests.post(url=url , data=data ,headers=headers).status_code mlsj = mock.Mock(return_value=200) fhjg = mlsj(response) self.assertEqual(fhjg , 300, ‘测试失败,返回的状态码不等于200‘) if __name__ == ‘__main__‘: # cs=Csjg() # cs.test_01() unittest.main()
原文:https://www.cnblogs.com/zihkj/p/12159309.html