在 Python 中模拟对象
我是 Python 中单元测试和模拟对象的新手。我有一个需要为其创建单元测试的函数。
def BuildBall(self, material):
"""Create a Ball from material."""
result = {}
for b in xrange(material.ball_size()):
ball = material.ball(b)
result[ball.name()] = (ball.name(), ball.description())
return result
我想创建一个虚拟对象(材质)并将其传递给 BuildBall(),以便我可以预测单元测试中的结果。
我不太确定在这种情况下如何将虚拟对象传递给我的方法。知道我是否可以使用 mox 在我的情况下创建一个虚拟对象吗?
提前致谢
I'm new to Unit testing and mocking objects in Python. I have a function that I need to create a unit test for.
def BuildBall(self, material):
"""Create a Ball from material."""
result = {}
for b in xrange(material.ball_size()):
ball = material.ball(b)
result[ball.name()] = (ball.name(), ball.description())
return result
I want to create a dummy object(material) and pass it to BuildBall(), so that I can predict what the result will look like in my unit test.
I'm not too sure how I can pass a dummy object to my method in this case. Any idea if I can use mox to create a dummy object in my case?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 Python Mocker 包:
它允许您轻松生成模拟特定功能以进行测试的对象。您可以在此处找到一个简单的示例。
Take a look at the Python Mocker package:
It allows you to easily generate objects that mock a certain functionality for testing. You can find a simple example here.