我无法测试以下代码。如果删除client_mock,则测试将成功运行,显示已调用boto3.client()。但是我无法测试返回的模拟客户端进行的任何调用。我收到AssertionError:预期已调用'get_paginator'。
我知道moto和安慰剂,但是出于本练习的目的,我试图学习如何使用unittest.mock进行此操作。
accounts.py
import boto3
def get_accounts():
'''
Return a list of account information from the organization.
'''
client = boto3.client('organizations')
paginator = client.get_paginator('list_accounts')
response_iterator = paginator.paginate()
accounts = []
for page in response_iterator:
for account in page['Accounts']:
accounts.append(account)
return accounts
tests.py
import unittest
from accounts import get_accounts, get_session
from unittest.mock import patch
class TestAccounts(unittest.TestCase):
@patch('accounts.boto3')
@patch('accounts.boto3.client')
def test_get_accounts(self, client_mock, boto3_mock):
get_accounts()
boto3_mock.client.assert_called()
client_mock.get_paginator.assert_called()
if __name__ == '__main__':
unittest.main()