注意事項: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()