模拟调用三方服务接口
import unittest
from unittest import mock
from monitor_server import app
def mock_account_info(*args, **kwargs):
response_mock = mock.Mock()
response_mock.status_code = 200
if "getRpaStatusData" in args[0]:
response_mock.json.return_value = [
{
"userName": "xxx",
"displayName": "xxx",
"sipNum": "9876666",
"ip": "172.26.197.182",
"port": "8003",
"endPort": "8004",
"lastLiveTime": "2021-06-30 15:10:10",
"loginTime": "2021-06-30 15:10:30",
"noTask": "false"
},
]
elif "reStart" in args[0]:
response_mock.text = "ok"
elif "getPlanWorkTime" in args[0]:
response_mock.json.return_value = [{
"startTime": "09:05:00",
"endTime": "21:05:00",
}]
return response_mock
class TestCase(unittest.TestCase):
def setUp(self) -> None:
self.client = app.test_client()
@mock.patch('utils.restart_server.requests')
@mock.patch('utils.company.requests')
@mock.patch('utils.third_service.requests')
def test_task_server(self, mock_work_time, mock_get_account, mock_restart):
mock_restart.get.side_effect = mock_account_info
mock_work_time.get.side_effect = mock_account_info
mock_get_account.get.side_effect = mock_account_info
response = self.client.get("/taskServer?company=2121&type=taskTime")
data = response.get_data(as_text=True)
print("---")
print(data)
if __name__ == '__main__':
unittest.main()
|