【发布时间】:2021-10-06 20:39:12
【问题描述】:
我正在使用 mockito 对 Python 中的程序进行单元测试。我有一个像这样的课程:
import boto3
import datetime
class Cache:
def __init__(self):
client = boto3.resource('s3')
self.bucket_name = 'name'
self.bucket = client.Bucket(self.bucket_name)
def setup_cache(self, cache_file='cache.csv', cache_filepath='cache'):
cache_object = self.bucket.Object(cache_file)
if cache_object.last_modified < datetime.datetime.now():
self.bucket.download_file(cache_filepath, cache_file)
else:
print('Cache already up to date')
def main():
cache = Cache()
cache.setup_cache()
我遇到的测试代码是这样的:
from mockito import mock, when
import datetime
import boto3
import mock_cache
class TestMockCache:
def test_last_mod(self):
mock_client = mock()
when(boto3).resource('s3').thenReturn(mock_client)
mock_bucket = mock()
when(mock_client).Bucket('name').thenReturn(mock_bucket)
mock_bucket.last_modified = datetime.datetime.now()
mock_cache.main()
在单元测试上运行 pytest 时,我收到了这个属性错误:
AttributeError: 'NoneType' object has no attribute 'last_modified'
从文档看来,我可以像这样分配“cache_mock.last_modified”。不过,我也试过:
when(cache_mock).last_modified.thenReturn(test_date)
得到:
AttributeError: 'StubbedInvocation' object has no attribute 'thenReturn'
我不完全理解,但假设这意味着 mockito mock() 对象不能有多个返回值?
对此的任何帮助将不胜感激。我觉得我误解了关于 mockito 的模拟如何工作或一般模拟的一些基本知识。
【问题讨论】:
-
您能生成实时代码示例来重现问题吗?还请指定您尝试使用的 some_cache_library?
-
@Roxy 编辑了代码。 'some_cache_library' 只是一个虚构的东西。这里真正起作用的库是 boto3
-
预期相同。我已经更新了下面的答案。如果您仍然无法解决,请告诉我。
-
您是否能够通过以下解决方案?
-
谢谢!那行得通。
标签: python unit-testing mocking mockito attributeerror