vendredi 30 juin 2017

Running smtpd.DebuggingServer in pytest fails but unittest work?

I am attempting to unit test my emailing functionality using the smtpd package. This package allows the use of a debug server.

When I run this test using pytest it produces an error during collection:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtpd.py:646: in __init__
type=socket.SOCK_STREAM)
E   TypeError: getaddrinfo() got an unexpected keyword argument 'type'

However if I run this test using unittest, it manages to pass fine. Both of them are run inside the same virtual environment using Python3.6

This is the code I use to instantiate the debugging server and the test functions:

class DebuggingServer(smtpd.DebuggingServer):

    def __init__(self, addr, port):
        smtpd.DebuggingServer.__init__(self, localaddr=(addr, port), remoteaddr=None)


class DebuggingServerThread(threading.Thread):

    def __init__(self, addr='localhost', port=1025):
        threading.Thread.__init__(self)
        self.server = DebuggingServer(addr, port)

    def run(self):
        asyncore.loop(use_poll=True)

    def stop(self):
        self.server.close()
        self.join()


class TestMyEmail(unittest.TestCase):

    server = DebuggingServerThread()

    def setUp(self):

        self.server.start()
        print('Server has started')

    def tearDown(self):

        self.server.stop()
        print('Server has stopped')

    def test_png(self):

        png_files = [os.path.join(DATA_DIR, 'page1.png'),
                 os.path.join(DATA_DIR, 'page2.png')]

        with open(os.path.join(DATA_DIR, 'test_png.txt'), 'w+') as f:
            with redirect_stdout(f):
                success = myemail.mail(recipients=['email@email'],
                                       message="Test email sent from server to test inline attachment of png files",
                                       attachments=png_files,
                                       subject="TestMyEmail.test_png")

        with open(os.path.join(DATA_DIR, 'test_png.eml'), 'w+') as email_file:

            gen = generator.Generator(email_file)
            gen.flatten(success)

        assert type(success)

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

Aucun commentaire:

Enregistrer un commentaire