Python,运行测试,如果失败则发送电子邮件

时间:2014-01-30 13:19:35

标签: python

我想要运行测试,如果失败,请发送电子邮件。 请建议我可以使用任何传统的框架,如UnitTest。我没有找到一种方法来在失败时修改它的行为。

6 个答案:

答案 0 :(得分:5)

您可以提供自己的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)

答案 1 :(得分:1)

您可以覆盖run() / unittest.TestSuite的{​​{1}}方法,通过电子邮件或任何其他渠道通知测试结果。看看这些:

答案 2 :(得分:1)

您可以使用fabric来处理测试结果并通过电子邮件发送。这种方法的好处是你保持你的单元测试不变。也就是说,您仍然可以在不发送不必要的电子邮件的情况下运行python mytest.py,例如在您当地的开发环境中。

假设您在文件mytest.py中有这个单元测试:

class MyTest(unittest.TestCase):
     def test_spam(self):
         ...
         self.assertTrue(condition) 

if __name__ == '__main__':
    unittest.main()

fabfile.py添加:

 def test(from_email="server@mydomain.com",
                 to_email="test@mydomain.com"):
    with settings(warn_only=True):
        result=local('python mytest.py', capture=True)
        if result.failed:
            # prepare message
            msg = MIMEText(result)
            msg['From'] = from_email
            msg['To'] = to_email
            msg['Subject'] = "Tests failed"
            # send email
            s = smtplib.SMTP('localhost')
            s.sendmail(from_email, [to_email], msg.as_string())

然后您可以按如下方式调用您的测试:

# send to default email address
fab test
# send to another email address
fab test:to_email="other@mydomain.com"

答案 3 :(得分:1)

出于某种原因,并非所有@ GabiMe的答案都适合我。相反,我用过:

write()

其中def sendmail(from_who, to, msg): s = smtplib.SMTP('localhost') s.sendmail(from_who, [to], msg) s.quit() class TestResults(unittest.TestResult): def addError(self, test, err): super(TestResults, self).addError(test, err) error = self._exc_info_to_string(err, test) sendmail(from_who, to, err_desc) def addFailure(self, test, err): super(TestResults, self).addFailure(test, err) error = self._exc_info_to_string(err, test) sendmail(from_who, to, err_desc) if __name__ == '__main__': suite = unittest.TestSuite( unittest.TestLoader().discover('tests') ) results = TestResults() suite.run(results) 是包含我的测试的文件夹的名称。

答案 4 :(得分:0)

import smtplib

from email.mime.text import MIMEText

msg = MIMEText('todi si tho lift karade :P')

me = 'from_mail@gmail.com'

to = 'to_mail@gmail.com'

pswd = 'abcderfyqwerpokl'

#(if 2 factor-auth is active for your mail, visit https://myaccount.google.com/apppasswords select your app device and generate your  12 letter app password, Give those credentials to signin when you are writing the code)

msg['Subject'] = "lift_karade"

msg['From'] = me

msg['To'] = to

s = smtplib.SMTP('smtp.gmail.com', 587)

s.starttls()

s.login(me,pswd)

s.sendmail(me, [to], msg.as_string())

答案 5 :(得分:-2)

你可以试试,除了块

import script

try:
   script()
except Exception, e:
   import smtplib
   from email.mime.text import MIMEText
   msg['Subject'] = 'Script failed!!'
   msg['From'] = me
   msg['To'] = you
   s = smtplib.SMTP('localhost')
   s.sendmail(me, [you], e.message)
   s.quit()