【发布时间】:2020-01-30 00:54:00
【问题描述】:
问题
我正在寻找一种在我的单元测试中正确模拟对象的方法,但我无法让 unittest.mock.create_autospec 或 unittest.mock.Mock 做我需要的事情。我想我需要对 Mock 对象使用继承,但我无法让它工作。
我需要在看起来像这样的第 3 方模块中模拟类的图像(假设带有 raise NotImplementedError 的行是我想在单元测试中避免的外部 API 调用):
class FileStorageBucket():
def __init__(self, bucketname: str) -> None:
self.bucketname = bucketname
def download(self, filename) -> None:
raise NotImplementedError
# ...lots more methods...
class FileStorageClient():
def auth(self, username: str, password: str) -> None:
raise NotImplementedError
def get_bucket(self, bucketname: str) -> FileStorageBucket:
raise NotImplementedError
return FileStorageBucket(bucketname)
# ...lots more methods...
它可能会像这样在我的应用程序的其他地方使用:
client = FileStorageClient()
client.auth("me", "mypassword")
client.get_bucket("my-bucket").download("my-file.jpg")
如果我将 FileStorageClient 替换为 Mock 对象,我希望能够确定我的单元测试是否在以下位置运行任何代码:
- 调用
FileStorageClient或FileStorageBucket上不存在的方法 - 在
FileStorageClient或FileStorageBucket上确实存在的方法被调用时使用了错误的参数
因此,client.get_bucket("foo").download() 应该引发一个异常,即文件名是 .download() 的必需参数。
我尝试过的事情:
首先,我尝试使用create_autospec。它能够捕获某些类型的错误:
>>> MockClient = create_autospec(FileStorageClient)
>>> client = MockClient()
>>> client.auth(user_name="name", password="password")
TypeError: missing a required argument: 'username'
但是,当然,因为它不知道get_bucket 应该具有的返回类型,所以它不会捕获其他类型的错误:
>>> MockClient = create_autospec(FileStorageClient)
>>> client = MockClient()
>>> client.get_bucket("foo").download(wrong_arg="foo")
<MagicMock name='mock.get_bucket().download()' id='4554265424'>
我想我可以通过创建继承自 create_autospec 输出的类来解决这个问题:
class MockStorageBucket(create_autospec(FileStorageBucket)):
def path(self, filename) -> str:
return f"/{self.bucketname}/{filename}"
class MockStorageClient(create_autospec(FileStorageClient)):
def get_bucket(self, bucketname: str):
bucket = MockStorageBucket()
bucket.bucketname = bucketname
return bucket
但它实际上并没有按预期返回 MockStorageBucket 实例:
>>> client = MockStorageClient()
>>> client.get_bucket("foo").download(wrong_arg="foo")
<MagicMock name='mock.get_bucket().download()' id='4554265424'>
然后我尝试从 Mock 继承并在 init 中手动设置“规范”:
class MockStorageBucket(Mock):
def __init__(self, *args, **kwargs):
# Pass `FileStorageBucket` as the "spec"
super().__init__(FileStorageBucket, *args, **kwargs)
def path(self, filename) -> str:
return f"/{self.bucketname}/{filename}"
class MockStorageClient(Mock):
def __init__(self, *args, **kwargs):
# Pass `FileStorageClient` as the "spec"
super().__init__(FileStorageClient, *args, **kwargs)
def get_bucket(self, bucketname: str):
bucket = MockStorageBucket()
bucket.bucketname = bucketname
return bucket
现在,get_bucket 方法按预期返回了一个MockStorageBucket 实例,并且我能够捕获一些错误,例如访问不存在的属性:
>>> client = MockStorageClient()
>>> client.get_bucket("my-bucket")
<__main__.FileStorageBucket at 0x10f7a0110>
>>> client.get_bucket("my-bucket").foobar
AttributeError: Mock object has no attribute 'foobar'
但是,与使用 create_autospec 创建的 Mock 实例不同,使用 Mock(spec=whatever) 的 Mock 实例似乎不会检查是否将正确的参数传递给函数:
>>> client.auth(wrong_arg=1)
<__main__.FileStorageClient at 0x10dac5990>
【问题讨论】:
标签: python unit-testing mocking python-unittest python-mock