[英]Unit testing a change in a function that is by design not reflected in the function return value
import smtplib
from email.mime.text import MIMEText
def test():
frm = "a@b.com"
to = "c@d.com"
# Original line: msg = MIMEText("Hello, Dear")
msg = MIMEText("Hello, World") # Changed line
try:
s = smtplib.SMTP("localhost")
s.sendmail(frm, to, msg.as_string())
except Exception as e:
return False
return True
test()
現在,我要對在味精行上所做的更改進行單元測試。 換句話說,我希望能夠斷言在代碼運行時msg值確實是“ Hello,World”,或者sendmail()調用確實發送了一條消息,說“ Hello,World”。 我該怎么做?
這是使用模擬的一個很好的例子。 在任何情況下,您都不希望單元測試實際發送電子郵件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.