Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何测试google驱动API python客户端_Python 3.x_Python Unittest_Google Drive Realtime Api - Fatal编程技术网

Python 3.x 如何测试google驱动API python客户端

Python 3.x 如何测试google驱动API python客户端,python-3.x,python-unittest,google-drive-realtime-api,Python 3.x,Python Unittest,Google Drive Realtime Api,我目前在django项目中有一个GoogleDriveAPI客户端,它可以正常工作 import unittest from unittest import mock DRIVE_API_VERSION = "v3" DRIVE_API_SERVICE_NAME = "drive" DRIVE_AUTHORIZED_USER_FILE = "path/to/secrets/json/file" DRIVE_SCOPES = ['https://www.googleapis.com/auth/d

我目前在django项目中有一个GoogleDriveAPI客户端,它可以正常工作

import unittest
from unittest import mock

DRIVE_API_VERSION = "v3"
DRIVE_API_SERVICE_NAME = "drive"
DRIVE_AUTHORIZED_USER_FILE = "path/to/secrets/json/file"
DRIVE_SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file ', 'https://www.googleapis.com/auth/drive.appdata']

def construct_drive_service():
    try:
        drive_credentials = google.oauth2.credentials.Credentials.from_authorized_user_file(
            DRIVE_AUTHORIZED_USER_FILE, scopes=DRIVE_SCOPES)
    except FileNotFoundError:
        print('Drive credentials not created')
        pass
    if drive_credentials:
        return build(DRIVE_API_SERVICE_NAME, DRIVE_API_VERSION, credentials=drive_credentials, cache_discovery=False)
    else:
        return None
我现在面临的挑战是为这个函数编写测试。但我不知道该用什么策略。我试过这个

class TestAPICalls(unittest.TestCase):

    @mock.patch('api_calls.google.oauth2.credentials', autospec=True)
    def setUp(self, mocked_drive_cred):
        self.mocked_drive_cred = mocked_drive_cred

    @mock.patch('api_calls.DRIVE_AUTHORIZED_USER_FILE')
    def test_drive_service_creation(self, mocked_file):
        mocked_file.return_value = "some/file.json"
        self.mocked_drive_cred.Credentials.return_value = mock.sentinel.Credentials
        construct_drive_service()
        self.mocked_drive_cred.Credentials.from_authorized_user_file.assert_called_with(mocked_file)
但是我的测试失败了,错误如下

    with io.open(filename, 'r', encoding='utf-8') as json_file:
ValueError: Cannot open console output buffer for reading
我知道客户端正在尝试读取文件,但却得到了一个
模拟对象
。问题是我不知道该怎么办 解决这个问题

我一直在阅读
mock
库,但整个过程仍然模糊不清

见: