嗨,我想通过模拟api调用来测试下面的代码。
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我试过使用以下代码
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”
发布于 2022-01-24 03:31:33
您可以使用unittest.mock.patch来修补google.oauth2.service_account模块和googleapiclient.discovery.build函数。
例如。
example.py
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 rowstest_example.py
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()测试结果:
.
----------------------------------------------------------------------
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%https://stackoverflow.com/questions/70791609
复制相似问题