首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何模拟googleapiclient.discovery.build到google中的单元测试读取

如何模拟googleapiclient.discovery.build到google中的单元测试读取
EN

Stack Overflow用户
提问于 2022-01-20 18:32:22
回答 1查看 829关注 0票数 2

嗨,我想通过模拟api调用来测试下面的代码。

代码语言:javascript
运行
复制
def get_data_from_google_sheets(spreadsheet_id, google_creds):
    """
    Pupose:
    """
    scopes = ['https://www.googleapis.com/auth/sqlservice.admin','https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    range_name = 'A1:AA1000'

  

    credentials = service_account.Credentials.from_service_account_info(google_creds, scopes = scopes)
    
    service = build('sheets', 'v4', credentials = credentials)
    
    sheet = service.spreadsheets()
 

    result_from_sheet = sheet.values().get(spreadsheetId = spreadsheet_id,range = range_name).execute()

    rows = result_from_sheet['values'][1:]
 
    return rows

我试过使用以下代码

代码语言:javascript
运行
复制
def test_get_data(mocker):
    sheet_id = "123"
    creds = {"b":"d"}
    mocker.patch.object(
        google.oauth2.service_account.Credentials,'from_service_account_info',
        return_value = ""
    )

    mocker.patch.object(
        googleapiclient.discovery.build.return_value.spreadsheets.return_value.values.return_value.get.return_value,'execute',
        return_value = []
    )

    result = get_data_from_google_sheets(sheet_id, creds)
    assert len(result) == 0

获取错误- AttributeError:“函数”对象没有属性“return_value”

EN

回答 1

Stack Overflow用户

发布于 2022-01-24 03:31:33

您可以使用unittest.mock.patch来修补google.oauth2.service_account模块和googleapiclient.discovery.build函数。

例如。

example.py

代码语言:javascript
运行
复制
from google.oauth2 import service_account
from googleapiclient.discovery import build

def get_data_from_google_sheets(spreadsheet_id, google_creds):
    scopes = ['https://www.googleapis.com/auth/sqlservice.admin','https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    range_name = 'A1:AA1000'
    credentials = service_account.Credentials.from_service_account_info(google_creds, scopes = scopes)
    service = build('sheets', 'v4', credentials = credentials)
    sheet = service.spreadsheets()
    result_from_sheet = sheet.values().get(spreadsheetId = spreadsheet_id,range = range_name).execute()
    rows = result_from_sheet['values'][1:]
    return rows

test_example.py

代码语言:javascript
运行
复制
from unittest import TestCase
import unittest
from unittest.mock import Mock, patch
from example import get_data_from_google_sheets


class TestExample(TestCase):
    @patch('example.build')
    @patch('example.service_account.Credentials')
    def test_get_data(self, mock_service_acount_credentials, mock_build):
        sheet_id = "123"
        creds = {"b": "d"}
        mock_service_acount_credentials.from_service_account_info.return_value = '123'
        mock_build.return_value.spreadsheets.return_value.values.return_value.get.return_value.execute.return_value = {
            'values': []}
        result = get_data_from_google_sheets(sheet_id, creds)
        self.assertEqual(len(result), 0)


if __name__ == '__main__':
    unittest.main()

测试结果:

代码语言:javascript
运行
复制
.
----------------------------------------------------------------------
Ran 1 test in 0.004s

OK
Name                                         Stmts   Miss  Cover   Missing
--------------------------------------------------------------------------
src/stackoverflow/70791609/example.py           11      0   100%
src/stackoverflow/70791609/test_example.py      16      0   100%
--------------------------------------------------------------------------
TOTAL                                           27      0   100%
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70791609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档