我想运行测试,如果失败,请发送电子邮件。 请建议我可以使用任何传统框架(如 UnitTest)来执行此操作。我还没有找到一种方法来修改它失败时的行为。
最佳答案
您可以提供自己的 unittest.TestResult 实现来发送这样的电子邮件:
import smtplib
import unittest
def sendmail(from_who, to, msg):
s = smtplib.SMTP('localhost')
s.sendmail(from_who, [to], msg)
s.quit()
class MyTestResult(unittest.TestResult):
def addError(self, test, err):
self.super(MyTestResult, self).addError(test, err)
err_desc = self._exc_info_to_string(err, test)
sendmail(from_who, to, err_desc)
def addFailure(self, test, err):
self.super(MyTestResult, self).addFailure(test, err)
err_desc = self._exc_info_to_string(err, test)
sendmail(from_who, to, err_desc)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromModule()
results = MyTestResult()
suite.run(results)
关于Python,运行测试,如果失败则发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21457665/