测试时用赝品(mock、stub等)替换真实对象。
fudge-je的Python项目详细描述
完整的文档可在http://farmdev.com/projects/fudge/
fudge是一个python模块,用于使用假对象(mock和stub)来测试真实对象。
在可读的python代码中,声明哪些方法可用于您的伪和 如何称呼他们。然后将其注入应用程序并启动 测试。这种声明性的方法意味着您不必录制和回放 在运行代码之后,您不必检查您的赝品。如果是假的 对象的使用不正确,则您将看到一条信息丰富的异常消息 带着指向罪犯的痕迹。
下面是如何测试发送的代码的快速预览 不实际发送电子邮件的电子邮件:
@fudge.patch('smtplib.SMTP') def test_mailer(FakeSMTP): # Declare how the SMTP class should be used: (FakeSMTP.expects_call() .expects('connect') .expects('sendmail').with_arg_count(3)) # Run production code: send_mail() # ...expectations are verified automatically at the end of the test