Python, run a test, send an email if it doesn't work

I want to run a test, and if it does not work, send an email. Please invite me to do this using any of the usual frameworks such as UnitTest. I have not found a way to change his behavior when he fails.

+4
source share
5 answers

You can provide your own implementation of unittest.TestResult to send email as follows:

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)
+4
source

You can override the run() unittest.TestSuite/ method TestCaseto notify the test result by email or any other channels. Check them out:

+1

. , . python mytest.py , . .

, unittest 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"
+1

- @GabiMe . :

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)

tests - , .

0

,

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()
-2

Source: https://habr.com/ru/post/1524502/


All Articles