Sha*_*awn 6 python unit-testing mocking boto3
我是 Python 单元测试的新手,我想模拟对 boto3 第三方库的调用。这是我的精简代码:
真实代码.py:
import boto3
def temp_get_variable(var_name):
return boto3.client('ssm').get_parameter(Name=var_name)['Parameter']['Value']
Run Code Online (Sandbox Code Playgroud)
测试真实代码.py:
import unittest
from datetime import datetime
from unittest.mock import patch
import real_code
class TestRealCode(unittest.TestCase):
@patch('patching_config.boto3.client')
def test_get_variable(self, mock_boto_client):
response = {
'Parameter': {
'Name': 'MyTestParameterName',
'Type': 'String',
'Value': 'myValue',
'Version': 123,
'Selector': 'asdf',
'SourceResult': 'asdf',
'LastModifiedDate': datetime(2019, 7, 16),
'ARN': 'asdf'
}
}
mock_boto_client.get_variable.return_value = response
result_value = real_code.get_variable("MyTestParameterName")
self.assertEqual("myValue", result_value)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,测试失败
Expected :myValue
Actual :<MagicMock name='client().get_parameter().__getitem__().__getitem__()' id='2040071816528'>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我认为通过设置mock_boto_client.get_variable.return_value = response它会模拟呼叫并返回我的预设回复。我不明白为什么我得到的是 MagicMock 对象而不是我尝试设置的返回值。我想设置我的测试,以便当get_parameter使用特定参数进行调用时,模拟会返回我在测试中指定的预设响应。
您的测试代码有两个问题。第一个是当你的模拟对象mock_boto_client调用时,它返回一个新的模拟对象。这意味着get_parameter()正在调用的对象与您尝试设置返回值的对象不同。您可以使用以下命令让它自行返回:
mock_boto_client.return_value = mock_boto_client
Run Code Online (Sandbox Code Playgroud)
您还可以使用不同的模拟对象:
foo = MagicMock()
mock_boto_client.return_value = foo
Run Code Online (Sandbox Code Playgroud)
您遇到的第二个问题是您正在模拟错误的方法调用。mock_boto_client.get_variable.return_value应该mock_boto_client.get_parameter.return_value。这是更新并运行的测试:
import unittest
from datetime import datetime
from unittest.mock import patch
import real_code
class TestRealCode(unittest.TestCase):
@patch('boto3.client')
def test_get_variable(self, mock_boto_client):
response = {
'Parameter': {
'Name': 'MyTestParameterName',
'Type': 'String',
'Value': 'myValue',
'Version': 123,
'Selector': 'asdf',
'SourceResult': 'asdf',
'LastModifiedDate': datetime(2019, 7, 16),
'ARN': 'asdf'
}
}
mock_boto_client.return_value = mock_boto_client
mock_boto_client.get_parameter.return_value = response
result_value = real_code.get_variable("MyTestParameterName")
self.assertEqual("myValue", result_value)
Run Code Online (Sandbox Code Playgroud)